[英]Subscription failed using Stripe API in Google Apps Script
I'm trying to make an add-on using Google Apps Script
& Stripe
where user can subscribe for an item as an yearly subscription.我正在尝试使用
Google Apps Script
& Stripe
制作一个插件,用户可以在其中订阅一个项目作为年度订阅。 Every time I purchase the subscription from Stripe checkout, I get error like this,每次我从 Stripe checkout 购买订阅时,我都会收到这样的错误,
{
"error": {
"code": "parameter_unknown",
"doc_url": "https://stripe.com/docs/error-codes/parameter-unknown",
"message": "Received unknown parameter: @45b5a607",
"param": "@45b5a607",
"type": "invalid_request_error"
}
}
When I check the log in Stripe Dashboard I get the POST body like this,当我检查 Stripe Dashboard 中的日志时,我得到了这样的 POST 正文,
{
"items": "[Ljava.lang.Object",
"@45b5a607": null,
"customer": "cus_Dix0eSYM5qP0kx"
}
This is my code in Google Apps Script,这是我在 Google Apps Script 中的代码,
var headers = {
"Authorization" : "Basic " + Utilities.base64Encode(USERNAME + ':' + PASSWORD)
};
var customer = {
'email': customerEmail,
'source': token
};
var optCreate = {
'method' : 'post',
"headers" : headers,
'contentType': 'application/x-www-form-urlencoded',
'payload' : customer,
'muteHttpExceptions' : true
};
var createCustomer = UrlFetchApp.fetch(urlCreate, optCreate);
var respCreate = JSON.parse(createCustomer.getContentText());
var customerId = respCreate.id;
if (customerId == null) { return "Error"; }
var data = {
"customer" : customerId,
"items" : [
{
"plan" : "plan_Diuw7CdAGcSrhm"
}
]
};
var options = {
'method' : 'post',
"headers" : headers,
'contentType': 'application/x-www-form-urlencoded',
'payload' : data,
'muteHttpExceptions' : true
};
var response = UrlFetchApp.fetch(url, options);
var resp = JSON.parse(response.getContentText());
Logger.log(resp);
I think I must be doing something wrong in my data
JSON object.我想我的
data
JSON 对象一定是做错了什么。 The items
field is not working correctly that's why POST body is weird. items
字段无法正常工作,这就是 POST body 很奇怪的原因。 What is the correct way here?这里的正确方法是什么?
You need to stringify the payload.您需要对有效负载进行字符串化。
var options = {
'method' : 'post',
"headers" : headers,
'contentType': 'application/x-www-form-urlencoded',
'payload' : JSON.stringify(data),
'muteHttpExceptions' : true
};
It looks like you're POSTing JSON data, but Stripe's API does not accept JSON — you need to use form encoding.看起来您正在发布 JSON 数据,但 Stripe 的 API 不接受 JSON — 您需要使用表单编码。 ie your code needs to set
data
to be in this format:即您的代码需要将
data
设置为这种格式:
items[0][plan]=plan_CvVNfwZ4pYubYg&customer=cus_Diygqj4wAq6L9T
You can refer to cURL examples in Stripe's API docs for this.为此,您可以参考 Stripe 的 API 文档中的cURL 示例。 Generally you should use an official library to simplify making API requests, but that may not be possible with Apps Script.
通常,您应该使用官方库来简化 API 请求,但 Apps Script 可能无法做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.