2017-08-25 112 views
3

我已經使用localStorage構建了購物車,並且使用PayPal PHP SDK來處理付款。

在點擊通過貝寶支付,通過AJAX,我張貼$_POST的localStorage的數據(車)和表格數據(用戶的詳細信息)的PHP頁面,我有PayPal的API設置,然後抓起$_POST數據(創建的項目,交易,支付,redirectURLs),併成功返回批准的URL重定向到貝寶(使用window.location.href,而這一切工作正常。

var formData = form.serialize(); 
var cartData = JSON.parse(localStorage.getItem('drfStorage')); 

$.ajax({ 
    url: rootURL + 'api/payment__paypal.php', 
    type: 'POST', 
    data: { formData: formData, cartData: cartData }, 
    beforeSend: function() { 
     console.log('processing'); 
    }, 
    success: function(data) { 
     console.log('success!'); 
     console.log(data); 
     window.location.href = data; 
    }, 
    error: function(xhr,err) { 
     console.log('readyState: '+xhr.readyState+'\nstatus: '+xhr.status); 
     console.log('responseText: '+xhr.responseText); 
    } 
}); 

然後我RETURNURL被設置爲redirect__paypal.php?pp_success=true被訪問,如果$_GET請求是'成功',那麼它驗證並且付款。

這一切都運作良好,直到這一點。下一個階段是我想向包含localStorage中某些數據的用戶發送電子郵件收據但問題是,在此returnURL上不再存儲$_POST請求中的localStorage。我顯然可以將所有這些信息作爲$ _GET請求傳遞,但並不真正需要URL中的這些信息(?email=&address=&order=)等。

有什麼方法或建議可以看到我能夠訪問localStorage或$_POST數據在它返回到PayPal的returnURL之前?

下面是我的redirect__paypal.php目前包含的內容,以幫助解釋。

use PayPal\Api\Payment; 
use PayPal\Api\PaymentExecution; 

// Require relevent libraries 
require_once('./sendgrid/sendgrid-php.php'); 
require_once('./api__paypal.php'); 

// SendGrid API init 
$sgAPIKey = "REMOVED FROM EXAMPLE"; 

if (isset($_GET['pp_success'])) { 

    $approved = $_GET['pp_success'] === 'true'; 

    if ($approved) { 

     $payerID = $_GET['PayerID']; 
     $paymentID = $_GET['paymentId']; 

     $payment = Payment::get($paymentID, $api); 

     $execution = new PaymentExecution(); 
     $execution->setPayerId($payerID); 

     $payment->execute($execution, $api); 

     // Set email confirmation settings 
     $email_admin = 'REMOVED FROM EXAMPLE'; // Client 
     $email_customer = 'REMOVED FROM EXAMPLE'; 
     $email_admin_subject = 'You have a new order from Testing McTest via PayPal'; 
     $email_admin_customer = 'Your Testing McTest order'; 

     ob_start(); 
      require_once './confirmation__email--admin.php'; 
      $email_admin_body = ob_get_contents(); 
     ob_end_clean(); 

     ob_start(); 
      require_once './confirmation__email--customer.php'; 
      $email_customer_body = ob_get_contents(); 
     ob_end_clean(); 

     // SendGrid init 
     function send_email($from_email, $to_email, $subject, $body/*, $attachments*/) { 
      global $sgAPIKey; 
      $from = new SendGrid\Email(null, $from_email); 
      $to = new SendGrid\Email(null, $to_email); 
      $content = new SendGrid\Content("text/html", $body); 
      $mail = new SendGrid\Mail($from, $subject, $to, $content); 
      //foreach($attachments as $a) { 
      // $mail->addAttachment($a); 
      //} 
      $sg = new \SendGrid($sgAPIKey); 
      $response = $sg->client->mail()->send()->post($mail); 
     } 

     // Send confirmation to customer first before it clears the attachments 
     if ($email_customer) { 
      send_email($email_admin, $email_customer, $email_admin_customer, $email_customer_body/*, $attachments*/); 
     } 

     // Send to confirmation to admin 
     if ($email_admin) { 
      send_email($email_admin, $email_admin, $email_admin_subject, $email_admin_body/*, $attachments = []*/); 
     } 

    } else { 



    } 

} 

回答

2

我認爲您需要將您的數據保存到貝寶前的地方。 在重定向時,所有$ _POST字段丟失。

的easist辦法就是保存所有數據會話($ _SESSION) 你可以從那裏抓住它,當你從貝寶回來;)

+1

感謝您的答覆。我試過這個(在payment__paypal.php中)設置'$ _SESSION ['cartData'] = $ _POST ['cartData'];'然後在returnURL上,出於調試的目的'var_dump($ _ SESSION ['cartData']] );'但它只是返回NULL –

+2

你的會話是否開始? 你是否在你的索引上使用session_start();在使用$ _SESSION變量之前? – Olifant1990

+1

就是這樣:)謝謝! –