【发布时间】:2014-10-15 10:44:06
【问题描述】:
在我的网站中,我希望实施自适应贝宝付款方式。是否可以使用curl实现。任何人都可以提供自适应支付的php代码
【问题讨论】:
在我的网站中,我希望实施自适应贝宝付款方式。是否可以使用curl实现。任何人都可以提供自适应支付的php代码
【问题讨论】:
这是 curl 调用 paypal 的代码
对于自适应支付,您首先需要使用您的“clientId”和“密钥”生成访问令牌。然后使用该访问令牌,您可以调用 Paypal 自适应支付 API。
我已经给出了生成访问令牌的示例。然后从 paykey 获取交易详情(您可以根据自己的要求更改通话)。
//Get access token from client Id
$ch = curl_init();
$clientId = PAYPAL_CLIENT_ID;
$secret = PAYPAL_CLIENT_SECRET;
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/oauth2/token');// Change it to "https://api.paypal.com/v1/oauth2/token" for live
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);
$accessToken = null;
if (empty($result))
return 'Access Token could not generated';
else {
$json = json_decode($result);
$accessToken = $json->access_token;
}
curl_close($ch);
/**
* Get payment transaction details from
* - AccessToken
* - PayKey
*/
$curl = curl_init('https://api.sandbox.paypal.com/v1/payments/payment/' . $input['payKey']);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer ' . $accessToken,
'Accept: application/json',
'Content-Type: application/json'
));
$response = curl_exec($curl);
$result = json_decode($response);
您可以参考 paypal curl 调用的示例 https://github.com/paypal/rest-api-curlsamples/blob/master/execute_all_calls.php
【讨论】:
【讨论】:
//This example for making a payment
//沙盒模式 $apiUrl = 'https://svcs.sandbox.paypal.com/AdaptivePayments/Pay';
//对于直播模式 //$apiUrl = 'https://svcs.paypal.com/AdaptivePayments/Pay';
$_username = "**********";
$_password = "**********";
$_ipaddress = "**********";
$_application_id = "**********";
$returnUrl = "**********"
$cancelUrl = "****************";
$receivers = array(
array(
'email' => "receiver1@abc.com",
'amount' => 50,
'primary' => true
),
array(
'email' => "receiver2@abc.com",
'amount' => 20,
'primary' => false
)
);
$requestEnvelope = array(
'errorLanguage' => 'en_US',
'detailLevel' => 'ReturnAll'
);
$packet = array(
'actionType' => "PAY",
'currencyCode' => "USD",
'feesPayer' => "EACHRECEIVER",
'receiverList' => array(
'receiver' => $receivers
),
'memo' => "Here you can write memo desc",
'returnUrl' => $returnUrl,
'cancelUrl' => $cancelUrl,
'requestEnvelope' => $requestEnvelope
);
$headers = array(
"X-PAYPAL-SECURITY-USERID: " . $_username,
"X-PAYPAL-SECURITY-PASSWORD: " . $_password,
"X-PAYPAL-SECURITY-SIGNATURE: " . $_signature,
"X-PAYPAL-DEVICE-IPADDRESS: ".$_ipaddress,
"X-PAYPAL-REQUEST-DATA-FORMAT: JSON",
"X-PAYPAL-RESPONSE-DATA-FORMAT: JSON",
"X-PAYPAL-APPLICATION-ID: " . $_application_id);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl . $call);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($packet));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
print_r( json_decode(curl_exec($ch), TRUE));
//After execution of request , you got a paykey and using that key you can receive payment from Buyer.
【讨论】: