PayPal "订阅"按钮:错误:无法读取未定义的属性'subscription'



我正在整合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 => "{rn  "product_id": "PROD-65T41140NK6579847",rn  "name": "Video Streaming Service Plan",rn  "description": "Video Streaming Service basic plan",rn  "status": "ACTIVE",rn  "billing_cycles": [rn    {rn      "frequency": {rn        "interval_unit": "MONTH",rn        "interval_count": 1rn      },rn      "tenure_type": "TRIAL",rn      "sequence": 1,rn      "total_cycles": 2,rn      "pricing_scheme": {rn        "fixed_price": {rn          "value": "3",rn          "currency_code": "USD"rn        }rn      }rn    },rn    {rn      "frequency": {rn        "interval_unit": "MONTH",rn        "interval_count": 1rn      },rn      "tenure_type": "TRIAL",rn      "sequence": 2,rn      "total_cycles": 3,rn      "pricing_scheme": {rn        "fixed_price": {rn          "value": "6",rn          "currency_code": "USD"rn        }rn      }rn    },rn    {rn      "frequency": {rn        "interval_unit": "MONTH",rn        "interval_count": 1rn      },rn      "tenure_type": "REGULAR",rn      "sequence": 3,rn      "total_cycles": 12,rn      "pricing_scheme": {rn        "fixed_price": {rn          "value": "10",rn          "currency_code": "USD"rn        }rn      }rn    }rn  ],rn  "payment_preferences": {rn    "auto_bill_outstanding": true,rn    "setup_fee": {rn      "value": "10",rn      "currency_code": "USD"rn    },rn    "setup_fee_failure_action": "CONTINUE",rn    "payment_failure_threshold": 3rn  },rn  "taxes": {rn    "percentage": "10",rn    "inclusive": falsern  }rn}",
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>&currency=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(我已经创建的),它会给我错误:无法读取未定义的属性'订阅'。

请帮忙!

好的,发现你的问题了。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
});
});
},

这是一种奇怪的编码模式,但是——如果您在createssubscription中对服务器进行读取,您不妨创建订阅服务器端(getsubscription.php或其他)并返回订阅id而不是计划id。

最新更新