如何 link 将客户解析为 Stripe 客户

How to link parse customer to Stripe customer

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

我想知道 1. 如何使用解析云代码 (JS) 创建 Stripe 客户。 2. 我如何 link 那个 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");
    }
})
});

首先,在您的云代码中初始化 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。在客户端使用该用户卡信息以允许用户 select 他们想使用哪张卡,将卡 ID 发送到创建费用的方法,然后将该卡 ID 与附加的客户 ID 一起传递到用户对象。

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