【问题标题】:Creating Stripe customer with Parse Cloud Code - Android使用 Parse Cloud Code 创建 Stripe 客户 - Android
【发布时间】: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


【解决方案1】:

错误 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 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 期望在此代码中使用 "token"

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

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


解决方案组合

一旦错误 2 被修复,您仍然需要解决错误 1。正如我在上面的 cmets 中建议的,您应该只将 Token 的 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());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-12
    • 2015-12-08
    • 2017-02-01
    • 2016-01-23
    • 2023-01-18
    • 2018-01-01
    • 2015-12-26
    • 2020-09-30
    相关资源
    最近更新 更多