Stripe创建客户iOS

Jac*_*ack 2 ios stripe-payments parse-platform

我正在使用条带和解析来允许我的应用程序的用户输入他们的信用卡并购买.我到目前为止用户可以购买一切都很好.但我希望允许用户输入他们的CC信息并进行保存,这样他们就不必再继续输入了.我真的很难做到这一点我得到了第一部分都弄明白我只需要得到这个.

更新:

- (IBAction)save:(id)sender {
    if (![self.paymentView isValid]) {
        return;
    }
    if (![Stripe defaultPublishableKey]) {
        UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Publishable Key"
                                                          message:@"Please specify a Stripe Publishable Key in Constants.m"
                                                         delegate:nil
                                                cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                                otherButtonTitles:nil];
        [message show];
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    STPCard *card = [[STPCard alloc] init];
    card.number = self.paymentView.card.number;
    card.expMonth = self.paymentView.card.expMonth;
    card.expYear = self.paymentView.card.expYear;
    card.cvc = self.paymentView.card.cvc;
    [Stripe createTokenWithCard:card completion:^(STPToken *token, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (error) {
            [self hasError:error];
        } else {
           [self createCustomerFromCard:(NSString *)token completion:(PFIdResultBlock)handler]; //I'm having trouble on this line here.
        }
    }];
}
- (void)hasError:(NSError *)error {
    UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"Error")
                                                      message:[error localizedDescription]
                                                     delegate:nil
                                            cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
                                            otherButtonTitles:nil];
    [message show];
}

+ (void)createCustomerFromCard:(NSString *)token completion:(PFIdResultBlock)handler
{
    [PFCloud callFunctionInBackground:@"createCustomer"
                       withParameters:@{
                                        @"tokenId":token,
                                        }
                                block:^(id object, NSError *error) {
                                    //Object is an NSDictionary that contains the stripe customer information, you can use this as is, or create an instance of your own customer class
                                    handler(object,error);
                                }];
}
Run Code Online (Sandbox Code Playgroud)

小智 11

所以,你在iOS方面做的一切都是正确的.不同之处在于,在你的后端,你会想要使用Customer这个令牌,然后对此进行指控Customer.在https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later上的文档中有一个高度相关的部分.

如果我这样做,我会创建2个Parse函数:一个调用createCustomer,它将带a tokenId,Customer用它创建一个,并返回该客户的ID.您可以在iOS应用中调用此功能,然后在本地保留客户ID.(你也可以将它附加到你User的Parse后端.重要的是你希望以后能够检索它).当您的应用用户通过输入卡信息创建令牌时,您将调用此功能一次.

然后,任何未来你想要对该信用卡另外收费的时间,你都会调用第二个Parse函数,调用它chargeCustomer.这将采用您之前保存的customerId和金额(以及可选的货币等).而已!

以下是这些函数的外观(请注意,我没有测试过这段代码,因此可能会出现一些小错误,但它应该足以传达我的观点):

Parse.Cloud.define("createCustomer", function(request, response) {
  Stripe.Customers.create({
    card: request.params['tokenId']
  }, {
    success: function(customer) {
      response.success(customer.id);
    },
    error: function(error) {
      response.error("Error:" +error); 
    }
  })
});

Parse.Cloud.define("chargeCustomer", function(request, response) {
  Stripe.Charges.create({
    amount: request.params['amount'],
    currency: "usd",
    customer: request.params['customerId']
  }, {
    success: function(customer) {
      response.success(charge.id);
    },
    error: function(error) {
      response.error("Error:" +error); 
    }
  })
});
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助.如果您需要进一步的帮助,请随时联系support@stripe.com.

插口