我正在尝试通过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());
}
}
});
}
我的方法调用的Cloud Code:
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.");
}
});
});
&#13;
当我这样做时,它会调用Cloud Code,但它会触发错误响应&#34;无法创建新客户&#34;。
如果我尝试直接发送令牌(而不是将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);
它仍然会回复错误响应&#34;无法创建新客户&#34;。
EDIT2:云代码方法的console.log createCustomer:
E2016-01-12T21:09:54.487Z] v13用户1xjfnmg0GN的云功能createCustomer: 输入:{&#34;说明&#34;:&#34; myExample客户&#34;,&#34;电子邮件&#34;:&#34; cjfj@ncjf.com",&#34;名称&# 34;:&#34; HFF&#34;&#34;的ObjectID&#34;:&#34; 1xjfnmg0GN&#34;&#34;令牌&#34;:&#34; \ u003ccom.stripe.android。 model.Token@1107376352 id = \ u003e JSON:{\ n \&#34; card \&#34;:{\ n \&#34; address_city \&#34;:null,\ n \&#34; address_country \&#34;:null,\ n \&#34; address_line1 \&#34;:null,\ n \&#34; address_line2 \&#34;:null,\ n \&#34; address_state \ &#34;:null,\ n \&#34; address_zip \&#34;:null,\ n \&#34; country \&#34;:\&#34; US \&#34;,\ n \&#34; cvc \&#34;:null,\ n \&#34; exp_month \&#34;:2,\ n \&#34; exp_year \&#34;:2019,\ n \ &#34;指纹\&#34;:null,\ n \&#34; last4 \&#34;:\&#34; 4242 \&#34;,\ n \&#34;名称\&# 34;:null,\ n \&#34; number \&#34;:null,\ n \&#34;输入\&#34;:null \ n},\ n \&#34; created \& #34;:\&#34; 2016年1月12日下午1:09:53 \&#34;,\ n \&#34; id \&#34;:\&#34; tok_17SZOnJQMWHHKlPAwdveiUde \&#34; ,\ n \&#34; livemode \&#34;:false,\ n \&#34;使用\&#34;:false \ n}&#34;} 结果:无法创建新客户。 I2016-01-12T21:09:55.274Z] {&#34;名称&#34;:&#34; invalid_request_error&#34;}
EDIT3:有人建议更改来源&#39;到#令牌&#39;并发送tokenId而不是token.toString,这是有效的。我确实需要更改我的云代码中的另一行,更改:
success: function(httpResponse) {
response.success(customerId); // return customerId
到
success: function(httpResponse) {
response.success(httpResponse); // return customerId
它完全符合要求。
答案 0 :(得分:2)
Parse只知道如何保存某些Java数据类型(String,int,boolean等),所以这个错误信息
com.parse.ParseException: java.lang.IllegalArgumentException: invalid type for ParseObject: class com.stripe.android.model.Token
指的是这段代码
private void createCustomer(Token token) {
Map<String, Object> params = new HashMap<>();
params.put("source", token);
解决方案:以不同方式存储令牌对象
Stripe API expects certain parameters并在your request has invalid parameters时抛出invalid_request_error
。
Result: Cannot create a new customer.
{"name":"invalid_request_error"}`
你有无效参数的原因是因为你的Java代码将"source"
键放入param
映射(与上面相同的代码),但是JavaScript期望{{1}的密钥在此代码中
"token"
解决方案:将Java中的Stripe.Customers.create({
card: request.params.token,
键重命名为"source"
,或将JavaScript中的值重命名为"token"
至request.params.token
。
修复错误2后,您仍然需要解决错误1.正如我在上面的评论中所建议的那样,您应该只在Parse中存储令牌的ID。当您需要Stripe客户对象时,使用ID查询Stripe API。否则,您正在复制数据。
为此,如果您在Java中将request.params.source
重命名为"source"
,则可以执行此操作
"token"