简体   繁体   English

条纹创建客户iOS

[英]Stripe create customer iOS

I'm using stripe and parse to allow the user of my app to enter in their credit card and purchase. 我正在使用Stripe和Parse来允许我的应用程序的用户输入其信用卡并进行购买。 I got that far the user can make a purchase everything is good. 我知道用户可以购买的东西一切都很好。 But I want to allow the user to enter their CC information and have that save so they don't have to keep re-entering it. 但是我想允许用户输入他们的抄送信息并保存该信息,这样他们就不必继续输入。 I'm having a really hard time doing this I got the first part all figured out I just need to get this. 我很难做到这一点,我已经弄清楚了第一部分,我只需要得到这个。

Update : 更新

- (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);
                                }];
}

So, you're doing everything right on the iOS side of things. 因此,您正在iOS方面做所有事情。 The difference is that on your backend, you're going to want to make a Customer out of this token and then make charges against that Customer . 不同之处在于,在您的后端,您将要使用此令牌创建一个Customer ,然后向该Customer收费。 There is a highly-relevant section on this in our documentation at https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later . https://stripe.com/docs/tutorials/charges#saving-credit-card-details-for-later的文档中,有与此相关的部分。

If I were doing this, I'd create 2 Parse functions: one called createCustomer , which would take a tokenId , create a Customer with it, and return that customer's ID. 如果这样做,我将创建2个Parse函数:一个名为createCustomer函数,它将使用tokenId ,使用它创建一个Customer ,然后返回该客户的ID。 You could call this in your iOS app, and then persist the customer ID locally. 您可以在iOS应用中调用它,然后在本地保留客户ID。 (You could also attach it to your User on the Parse backend. The important thing is just that you want to be able to retrieve it later). (您也可以将其附加到Parse后端的User上。重要的是您希望以后能够检索它)。 When your app user creates a token by entering their card info, you'd call this function once. 当您的应用程序用户通过输入其卡信息创建令牌时,您只需调用一次此功能。

Then, any future time you want to make another charge against that credit card, you'd call a second Parse function, call it chargeCustomer . 然后,以后您想用该信用卡收取其他费用时,您将调用第二个Parse函数,将其chargeCustomer This would take the customerId that you saved previously and an amount (and, optionally, currency, etc). 这将使用您先前保存的customerId和一个金额(以及(可选)货币等)。 That's it! 而已!

Here's what those functions might look like (please note, I haven't tested this code, so there may be small errors, but it should be enough to communicate my point): 这些功能可能如下所示(请注意,我尚未测试此代码,因此可能会有一些小错误,但足以表明我的观点):

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); 
    }
  })
});

Hopefully that helps. 希望有帮助。 If you need further assistance, feel free to contact support@stripe.com. 如果您需要进一步的帮助,请随时联系support@stripe.com。

Jack 插口

Step 1 Generate a Customer using Parse's API. 步骤1使用Parse的API生成客户。

Step 2 Generate a Token from the CC information they enter again using Parse's API. 步骤2根据他们使用Parse API再次输入的CC信息生成令牌。 If you need help with this, and the cloud code required let me know. 如果您需要帮助,请告知我所需的云代码。

Step 3 Add a CC to a customer. 步骤3向客户添加抄送。 I have the code below. 我有下面的代码。 The response back will be a Dictionary, and then I create an STPCard from the dictionary. 返回的响应将是一个Dictionary,然后根据该字典创建一个STPCard。

iOS Code: 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 Required: 所需的云代码:

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); 
            }
        }
    );
});

Here I have implemented jflinter's cloud code. 在这里,我实现了jflinter的云代码。 Keep in mind you can include a lot more than just the tokenId to create a customer, like email, description, metadata, etc. but this just creates a customer with a card, and no other information: 请记住,您不仅可以包含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);
    }];
}

Creating a charge using jflinter's code: 使用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

Correction on @jflinter 's code above. 上面@jflinter的代码的更正。 On the chargeCustomer function. 在chargeCustomer函数上。 replace function( customer ) with function ( charge ) 将功能( 客户 )替换为功能( 收费

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); 
    }
  })
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM