【发布时间】:2016-04-17 07:22:09
【问题描述】:
我正在尝试通过 Parse Cloud Code 函数向 Stripe 发送所需信息来创建新的 Stripe 客户。我的方法如下:
安卓:
private void createCustomer(Token token) {
String token3 = token.getId();
Map<String, Object> params = new HashMap<>();
params.put("email", username);
params.put("source", token3);
params.put("name", firstName);
params.put("objectId", parseID);
params.put("description", "myExample customer");
Log.e("createCustomer method", "about to call cloud code function with " + token.getId());
ParseCloud.callFunctionInBackground("createCustomer", params, new FunctionCallback<Object>() {
@Override
public void done(Object object, ParseException e) {
if (e == null) {
Toast.makeText(SignUpActivity.this, object.toString(), Toast.LENGTH_LONG).show();
} else {
Log.e("createCustomer method", e.toString());
}
}
});
}
以及我的方法调用的云代码:
var Stripe = require('stripe');
Stripe.initialize(STRIPE_SECRET_KEY);
Parse.Cloud.define("createCustomer", function(request, response) {
Stripe.Customers.create({
card: request.params.token,
description: request.params.description,
metadata: {
name: request.params.name,
userId: request.params.objectId, // e.g PFUser object ID
}
}, {
success: function(httpResponse) {
response.success(customerId); // return customerId
},
error: function(httpResponse) {
console.log(httpResponse);
response.error("Cannot create a new customer.");
}
});
});
当我这样做时,它调用云代码就好了,但它会触发错误响应“无法创建新客户”。
如果我尝试直接发送令牌(而不是将 ID 值作为字符串获取)并以这种方式发送,如下所示:
private void createCustomer(Token token) {
//String token3 = token.getId();
Map<String, Object> params = new HashMap<>();
params.put("email", username);
params.put("source", token);
params.put("name", firstName);
params.put("objectId", parseID);
params.put("description", "myExample customer");
Log.e("createCustomer method", "about to call cloud code function with " + token.getId());
ParseCloud.callFunctionInBackground("createCustomer", params, new FunctionCallback<Object>() {
@Override
public void done(Object object, ParseException e) {
if (e == null) {
Toast.makeText(SignUpActivity.this, object.toString(), Toast.LENGTH_LONG).show();
} else {
Log.e("createCustomer method", e.toString());
}
}
});
}
它返回此错误:
01-12 07:31:27.999 16953-16953/com.stripetestapp.main E/createCustomerMethod: com.parse.ParseException: java.lang.IllegalArgumentException: invalid type for ParseObject: class com.stripe.android.model.Token
因此,从上述错误中,我了解到发送纯令牌会产生错误,但是如果我在其位置发送令牌 ID,它也会触发错误(尽管是不同的错误)。我不禁认为我在这里遗漏了一些明显的东西。
编辑:我尝试将令牌转换为字符串,如下所示:
String token3 = token.toString();
Map<String, Object> params = new HashMap<>();
params.put("source", token3);
它仍然以错误响应“无法创建新客户”进行响应。
EDIT2:云代码方法createCustomer的console.log:
E2016-01-12T21:09:54.487Z]v13 为用户 1xjfnmg0GN 运行云函数 createCustomer: 输入:{"description":"myExample customer","email":"cjfj@ncjf.com","name":"hff","objectId":"1xjfnmg0GN","token":"\u003ccom.stripe。 android.model.Token@1107376352 id=\u003e JSON: {\n \"card\": {\n \"address_city\": null,\n \"address_country\": null,\n \"address_line1\" : null,\n \"address_line2\": null,\n \"address_state\": null,\n \"address_zip\": null,\n \"country\": \"US\",\n \ "cvc\": null,\n \"exp_month\": 2,\n \"exp_year\": 2019,\n \"fingerprint\": null,\n \"last4\": \"4242\" ,\n \"name\": null,\n \"number\": null,\n \"type\": null\n },\n \"created\": \"Jan 12, 2016 1: 09:53 PM\",\n \"id\": \"tok_17SZOnJQMWHHKlPAwdveiUde\",\n \"livemode\": false,\n \"used\": false\n}"} 结果:无法创建新客户。 I2016-01-12T21:09:55.274Z]{"name":"invalid_request_error"}
EDIT3: 建议将“source”更改为“token”并发送 tokenId 而不是 token.toString,这很有效。我确实必须更改我的云代码中的另一行,更改:
success: function(httpResponse) {
response.success(customerId); // return customerId
到
success: function(httpResponse) {
response.success(httpResponse); // return customerId
它完全按照要求工作。
【问题讨论】:
-
params.put("source", token);=> 尝试将 Token 转换为字符串。 Parse 不知道如何接受Token对象 -
我根据您的建议编辑了我的帖子
-
这是您的错误响应,而不是实际响应。
console.log(httpResponse);在哪里显示该 Stripe 方法? -
我已将其添加到我的帖子中,我看到一个 {"name:invalid_request_error"}?
-
根据文档-“当您的请求具有无效参数时会出现无效请求错误”,因此您的卡片、描述和元数据的参数不正确。
标签: android stripe-payments parse-cloud-code