Ich habe einen Einkaufswagen mit localStorage erstellt und verwende das PayPal PHP SDK, um die Zahlung zu verarbeiten.
Beim Klicken durch PayPal, über AJAX zu bezahlen, ich bin Entsendung $_POST
die localstorage-Daten (der Wagen) und Formulardaten (die Details des Benutzers) auf eine PHP-Seite, wo ich die PayPal-API-Konfiguration haben, die dann die $_POST
packt Daten und zum Erfolg gibt sie die genehmigte URL (die Elemente, Transaktion, Zahlung, redirectURLs zu erstellen) zu PayPal (mit window.location.href
, und das alles funktioniert gut.
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);
}
});
Dann ist mein ReturnUrl zu umleiten, die als redirect__paypal.php?pp_success=true
gesetzt wird besucht, wenn die $_GET
Anfrage ist "Erfolg" dann validiert und nimmt die Zahlung.
Dies funktioniert alles gut bis zu diesem Zeitpunkt. Die nächste Stufe ist, dass ich eine E-Mail-Empfangsbestätigung an den Benutzer senden möchte, die einige der Daten von localStorage enthält. JEDOCH ist das Problem, dass auf dieser returnURL nicht mehr die localStorage in der $_POST
Anfrage gespeichert ist. Ich könnte natürlich all diese Informationen als $ _GET Anfrage passieren, aber nicht wirklich will, diese Informationen in der URL (?email=&address=&order=
) usw.
Gibt es eine Möglichkeit oder Ratschläge, die Sie sehen ich in der Lage sein die localstorage zuzugreifen oder $_POST
Daten, bevor es auf der RückreiseURL zu PayPal ging?
Unten ist, was derzeit in meinem redirect__paypal.php enthalten ist, um mit der Erklärung zu helfen.
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 {
}
}
Danke für die Antwort. Ich habe das (in payment__paypal.php) versucht, indem ich $ _SESSION ['cartData'] = $ _POST ['cartData']; 'und dann in der returnURL zu Debugging-Zwecken' var_dump ($ _ SESSION ['cartData']); 'aber es gab einfach NULL zurück –
Ist Ihre Sitzung gestartet? tun Sie, zum Beispiel auf Ihrem Index, session_start(); bevor Sie die $ _SESSION-Variablen verwenden? – Olifant1990
Genau das war es :) Danke! –