在使用 Parse.Cloud.httpRequest 获取正确的格式时遇到问题,以便在_period_end 删除订阅。
我能够使用 x-www-form-urlencoded 键“at_period_end”值 true 通过 PostMan 成功发出此请求。 (由于我的声誉抱歉无法发布屏幕截图)
这是我的云代码:
Parse.Cloud.httpRequest({
method : 'DELETE',
url : 'https://' + skey + ':@' + 'api.stripe.com/v1' + '/customers/' + request.params.customerId + '/subscriptions/' + request.params.subscriptionId,
body : {
"at_period_end": true
},
success: function(httpResponse) {
if (httpResponse.status === 200) {
response.success(httpResponse);
}
else {
response.error(httpResponse);
}
},
error: function(httpResponse) {
response.error(httpResponse);
}
});
我试过添加一个设置了 Content-Type 的标题对象,但没有成功。
我认为这只是格式转换问题,从我正确输入到 PostMan 的内容到我的 httpRequest 对象中的内容...
我也找不到关于 httpRequest 方法的文档的任何重要信息,所以它非常令人沮丧:(。
谢谢你
***** 编辑 ****** 解决方案:
设法使用 url 内联参数解决此问题:
var options = request.params.options,
url = 'https://' + skey + ':@api.stripe.com/v1/customers/' + request.params.customerId + '/subscriptions/' + request.params.subscriptionId,
keys;
keys = Object.keys(options);
// This is disgusting, I need to know a better way.
for (var i = 0; i < keys.length; i++)
{
if (i === 0)
{
url += '?';
}
url += keys[i] + '=' + options[keys[i]];
if (i !== keys.length - 1)
{
url += '&';
}
}
Parse.Cloud.httpRequest({
method : 'DELETE',
url : url,
success: function(httpResponse) {
if (httpResponse.status === 200) {
response.success(httpResponse);
}
else {
response.error(httpResponse);
}
},
error: function(httpResponse) {
response.error(httpResponse);
}
});
如果有人能告诉我一个更好的方法来写这个,那将是史诗般的:)
干杯
最佳答案
这个对我来说一直特别棘手,这是我一直在使用的有效方法:
Parse.Cloud.httpRequest({
method: 'DELETE',
url: 'https://api.stripe.com/v1/customers/' + request.params.stripeId + '/subscriptions/' + request.params.stripeSubscriptionId,
headers: {
'Authorization': 'Basic BASE_64_ENCODE_SECRET_KEY'
},
params: {
at_period_end: true
},
success: function(httpResponse) {
...
},
error: function(httpResponse) {
...
}
});
这里有一些额外的细节。
- 我最初将“Content-Type: application/json”作为 header 之一,但这似乎不正确,尽管(我认为)过去需要它。
您的 key 的 base64 编码可以用
生成echo -e 'sk_live_ABCDEF123456:' | openssl base64
不要忘记 key 末尾的冒号 (:),它很重要。
但这只是一个细节,看起来将 key 直接放在 URL 中也能正常工作。
关于http - 解析云 httpRequest strip 订阅 at_period_end 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29910651/