【发布时间】:2021-05-05 06:18:37
【问题描述】:
我是使用 Paypal Smart 按钮设置订阅的新手。我创建了一个从 Paypal 帐户订阅的 Paypal 智能按钮,并将其粘贴到我的 html 中。已添加我的 OnInit 和 Onclick 函数以在用户点击之前进行验证。
在用户浏览“同意并订阅”页面后,我正在检查订阅是否处于活动状态。 (这是在“notify.php”中完成的,我使用 paypal REST api 调用并获取订阅状态)
<div id="paypal-button-container"></div>
<script src="https://www.paypal.com/sdk/js?client-id=XXXXXXXXXXXXXXXX&vault=true&intent=subscription" data-sdk-integration-source="button-factory"></script>
<script>
paypal.Buttons({
style: {
shape: 'pill',
color: 'gold',
layout: 'vertical',
label: 'subscribe'
},
// onInit is called when the button first renders
onInit: function(data, actions) {
// Disable the buttons
actions.disable();
// Listen for changes to the radio
var items = document.getElementsByName("settings");
[].forEach.call(items, function (item) {
item.addEventListener("change", function () {
// Enable or disable the button when it is checked or unchecked
if (event.target.checked) {
actions.enable();
} else {
actions.disable();
}
});
});
},
// onClick is called when the button is clicked
onClick: function() {
var radios=document.querySelectorAll('input[type="radio"]:checked').length;
// Show a validation error if the checkbox is not checked
if (radios == 0) {
document.querySelector('#error').classList.remove('hidden');
}
},
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-XXXXXXXXXXXX'
});
},
onApprove: function(data, actions) {
alert(data.subscriptionID);
alert(data.SubscriptionDate);
// You must return a promise from onClick to do async validation
return fetch("notify.php", {
method: 'post',
headers: {
'content-type': 'application/json'
},
body:JSON.stringify({
subscriptionID:data.subscriptionID
})
}).then(response => {
return response.text();
})
.then(data => {
console.log(data);
if(data == "ACTIVE"){
location.href = "validate.html?data=ACTIVE";
}
})
.catch(error => {console.log(error)
});
}
}).render('#paypal-button-container');
</script>
</div>
第一季度。当订阅状态返回“ACTIVE”时,请有人确认一下,这意味着用户已付款并已成功订阅。因此我可以在我的数据库中更新这些信息。 或者我是否需要在 OnApprove 中捕获交易,如下所示:
onApprove: function (data, actions) {
// Capture the transaction funds
return actions.order.capture().then(function () {
第二季度。这种在我的网页上集成按钮的方式是否安全?
更新: (所有这些都是针对我为客户提供服务的个人网页。我想为他们激活订阅。) 我从我的 Paypal 帐户制定了订阅计划。在我的网页上添加了生成的代码,现在我想检查用户/客户是否已成功支付/订阅我的服务,以便我可以为他们启用服务并更新他们在我的数据库中的信息。
【问题讨论】:
-
这能回答你的问题吗? PayPal Smart Subscribe server side
-
@PrestonPHX:不,我已经检查过了。我没有使用 API 创建订阅计划。我只想检查订阅是否处于活动状态,以便我可以在我的数据库中输入详细信息。我想知道贝宝是否激活了订阅,这是否确认用户已付费?
-
计划应事先创建,是否通过API无关紧要。订阅应通过 API 创建,因此您可以从服务器激活它。然后你知道它是活跃的。 Javascript 代码不应触发写入数据库的事件,应与 PayPal API 通信。
-
我将信息(通过 onApprove 收到的订阅 ID)从 JS 传递到 notify.php,在其中我通过 REST 调用订阅 API 并检查订阅状态。如果处于活动状态,那么只有我更新数据库
-
然后订阅可能会在没有通知您的服务器的情况下激活,因为通知来自客户端 JS。正如我所说,您的服务器应该调用 API 来激活订阅。有关详细信息,请参阅上面的链接。
标签: paypal paypal-subscriptions