我正在整合 PayPal 订阅。由于我有一个用户定义的计划,我必须相应地创建一个计划,我在 getplan.php 中使用以下 curl 请求创建了该计划:
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api-m.sandbox.paypal.com/v1/billing/plans",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\r\n \"product_id\": \"PROD-65T41140NK6579847\",\r\n \"name\": \"Video Streaming Service Plan\",\r\n \"description\": \"Video Streaming Service basic plan\",\r\n \"status\": \"ACTIVE\",\r\n \"billing_cycles\": [\r\n {\r\n \"frequency\": {\r\n \"interval_unit\": \"MONTH\",\r\n \"interval_count\": 1\r\n },\r\n \"tenure_type\": \"TRIAL\",\r\n \"sequence\": 1,\r\n \"total_cycles\": 2,\r\n \"pricing_scheme\": {\r\n \"fixed_price\": {\r\n \"value\": \"3\",\r\n \"currency_code\": \"USD\"\r\n }\r\n }\r\n },\r\n {\r\n \"frequency\": {\r\n \"interval_unit\": \"MONTH\",\r\n \"interval_count\": 1\r\n },\r\n \"tenure_type\": \"TRIAL\",\r\n \"sequence\": 2,\r\n \"total_cycles\": 3,\r\n \"pricing_scheme\": {\r\n \"fixed_price\": {\r\n \"value\": \"6\",\r\n \"currency_code\": \"USD\"\r\n }\r\n }\r\n },\r\n {\r\n \"frequency\": {\r\n \"interval_unit\": \"MONTH\",\r\n \"interval_count\": 1\r\n },\r\n \"tenure_type\": \"REGULAR\",\r\n \"sequence\": 3,\r\n \"total_cycles\": 12,\r\n \"pricing_scheme\": {\r\n \"fixed_price\": {\r\n \"value\": \"10\",\r\n \"currency_code\": \"USD\"\r\n }\r\n }\r\n }\r\n ],\r\n \"payment_preferences\": {\r\n \"auto_bill_outstanding\": true,\r\n \"setup_fee\": {\r\n \"value\": \"10\",\r\n \"currency_code\": \"USD\"\r\n },\r\n \"setup_fee_failure_action\": \"CONTINUE\",\r\n \"payment_failure_threshold\": 3\r\n },\r\n \"taxes\": {\r\n \"percentage\": \"10\",\r\n \"inclusive\": false\r\n }\r\n}",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer A21AAJd7f6oo6_Inozxc4JfgLaPvnlU_-3kYw_8JdRLnjsJ-aiRe_ZgRaZWBnwd4Oh8WZvwBbvyI0QRHGY3pRVbWGgnV8kY1g",
"cache-control: no-cache",
"content-type: application/json",
"postman-token: 4ed388a2-e0eb-39f2-a624-51291f549960",
"prefer: return=representation"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
echo $response;
我已通过上述 curl 请求成功接收到计划 ID。
<script src="https://www.paypal.com/sdk/js?client-id=<client-id>¤cy=USD&intent=subscription&commit=false&vault=true&disable-funding=credit,card"></script>
<script>
paypal.Buttons({
createSubscription: function() {
return fetch('getplan.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
}).then(function(res) {
return res.json();
}).then(function(data,actions) {
return actions.subscription.create({
'plan_id': data.id
});
});
},
onApprove: function(data, actions) {
alert('You have successfully created subscription ' + data.subscriptionID);
}
}).render('#paypal-button');
</script>
<div id="paypal-button"></div>
但控制台给出错误“无法读取未定义的属性‘订阅’”。如果我在创建订阅函数时手动输入 plan_id(我创建的),它会给我错误:无法读取未定义的属性“订阅”。
请帮忙!
答案 0 :(得分:0)
好的,发现你的问题了。 actions
未定义,因为您在错误的位置将其作为参数。
createSubscription: function(data, actions) {
return fetch('getplan.php', {
method: 'post',
headers: {
'content-type': 'application/json'
},
}).then(function(res) {
return res.json();
}).then(function(serverData) {
return actions.subscription.create({
'plan_id': serverData.id
});
});
},
这是一种奇怪的编码模式——如果你在 createSubscription 中对你的服务器进行提取,你也可以创建订阅服务器端(getsubscription.php 或其他)并返回订阅 ID 而不是而不是计划 ID。