Paypal PHP REST API


Paypal PHP REST API

我正在使用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/,我能够一直到最后一部分。

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

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

"授权:持有者" .$_GET['token'],将此令牌替换为以前的令牌,方法是将其存储到会话"授权:持有者"中。$json->access_token,这个