Stripe创建客户iOS

时间:2014-09-17 23:12:34

标签: ios parse-platform stripe-payments

我正在使用条带和解析来允许我的应用用户输入他们的信用卡并购买。我到目前为止用户可以购买一切都很好。但我希望允许用户输入他们的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);
                                }];
}

3 个答案:

答案 0 :(得分:11)

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

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

然后,如果您希望再次对该信用卡收取额外费用,您可以拨打第二个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); 
    }
  })
});

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

杰克

答案 1 :(得分:4)

步骤1使用Parse的API生成客户。

步骤2使用Parse的API再次从他们输入的CC信息生成令牌。如果您需要这方面的帮助,并且需要云代码,请告诉我。

步骤3向客户添加CC。我有下面的代码。回复将是一个字典,然后我从字典中创建一个STPCard。

iOS代码:

typedef void (^STPCardCompletionBlock)(STPCard *card,NSError *error);

    +(void)addTokenId:(NSString *)tokenId toCustomerId:(NSString *)customerId completion:(STPCardCompletionBlock)handler
{
    [PFCloud callFunctionInBackground:@"stripeUpdateCustomer" withParameters:@{@"customerId":customerId,@"data":@{@"card":tokenId}} block:^(id object, NSError *error) {
        handler([[STPCard alloc]initWithAttributeDictionary:object],error);
    }];
}

需要Cloud Code:

Parse.Cloud.define("stripeUpdateCustomer", function(request, response) 
{
        Stripe.Customers.update
    (
        request.params["customerId"],
        request.params["data"],
        {
            success:function(results)
            {
                console.log(results["id"]);
                response.success(results);
            },
            error:function(error)
            {
                response.error("Error:" +error); 
            }
        }
    );
});

我在这里实施了jflinter的云代码。请记住,除了tokenId之外,您可以包含更多内容来创建客户,例如电子邮件,描述,元数据等,但这只会创建一个带有卡的客户,而不是其他信息:

+(void)createCustomerFromCard:(NSString *)tokenId completion:(PFIdResultBlock)handler
{
    [PFCloud callFunctionInBackground:@"createCustomer"
                       withParameters:@{
                                        @"tokenId":tokenId,
                                        }
                                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);
    }];
}

使用jflinter的代码创建费用:

+(void)chargeCustomer:(NSString *)customerId amount:(NSNumber *)amountInCents completion:(PFIdResultBlock)handler
{
    [PFCloud callFunctionInBackground:@"chargeCustomer"
                       withParameters:@{
                                        @"amount":amountInCents,
                                        @"customerId":customerId
                                        }
                                block:^(id object, NSError *error) {
                                    //Object is an NSDictionary that contains the stripe charge information, you can use this as is or create, an instance of your own charge class.
                                    handler(object,error);

                                }];
}
@end

答案 2 :(得分:3)

更正@jflinter上面的代码。关于chargeCustomer函数。用功能(充电

替换功能(客户
Parse.Cloud.define("chargeCustomer", function(request, response) {
  Stripe.Charges.create({
    amount: request.params['amount'],
    currency: "usd",
    customer: request.params['customerId']
  }, {
    success: function(charge) {
      response.success(charge.id);
    },
    error: function(error) {
      response.error("Error:" +error); 
    }
  })
});