【发布时间】:2015-07-10 13:49:23
【问题描述】:
我有一个使用 Parse.com 作为后端的应用程序和一个充当我的支付网关的外部站点。从 Stripe 收到客户/订阅 webhook 数据后,我希望查找用户的电子邮件,然后我可以运行云代码功能并将他们的用户状态更改为“付费”
我的 webhook 接收器是:
Parse.Cloud.define("update_user", function(request, response) {
var data = request.params["data"]
var customer = data.object.customer;
response.success'Working' + request);
});
我可以使用以下方式从客户 ID 的条带中收到一封电子邮件:
Parse.Cloud.define("pay", function(request, response) {
Stripe.initialize(STRIPE_SECRET_KEY);
console.log(JSON.stringify(request.params));
Stripe.Customers.retrieve(
customerId, {
success:function(results) {
console.log(results["email"]);
// alert(results["email"]);
response.success(results);
},
error:function(error) {
response.error("Error:" +error);
}
}
);
});
我需要帮助将其转换为一个完整的函数,该函数在收到来自 Stripe 的每个 webhook 时运行。如果无论出于何种原因这不起作用,我也在为后备选项而苦苦挣扎。
编辑
采取第一个答案的部分,我现在有:
Parse.Cloud.define("update_user", function(request, response) {
Stripe.initialize(STRIPE_SECRET_KEY);
var data = request.params["data"]
var customerId = data.object.customer;
get_stripe_customer(customerId, 100).then(function(stripeResponse) {
response.success(stripeResponse);
}, function(error) {
response.error(error);
});
});
function get_stripe_customer (customerId) {
Stripe.initialize(STRIPE_SECRET_KEY);
return Stripe.Customers.retrieve(
customerId, {
success:function(results) {
console.log(results["email"]);
},
error:function(error) {
}
}
);
};
我的知识确实落在了 Promise 方面以及回调(success:,error,requestresponse)等进一步阅读将不胜感激。
现在可以了
【问题讨论】:
-
我认为您对这个问题没有什么吸引力,因为它相当于“我如何编写云函数来更新用户”?这没问题,但它需要:(a) 输入清晰,例如在那一大堆 JSON 中用户识别特征在哪里,(b) 删除那一大堆 JSON,以及 (c) 至少尝试代码——这里的门槛很低,但与 SO 人交谈就像与巴黎人交谈。如果你尝试英语,只是大声一点,你会被冷落,但一个谦虚的,甚至是拙劣的法语尝试通常会得到慷慨的帮助。
-
谢谢...请看我的编辑
-
太棒了。下面根据 parse.com 上描述的使用 Stripe 的流程回答。它假定登录用户是付款的用户。
标签: javascript parse-platform stripe-payments webhooks