使用 Parse Cloud Code 创建 Stripe 客户 - Android

Creating Stripe customer with Parse Cloud Code - Android

我正在尝试通过 Parse Cloud Code 函数向 Stripe 发送所需信息来创建新的 Stripe 客户。我的方法如下:

Android:

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.");
        }
    });
});

当我执行此操作时,它会很好地调用 Cloud Code,但会触发错误响应 "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());
            }
        }
    });
}

它returns这个错误:

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 Ran cloud function createCustomer for user 1xjfnmg0GN with: Input: {"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}"} Result: Cannot create a new customer. 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);

解决方法:以不同方式存储token对象


错误 2

条纹 API expects certain parameters and will throw an invalid_request_error when your request has invalid parameters

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

你有无效参数的原因是因为你的 Java 代码将 "source" 键放入 param 映射(与上面的代码相同),但是 Java脚本需要此代码中的 "token"

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

解决方案:要么将Java中的"source"键重命名为"token",要么将Java脚本中的值重命名为request.params.tokenrequest.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());