PHP PayPal 服务器端 REST API



我正在使用PayPal rest API,但我无法让它工作。

我已经复制了他们为创建付款提供的代码。 当我运行它并回显响应时,响应似乎还可以。 科德是 :

            <?php
            // 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
            require __DIR__  . '/PayPal-PHP-SDK/autoload.php';
            use PayPalApiAmount;
            use PayPalApiDetails;
            use PayPalApiItem;
            use PayPalApiItemList;
            use PayPalApiPayer;
            use PayPalApiPayment;
            use PayPalApiRedirectUrls;
            use PayPalApiTransaction;
            // 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret
            // h**ps://developer.paypal.com/webapps/developer/applications/myapps
            $apiContext = new PayPalRestApiContext(
                new PayPalAuthOAuthTokenCredential(
                    'ClientID IS HERE AND IS CORRECT',     // ClientID
                    'Secret IS HERE AND IS CORRECT'      // ClientSecret
                )
            );
            // 3. Lets try to create a Payment
            // h**ps://developer.paypal.com/docs/api/payments/#payment_create
            $payer = new PayPalApiPayer();
            $payer->setPaymentMethod('paypal');
            /*$amount = new PayPalApiAmount();
            $amount->setTotal('1.00');
            $amount->setCurrency('USD');*/

            $item1 = new Item();
            $item1->setName('Ground Coffee 40 oz')
                ->setCurrency('USD')
                ->setQuantity(1)
                ->setPrice(7.5);
            $item2 = new Item();
            $item2->setName('Granola bars')
                ->setCurrency('USD')
                ->setQuantity(5)
                ->setPrice(2);
            $itemList = new ItemList();
            $itemList->setItems(array($item1, $item2));
            //Additional payment details
            //Use this optional field to set additional payment information such as tax, shipping charges etc.
            $details = new Details();
            $details->setShipping(1.2)
                ->setTax(1.3)
                ->setSubtotal(17.50);
            //Amount
            //Lets you specify a payment amount. You can also specify additional details such as shipping, tax.
            $amount = new Amount();
            $amount->setCurrency("USD")
                ->setTotal(20)
                ->setDetails($details);
            $transaction = new PayPalApiTransaction();
            $transaction->setAmount($amount);
            $redirectUrls = new PayPalApiRedirectUrls();
            $redirectUrls->setReturnUrl("h**ps://example.com/your_redirect_url.html")
                ->setCancelUrl("h**ps://example.com/your_cancel_url.html");
            $payment = new PayPalApiPayment();
            $payment->setIntent('sale')
                ->setPayer($payer)
                ->setTransactions(array($transaction))
                ->setRedirectUrls($redirectUrls);
            // 4. Make a Create Call and print the values
            try {
                $payment->create($apiContext);
              //  echo "nnRedirect user to approval_url: " . $payment->getApprovalLink() . "n";
            }
            catch (PayPalExceptionPayPalConnectionException $ex) {
                // This will print the detailed information on the exception.
                //REALLY HELPFUL FOR DEBUGGING
                echo $ex->getData();
            }
            return($payment);
            ?>

我认为这行得通

接下来,我使用他们的示例代码创建了按钮脚本:

            <!DOCTYPE html>
            <head>
                <meta h**p-equiv="X-UA-Compatible" content="IE=edge" />
                <meta name="viewport" content="width=device-width, initial-scale=1">
                <script src="h**ps://www.paypalobjects.com/api/checkout.js"></script>
            </head>
            <body>
                <div id="paypal-button-container"></div>
                <script>
                    paypal.Button.render({
                        env: 'sandbox', // sandbox | production
                        commit: true,
                        payment: function() {
                            // Set up a url on your server to create the payment
                            var CREATE_URL = 'h**ps://MySite.com/PayPalRest/first.php';
                            // Make a call to your server to set up the payment
                            console.log(paypal.request.post(CREATE_URL));
                            return paypal.request.post(CREATE_URL)
                                .then(function(res) {
                                    return res.paymentID;
                                });
                        },
                        // onAuthorize() is called when the buyer approves the payment
                        onAuthorize: function(data, actions) {
                            // Set up a url on your server to execute the payment
                            var EXECUTE_URL = '/demo/checkout/api/paypal/payment/execute/';
                            // Set up the data you need to pass to your server
                            var data = {
                                paymentID: data.paymentID,
                                payerID: data.payerID
                            };
                            // Make a call to your server to execute the payment
                            return paypal.request.post(EXECUTE_URL, data)
                                .then(function (res) {
                                    window.alert('Payment Complete!');
                                });
                        }
                    }, '#paypal-button-container');
                </script>
            </body>

当我运行(单击按钮(时,灯箱弹出几秒钟,然后消失。 该人没有机会验证其帐户并付款。

我已经通读了所有 sdk,可以让他们的样本正常工作。 我认为返回的付款代码一定是错误的或其他什么 - 任何如何调试的帮助建议都很棒。

对不起,长帖子。

问题可能出在多个地方。

你能先检查浏览器开发者日志,看看是否有任何错误吗?

如果您收到类似 - 无法加载 https://www.paypal.com/webapps/hermes/api/logger:对预检请求的响应未通过访问控制检查:"访问控制允许源"标头包含无效值"null//null"。因此,不允许访问源"空"。

然后尝试运行服务器的页面,看看是否有帮助?

最新更新