【问题标题】:Paypal PHP REST API贝宝 PHP REST API
【发布时间】:2013-10-04 23:24:23
【问题描述】:

我正在使用 PHP 创建一个 Paypal 结帐,这是目前的代码:

$ch = curl_init();
        $clientId = "clientID";
        $secret = "secret";

        curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");

        $result = curl_exec($ch);

        if(empty($result))die("Error: No response.");
        else
        {
                $json = json_decode($result);
                    //print_r($json->access_token);
                    if ($_POST['creditcard'] == 'paypal') {
                        $data = '{
                            "intent":"sale",
                            "redirect_urls":{
                            "return_url":"http://returnurl.com",
                            "cancel_url":"http://returnurl.com"
                            },
                            "payer":{
                            "payment_method":"paypal"
                            },
                            "transactions":[
                            {
                            "amount":{
                            "total":"' . preg_replace("/[^0-9,.]/", "", $order_price) .'",
                            "currency":"' . $currency_code .'"
                            },
                            "description":"' . ucfirst($type) . ' Logo Purchase. ' . '"
                            }
                            ]
                            }';

                            curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
                            curl_setopt($ch, CURLOPT_POST, true);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                            'Content-Type:application/json',
                            "Authorization: Bearer " . $json->access_token, 
                            "Content-length: ".strlen($data))
                            );
                            print_r($data);
                            $result = curl_exec($ch);

                            if(empty($result))die("Error: No response.");
                            else {
                                $json_output = json_decode($result);
                                print_r($result);

                                if(isset($json_output->{'links'}[2]->{'href'}))
                                    $_SESSION['execute_url'] = $json_output->{'links'}[2]->{'href'};
                                if(isset($json_output->{'links'}[1]->{'href'}))
                                    header('Location:' .$json_output->{'links'}[1]->{'href'} );
                            }

                    }

                }

        curl_close($ch);

我使用我的测试帐户结帐,然后我被重定向到重定向 URL。所以我假设一切正常。现在下一部分是我无法弄清楚的。我正在尝试在这里执行付款。

我是否使用从之前的代码中收到的付款 URL?

我试过这样做:

                            $data = '{ "payer_id" : "' .$_GET['PayerID'] . '" }';
                            curl_setopt($ch, CURLOPT_URL, $stored_execute_url;
                            curl_setopt($ch, CURLOPT_POST, true);
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                            'Content-Type:application/json',
                            "Authorization: Bearer " . $_GET['token'], 
                            "Content-length: ".strlen($data))
                            );
                            print_r($data);
                            $result = curl_exec($ch);

                            if(empty($result))die("Error: No response.");

我得到的只是“错误:无响应”。

我一直关注这个链接https://developer.paypal.com/webapps/developer/docs/integration/web/accept-paypal-payment/,直到最后一部分。

【问题讨论】:

  • 我想通了。我使用的是 PayPal 的返回 URL 中给出的令牌。我尝试使用原始令牌并且购买成功。当贝宝返回您的网站时,令牌是否有用途?

标签: php paypal paypal-sandbox


【解决方案1】:

在对 OAuth2 端点的调用中返回的访问令牌标识调用者和权限(您可以进行哪些 api 调用)。这是任何 API 调用 (REST) 创建付款、执行、退款等所必需的。有些调用需要比其他调用更多的权限,即您需要在创建访问令牌时请求更多信息或提供更多凭据。

URL 中返回的令牌标识付款,这样当用户被重定向回您的网站时,您可以识别哪些付款已被重定向回您的网站并识别正确的信息,即执行正确的付款方式。

【讨论】:

    【解决方案2】:

    “授权:承载”。 $_GET['token'],通过将之前的令牌存储到会话 "Authorization: Bearer" 中来替换这个令牌。 $json->access_token,这个

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2018-04-25
      • 2021-06-23
      • 2016-08-03
      • 2013-10-17
      • 2019-08-08
      • 1970-01-01
      • 2013-04-11
      • 2014-03-28
      相关资源
      最近更新 更多