创建Stripe客户与解析云代码-安卓系统



我正试图通过解析云代码功能向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);

并且它仍然以错误响应"Cannot create a new customer"进行响应。

EDIT2:云代码方法的console.log createCustomer:

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@1107376352id=\u003e JSON:{\n\"card\":{\ n\"address_city\":null,"address_cocountry\":null,"address_line1\":null、"address_line 2\":null、"address_state\":null、"address_zip\":空、"country\":\"US\"、"cvc\":null、"exp_month\":2、"exp_year\":2019、"fingerprint":null,\"last4\":\"4242\",\"name \":null,\n \"number \":null,"类型":null\n},"创建":"2016年1月12日下午1:09:53","id":"tok_17ZOnJQMWHHKlPAwdveiUde","livemode":false,"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

它完全按照要求工作。

错误1

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);

解决方案:以不同方式存储令牌对象


错误2

Stripe API需要某些参数,并且当您的请求具有无效参数时将抛出invalid_request_error

Result: Cannot create a new customer.
{"name":"invalid_request_error"}`

您有无效参数的原因是,您的Java代码正在将"source"密钥放入param映射中(与上面的代码相同),但JavaScript在该代码中期望"token"密钥

Stripe.Customers.create({
    card: request.params.token,

解决方案:将Java中的"source"键重命名为"token",或将JavaScript中的值从request.params.token重命名为request.params.source


解决方案组合

一旦修复了错误2,您仍然需要解决错误1。正如我在上面的评论中所建议的,您应该只将令牌的ID存储在Parse中。当您需要Stripe客户对象时,请使用ID向Stripe API查询该对象。否则,您将复制数据。

要做到这一点,如果您在Java中将"source"重命名为"token",您可以执行以下

private void createCustomer(Token token) {
    Map<String, Object> params = new HashMap<>();
    params.put("token", token.getId());

最新更新