简体   繁体   中英

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. I don't have knowledge on creating web service and sending token to server. So I have planned to charge the card through iOS code.

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

Thanks in advance..

You should never never *never* create a charge from within your iOS app. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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