PayPal按钮和Webhook集成



我有一个Paypal按钮设置与以下代码:

paypal.Buttons({
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-PLANID'
});
},
onApprove: function(data, actions) {
// Somehow pass info to Webhook
}
}).render('#paypal-button-container');

是否有办法将此与我与BILLING.SUBSCRIPTION.ACTIVATED设置的webhook集成?

我刚刚创建了webhook,但我不确定如何将我的PayPal按钮连接到它

你需要使用与按钮相同的app/client-id来订阅webhook事件。您可以在Developer Dashboard中手动订阅,也可以通过API调用订阅。这是文档


你代码中的注释没有意义:

// Somehow pass info to Webhook

订阅webhook事件通知是一个一次性的初始设置,你可以在其他地方做。它不是你从按钮上做的事情,它不会以任何方式调用或与按钮交互;webhook稍后异步发送和处理。

如果你想存储一些信息作为订阅的一部分,以便以后协调(例如,与订阅的用户,所以你的后端知道当它收到一个webhook时该怎么做),那么你可以设置一个custom_id作为订阅创建的一部分。

我的例子本身并不是一个webhook,但它对我来说效果很好。我在onApprove函数中调用Firebase云函数,如下所示:

paypal.Buttons({
createSubscription: function(data, actions) {
return actions.subscription.create({
'plan_id': 'P-PLANID'
});
},
onApprove: function(data, actions) {
const orderID = data.orderID;
let url = "myCloudFunction?orderID="+orderID;
//this will verify the purchase and complete order on back end
fetch(url);
//carry on with user experince
}
}).render('#paypal-button-container');

然后在我的云功能中使用orderID验证购买,并更新我的DB或其他任何我需要这样做的:

const checkoutNodeJssdk = require('@paypal/checkout-server-sdk');
const functions = require("firebase-functions");
function environment() {
let clientId = process.env.PAYPAL_CLIENT_ID || 'yourClientID';
let clientSecret = process.env.PAYPAL_CLIENT_SECRET || 'yourClientSecret';
return new checkoutNodeJssdk.core.LiveEnvironment(
clientId, clientSecret
);
}
function payPalClient() {
return new checkoutNodeJssdk.core.PayPalHttpClient(environment());
}
exports.completeOrder = functions.https.onRequest((req, res) => {
const orderID = req.query.orderID;
let request = new checkoutNodeJssdk.orders.OrdersGetRequest(orderID);
let order;
try {
order = await payPalClient().execute(request);
console.log(order);
} catch (err) {
console.error(err);
res.send({response: "failed"});
}
if (order.result.status === 'APPROVED') {
//update DB or whatever else needs to happen to complete order
res.send({response: "success"});
} else {
res.send({response: "failed"});
}
});

还可以通过将按钮的onApprove函数中返回的数据对象中的subscriptionID传递给云函数来验证订阅是否活动。使用以下命令验证订阅:

async function verifySubscription(subscriptionID, callback) {   
const authUrl = "https://api-m.paypal.com/v1/oauth2/token";
const subscriptionsUrl = "https://api.paypal.com/v1/billing/subscriptions/" + subscriptionID;
const clientIdAndSecret = "myClientID:myCLientSecret";
const base64 = Buffer.from(clientIdAndSecret).toString('base64')
fetch(authUrl, { 
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Accept-Language': 'en_US',
'Authorization': `Basic ${base64}`,
},
body: 'grant_type=client_credentials'
}).then(function(response) {
return response.json();
}).then(function(data) {
fetch(subscriptionsUrl, { 
method: 'get', 
headers: {
'Authorization': 'Bearer '+data.access_token, 
'Content-Type': 'application/json'
}, 
}).then(function(response) {
return response.json();
}).then(function(subscriptionData) {
console.log("subscriptionData.status: ", subscriptionData.status);
if (subscriptionData.status === "ACTIVE") {
callback(true);
} else {
callback(false);
}
}).catch(function(error) {
console.log("couldnt verify subscription");
console.log(error);
callback(false);
});
}).catch(function() {
console.log("couldnt get auth token");
callback(false);
});
}

最新更新