在PayPal API的returnURL上如何访问$_POST数据

3
我已经使用localStorage构建了一个购物车,并且正在使用PayPal PHP SDK来处理付款。当用户选择通过PayPal支付时,我会通过AJAX将localStorage数据(购物车)和表单数据(用户详细信息)发布到PHP页面。在该页面上,我设置了PayPal API并获取POST数据以创建项目、交易、付款和重定向URL等内容。如果成功,它将返回批准的URL以重定向到PayPal(使用window.location.href),所有这些功能都有效。请注意,HTML标签需要保留。
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 {



    }

}
1个回答

2

在重定向到PayPal之前,我认为您需要将数据保存在某个地方。

在重定向时,所有$_POST字段都会丢失。

最简单的方法是将所有数据保存在您的会话中($_SESSION)。 当您从PayPal返回时,您可以从那里获取它 ;)


1
谢谢回复。我已经尝试了这个(在payment__paypal.php中)设置$_SESSION['cartData'] = $_POST['cartData'];,然后在returnURL上,为了调试目的,使用var_dump($_SESSION['cartData']);但它只是返回了NULL。 - John the Painter
2
你的会话是否已经开始? 比如在使用 $_SESSION 变量之前,你是否在你的索引页上使用了 session_start(); ? - Olifant1990

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接