簡體   English   中英

PHP PayPal服務器端REST API

[英]PHP PayPal Server side 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 PayPal\Api\Amount;
            use PayPal\Api\Details;
            use PayPal\Api\Item;
            use PayPal\Api\ItemList;
            use PayPal\Api\Payer;
            use PayPal\Api\Payment;
            use PayPal\Api\RedirectUrls;
            use PayPal\Api\Transaction;
            // 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 \PayPal\Rest\ApiContext(
                new \PayPal\Auth\OAuthTokenCredential(
                    '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 \PayPal\Api\Payer();
            $payer->setPaymentMethod('paypal');

            /*$amount = new \PayPal\Api\Amount();
            $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 \PayPal\Api\Transaction();
            $transaction->setAmount($amount);

            $redirectUrls = new \PayPal\Api\RedirectUrls();
            $redirectUrls->setReturnUrl("h**ps://example.com/your_redirect_url.html")
                ->setCancelUrl("h**ps://example.com/your_cancel_url.html");

            $payment = new \PayPal\Api\Payment();
            $payment->setIntent('sale')
                ->setPayer($payer)
                ->setTransactions(array($transaction))
                ->setRedirectUrls($redirectUrls);

            // 4. Make a Create Call and print the values
            try {
                $payment->create($apiContext);

              //  echo "\n\nRedirect user to approval_url: " . $payment->getApprovalLink() . "\n";
            }
            catch (\PayPal\Exception\PayPalConnectionException $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 :對預檢請求的響應未通過訪問控制檢查:“ Access-Control-Allow-Origin”標頭包含無效值'null // null'。 因此,不允許訪問原始“空”。

然后嘗試運行服務器頁面,看看是否有幫助?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM