我已经使用localStorage构建了一个购物车,并且正在使用PayPal PHP SDK来处理付款。当用户选择通过PayPal支付时,我会通过AJAX将localStorage数据(购物车)和表单数据(用户详细信息)发布到PHP页面。在该页面上,我设置了PayPal API并获取POST数据以创建项目、交易、付款和重定向URL等内容。如果成功,它将返回批准的URL以重定向到PayPal(使用window.location.href),所有这些功能都有效。请注意,HTML标签需要保留。
然后访问了我设置的returnURL,即
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
请求是“success”,则验证并进行付款。到此为止,一切都很顺利。接下来,我想向用户发送包含localStorage中某些数据的电子邮件收据。然而,问题在于在这个returnURL上,$_POST
请求中不再存储localStorage。显然,我可以将所有这些信息作为$_GET
请求传递,但不想在URL(?email=&address=&order=
)中显示这些信息。您是否有任何建议或方法,使我能够在返回到PayPal的returnURL之前访问localStorage或$_POST
数据?以下是当前包含在我的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 {
}
}
$_SESSION['cartData'] = $_POST['cartData'];
,然后在returnURL上,为了调试目的,使用var_dump($_SESSION['cartData']);
但它只是返回了NULL。 - John the Painter