【问题标题】:How to link parse customer to Stripe customer如何将解析客户链接到 Stripe 客户
【发布时间】:2015-11-28 05:14:23
【问题描述】:

我正在使用 Stripe 和 Parse 云服务对卡进行收费。充电没问题,但我不知道是哪个用户购买的。我的应用程序要求用户通过 Parse 用户登录才能进行购买。

我想知道 1. 如何使用解析云代码 (JS) 创建 Stripe 客户。 2. 如何将该 Stripe 客户链接到当前登录到我的应用程序的客户(例如来自解析的 [PFUser currentUser])。

现在我有一个vc可以添加信用卡

[[STPAPIClient sharedClient] createTokenWithCard:stripeCard
                                      completion:^(STPToken *token, NSError *error) {
                                          if (error) {
                                              [self handleStripeError:error];
                                          } else {
                                              NSString *myVal = token.tokenId;
                                              NSLog(@"%@",token.tokenId);
                                              [PFCloud callFunctionInBackground:@"createBackendChargeWithToken" withParameters:@{@"token":myVal}
                                                                          block:^(NSString *result, NSError *error) {
                                                                              if (!error) {
                                                                                  NSLog(@"Success: from Cloud Code Res: %@",result);
                                                                                  self.pay.enabled = YES;
                                                                              }
                                                                              else
                                                                              {
                                                                                  NSLog(@"Error: from Cloud Code: %@",error);
                                                                                  self.pay.enabled = YES;
                                                                              }

                                                                          }];
                                                                        }
                                      }];

而我的main.js如下:

Parse.Cloud.define("createBackendChargeWithToken", function(request, response){
var stripeToken = request.params.token;
    var charge = Stripe.Charges.create({
    amount:1000,
    currency:"usd",
    card: stripeToken,
},{
    success: function(httpResponse){
    response.success("Purchase made!");
    },
    error: function(httpResponse){
    response.error("no good");
    }
})
});

【问题讨论】:

    标签: parse-platform stripe-payments


    【解决方案1】:

    首先,在您的云代码中初始化 Stripe 模块:

    var Stripe = require('stripe');
    var STRIPE_SECRET_KEY = '[your secret key]';
    var STRIPE_API_BASE_URL = 'api.stripe.com/v1'; //this is used for making http requests for parts of the Stripe API that aren't covered in parse's stripe module
    Stripe.initialize( STRIPE_SECRET_KEY );
    

    我在客户端创建了一个卡令牌,并将其传递到我的云代码函数中,因此我永远不会发送敏感数据。云代码功能很简单:

    Parse.Cloud.define("StripeRegisterUser", function(request, response)
    {   
        Stripe.Customers.create({
            card: request.params.stripeToken // the token id should be sent from the client
            },{
                success: function(customer) {
                    console.log(customer.id);
                    response.success(customer.id); 
                },
                error: function(httpResponse) {
                    console.log(httpResponse);
                    response.error("Uh oh, something went wrong"+httpResponse);
                }
            });
    });
    

    我实际上存储了 customer.id,我将其作为我的响应传递回我的客户端,传递给用户对象,但这是在我的客户端代码中。您可以将该代码添加到此云函数中。 request.user 将是调用此云代码函数的用户。

    Stripe 模块没有完整的 Stripe API,因此我使用该基本 URL 根据 Stripe 的 API 文档中的 curl 示例创建自定义 http 请求。

    获取用户的卡片数据需要一个 httpRequest,因为 Stripe API 不包含用于此的方法:

    Parse.Cloud.define("StripeUserCards", function(request, response)
    {   
        Parse.Cloud.httpRequest({
            method:"GET",
    
            url: "https://" + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + "/customers/" + request.params.customer_id + "/cards",
    
            success: function(cards) {
                response.success(cards["data"]);
            },
            error: function(httpResponse) {
                response.error('Request failed with response code ' + httpResponse.status);
            }
        });
    });
    

    您必须自己弄清楚如何解析返回的数据。它是 obj-c 中的“id”类型。

    Parse 中的 Stripe.Charges.create 方法需要客户 ID 和卡 ID。在客户端使用该用户卡信息以允许用户选择他们想要使用的卡,将卡 ID 发送到创建费用的方法,并将该卡 ID 与附加到用户对象的客户 ID 一起传递.

    我花了很多钱来帮助我解决这些问题,所以我无法为您提供更具体的帮助。

    【讨论】:

    • 这就是我所拥有的。我从我的应用程序生成一个令牌并将其传递给云功能,但我直接付款,现在我想将其添加给用户。那么我有两个后续问题。首先,Stripe.Customers.create 总是创建一个新客户,但是如果 [PFUser currentUser] 已经创建了怎么办。其次,也许我错过了一些东西,但是如何在 Stripe 中创建一个与 Parse 中的登录用户相关联的客户?我最后的手段是在解析时存储最后四位数字,exp 月/年,因为根据 PCI,它们不敏感。
    • 创建用户会返回您存储在 Parse 用户中的客户 ID。在 Charges.create 方法中,您传入该客户 ID 以及他们使用的卡 ID。由于您有一个已存储的用户,您可以拉取该用户的所有卡片对象,并允许他们选择要使用的添加的卡片,并将客户 ID 和关联的卡片 ID 都传递给收费方法。跨度>
    • 所以我会在我的解析用户上创建一个名为 StripID 或其他内容的字段?你能给我一些关于如何在解析用户上存储信用以及如何检索信用卡信息以将其传递给 Charges.create 的示例代码吗?真的很感激。我将更新问题中的代码供您查看。
    • 更新了我的答案,但我能帮到你的就这么多了。
    猜你喜欢
    • 2019-10-16
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2017-03-13
    • 2021-01-13
    • 1970-01-01
    • 2015-10-24
    • 2019-12-23
    相关资源
    最近更新 更多