简体   繁体   English

Stripe API通过iOS Code创建费用吗?

[英]Stripe API create charge through iOS Code?

I have been working on ApplePay with stripe, all fine until taking stripe token with PKPayment Here Everyone mentioned send your stripe token to server and charge the amount. 我一直在ApplePay与条纹,所有罚款,直至采取条纹令牌PKPayment这里每个人都提到把你带令牌服务器并收取金额。 I don't have knowledge on creating web service and sending token to server. 我不具备创建Web服务并将令牌发送到服务器的知识。 So I have planned to charge the card through iOS code. 因此,我计划通过iOS代码对卡进行收费。

Create-Charge Docs: Link 创建费用文档: 链接

curl https://api.stripe.com/v1/charges \
   -u sk_test_BQokikJOvBiI2HlWgH4olfQ2: \
   -d amount=999 \
   -d currency=usd \
   -d description="Example charge" \
   -d source=tok_IPLStrXFSITtr78XW5SyDWL8

Here We don't know how to make the post data with secret key. 在这里,我们不知道如何使用密钥创建帖子数据。

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

    NSString *urlString = @"https://api.stripe.com/v1/charges";
    NSURL *url = [NSURL URLWithString:urlString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    request.HTTPMethod = @"POST";
    NSString *postBody = [NSString stringWithFormat:@"source=%@&amount=%@", sourceID, @1099];
    NSData *data = [postBody dataUsingEncoding:NSUTF8StringEncoding];

    NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request
                                                               fromData:data
                                                      completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
                                                          NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
                                                          if (!error && httpResponse.statusCode != 200) {
                                                              error = [NSError errorWithDomain:StripeDomain
                                                                                          code:STPInvalidRequestError
                                                                                      userInfo:@{NSLocalizedDescriptionKey: @"There was an error connecting to your payment backend."}];
                                                          }
                                                          if (error) {
                                                              completion(STPBackendChargeResultFailure, error);
                                                          } else {
                                                              completion(STPBackendChargeResultSuccess, nil);
                                                          }
                                                      }];

    [uploadTask resume];

Error : 错误

You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY'). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/.

We have looked similar questions Apple Pay using Stripe send token to server and charge for purchase 我们看过类似的问题, Apple Pay使用Stripe将令牌发送到服务器并收取购买费用

Thanks in advance.. 提前致谢..

You should never never *never* create a charge from within your iOS app. 您永远不要*永远*不要*在iOS应用程序内创建费用。 You need your secret key to create a charge and it's not secure to either store your secret api key from within your app, or retrieve your secret key in your app. 您需要使用密钥来产生费用,从应用程序内部存储api密钥或在应用程序中检索密钥并不安全。

Your public key can be safely stored in your app to create a token, and then you can send that token to your backend to create a charge. 您的公钥可以安全地存储在应用程序中以创建令牌,然后您可以将该令牌发送到后端以创建费用。 This allows your secret key to be safely stored on your servers. 这样可以将您的密钥安全地存储在服务器上。

Here's Stripe's sample backend in ruby that shows you how to create a charge with the token you created: https://github.com/stripe/example-ios-backend 这是Stripe在ruby中的示例后端,向您展示了如何使用创建的令牌创建费用: https : //github.com/stripe/example-ios-backend

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

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