繁体   English   中英

条纹创建客户iOS

[英]Stripe create customer iOS

我正在使用Stripe和Parse来允许我的应用程序的用户输入其信用卡并进行购买。 我知道用户可以购买的东西一切都很好。 但是我想允许用户输入他们的抄送信息并保存该信息,这样他们就不必继续输入。 我很难做到这一点,我已经弄清楚了第一部分,我只需要得到这个。

更新

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

因此,您正在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使用Parse的API生成客户。

步骤2根据他们使用Parse API再次输入的CC信息生成令牌。 如果您需要帮助,请告知我所需的云代码。

步骤3向客户添加抄送。 我有下面的代码。 返回的响应将是一个Dictionary,然后根据该字典创建一个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);
    }];
}

所需的云代码:

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

上面@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); 
    }
  })
});

暂无
暂无

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

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