为了进一步接受答案,我无法实现我想要的 - 似乎无法通过 API 触发“自动计费”。您需要创建一个计费计划,然后根据该计划创建一个协议,然后执行该协议。
根据安德鲁的回答,首先我得到了一个访问令牌。在我的 cURLless 环境中,我这样做了:
//sandbox
$clientid = "AbFnq7P52jf6AUWpu.....";
$secret = "ECfJRJBDnA26VVNAq.....";
$url = 'https://api.sandbox.paypal.com/v1/oauth2/token';
$auth = base64_encode($clientid . ":" . $secret);
$data = array(
'grant_type' => 'client_credentials'
);
$options = array(
'http' => array(
'header' => "Authorization: Basic {$auth}\r\nAccept: application/json\r\nAccept-Language: en_US\r\n",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$arr = json_decode($result, true);
echo "<pre>"; print_r($arr); echo "</pre>";
$accesstoken = $arr['access_token'];
然后按照“集成步骤”https://developer.paypal.com/docs/integration/direct/billing-plans-and-agreements/#integration-steps
我像这样进行了 cURLless API 调用(创建计划示例):
$url = 'https://api.sandbox.paypal.com/v1/payments/billing-plans/';
$options = array(
'http' => array(
'header' => "Authorization: Bearer {$accesstoken}\r\nContent-Type: application/json\r\n",
'method' => 'POST',
'content' => '{
"name": "Test Plan",
"description": "My first test plan",
"type": "INFINITE",
"payment_definitions": [
{
"name": "Regular payment definition",
"type": "REGULAR",
"type":"REGULAR",
"frequency":"Month",
"amount":{
"currency":"AUD",
"value":"100.00"
},
"cycles":"0",
"charge_models":[
{
"type":"TAX",
"amount":{
"currency":"AUD",
"value":"0.00"
}
},
{
"type":"SHIPPING",
"amount":{
"currency":"AUD",
"value":"0.00"
}
}
],
"frequency_interval":"1"
}
],
"merchant_preferences": {
"setup_fee": {
"value": "0",
"currency": "AUD"
},
"return_url": "http://www.paypal.com",
"cancel_url": "http://www.paypal.com/cancel",
"auto_bill_amount": "YES",
"initial_fail_amount_action": "CONTINUE",
"max_fail_attempts": "0"
}
}'
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
$arr = json_decode($result, true);
echo "<pre>"; print_r($arr); echo "</pre>";
希望这些 sn-ps 可以节省我花费的时间!