我創建一個PayPal支付用PHP,這是到目前爲止的代碼:寶PHP REST API
$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。所以我假設一切工作正常。接下來的部分是我無法弄清楚的。我正在嘗試在這裏執行付款。
我是否使用從上述以前代碼收到的付款網址?
我試着這樣做:
$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返回網址中給出的令牌。我嘗試使用原始的令牌,併購買了。貝寶返回您的網站時,令牌是否有目的? – Marquisk2