无法在上下文中获取 Paypal checkout.js v4 使用 REST API

Trouble getting Paypal in-context checkout.js v4 work with REST API

我是 Paypal 和 PHP 的新手,所以也许我做错了(或没有做错), 我正在尝试让 Paypal in-context checkout v4 使用服务器端工作,以便能够在付款完成后进行操作。我遵循了 Paypal 指南 (https://github.com/paypal/paypal-checkout/blob/master/docs/button.md and https://github.com/paypal/paypal-checkout/blob/master/docs/paypal-rest-api.md) 并进行了大量研究,但 none 目前有用。

我在调用创建付款后遇到问题,Paypal window 不显示 Paypal 登录信息,而是向我显示消息 Return 给商家 目前,我们无法处理您的请求。请 return 尝试其他选项。"

这是我服务器端的代码:

class ExpressCheckout {

    private $request = null;

    function createPayment(){

        $token = $this->getConnectionToken();

        $this->request = curl_init();
        $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : '';

        $data = $this->getDataFromSession();
        $url = 'https://api'.$paypalmode.'.paypal.com/v1/payments/payment';
        $this->setDefaultRequest();
        curl_setopt($this->request, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token",
                                                             'Content-Type: application/json')); 

        curl_setopt($this->request, CURLOPT_POSTFIELDS, $data);
        curl_setopt($this->request, CURLOPT_URL, $url);
        $response = $this->sendHttp($url);
        return '{"paymentID":"'.$response->id.'"}';
    }

    function executePayment(){

        $token = $this->getConnectionToken();

        $entryData = json_decode(file_get_contents('php://input'), true);
        $paymentId = $entryData["paymentID"];
        $payerId = $entryData["payerID"];

        $this->request = curl_init();
        $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : '';

        $data = '{"payer_id":"'.$payerID.'"}';
        $url = 'https://api'.$paypalmode.'.paypal.com/v1/payments/payment/'.$paymentId.'/execute';
        $this->setDefaultRequest();
        curl_setopt($this->request, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token",
                                                   'Content-Type: application/json')); 

        curl_setopt($this->request, CURLOPT_POSTFIELDS, $data);
        curl_setopt($this->request, CURLOPT_URL, $url);
        $response = $this->sendHttp($url);
        return $response;
    }

    function getConnectionToken(){
        $this->request = curl_init();
        $userName = PPL_REST_API_CLIENT_ID;
        $password = PPL_REST_API_SECRET;
        $paypalmode = (PPL_MODE=='sandbox') ? '.sandbox' : '';
        $url = 'https://api'.$paypalmode.'.paypal.com/v1/oauth2/token';
        $data = 'grant_type=client_credentials';

        $this->setDefaultRequest();
        curl_setopt($this->request, CURLOPT_USERPWD, "$userName:$password");
        curl_setopt($this->request, CURLOPT_POSTFIELDS, $data);

        $response = $this->sendHttp($url);
        return $response->access_token;
    }

    private function setDefaultRequest(){
        curl_setopt($this->request, CURLOPT_VERBOSE, 1);
        curl_setopt($this->request, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($this->request, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($this->request, CURLOPT_TIMEOUT, 45);
        curl_setopt($this->request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($this->request, CURLOPT_POST, 1);
    }

    private function sendHttp($url){
        curl_setopt($this->request, CURLOPT_URL, $url);
        $responseJson = json_decode(curl_exec($this->request));
        curl_close ($this->request); 
        return $responseJson;
    }
}

我的客户代码是:

<!DOCTYPE HTML>
<html>
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <script src="https://code.jquery.com/jquery-3.2.0.min.js"></script>
</head>
<body>
    <div id="paypal-button2"></div>
    <script src="https://www.paypalobjects.com/api/checkout.js" data-version-4></script>


    <script>
        paypal.Button.render({
            locale: 'en_US',
            style: {
                size: 'small',
                color: 'gold',
                shape: 'pill',
                label: ''
            },

            payment: function(resolve, reject) {
                jQuery.post('http://dummy_url_site.com/create_payment.php')
                .done(function(data){
                    alert(data);
                    var myJson = JSON.parse(data);
                    alert(myJson.paymentID); 
                    resolve(myJson.paymentID);
                })
                .fail(function(err){reject(err); });
            },

            onAuthorize: function(data, actions) {
                console.log('The payment was authorized!');
                console.log('Payment ID = ',   data.paymentID);
                console.log('PayerID = ', data.payerID);
                // At this point, the payment has been authorized, and you will need to call your back-end to complete the
                // payment. Your back-end should invoke the PayPal Payment Execute api to finalize the transaction.
                /*
                jQuery.post('http://dummy_url_site.com/execute_payment.php', { paymentID: data.paymentID, payerID: data.payerID })
                    .done(function(data) { alert(data.toString()); })
                    .fail(function(err)  {   });
            },
            onCancel: function(data, actions) {
                return actions.redirect();
            },
onError: function(data, actions) {
// Show an error page here. You may try restarting payment execution.
alert('Something went wrong with payment approval' Data: ' + data.toSource());
return actions.restart();
            }
        }, '#paypal-button2');
    </script>
</body>

</html>

如果您要调用沙箱来设置付款,您可能需要在 paypal.Button.render() 中传递 env: 'sandbox'