Parse Strip integration in iOS

I am new to programming and I created an application for charging clients and would like to save their CC information and charge it later. I look through all the tutorials and documentation, and I cannot keep track of how I can integrate this into my application. Do I need to know other technical skills such as Rest API, Curl, Ruby etc. to get this setting? All manuals and documentation point to this direction. I really don't understand what GET / POST is and how it fits into iOS Objective-C.

Any recommendations on how to fix this will be greatly appreciated. I was stuck on this for a while.

+6
source share
2 answers

The Parse stripe API is not as complete as possible and should be. There are many functions that do not include initially, but the VIA HTTP request can be performed. I had to learn a bit of Javascript and an HTTP request to make many functions work. Of course, your first instinct should tell you that DO NOT store the CC number on any device! Each time you enter a custom CC number, you immediately receive a token, and then that’s all you need to use.

Luckly stripe gives you the ability to retain customers and attach CC to customers, and then charge that customer in the future without returning a CC number. Parse api does not handle adding CC to the client, so I added this function myself.

So, steps 1 and 2 Create a client using the API, and create a token from the CC information that they enter again using the Parse API. If you need help with this and need cloud code, let me know.

Step 3 Add CC to the client. I use a custom Customer object, but the main thing you really need is a stripe customerId, which is the customer.identifier identifier in my code, and the tokenID from your CC, which in my case is token.tokenId. The answer will be a JSON string with map information, I will turn it into a dictionary, and then create an STPCard from the dictionary. I also show how to remove a card from a client.

IOS code:

+(void)addToken:(STPToken *)token toCustomerId:(NSString *)customerId completionHandler:(PFIdResultBlock)block { [PFCloud callFunctionInBackground:@"stripeUpdateCustomer" withParameters:@{@"customerId":customerId,@"data":@{@"card":token.tokenId}} block:block]; } + (void)removeCard:(STPCard *)card FromCustomer:(ELCustomer *)customer completion:(STPCardDeletionBlock)handler { if (!customer ||!customer.identifier || !card || !card.identifier || !handler) [NSException raise:@"RequiredParameter" format:@"Required Parameter Missing for deleting card from customer"]; [PFCloud callFunctionInBackground:@"stripeDeleteCardFromCustomer" withParameters:@{@"cardId":card.identifier,@"customerId":customer.identifier} block:^(id object, NSError *error) { NSDictionary *dict = nil; NSError *jsonError = nil; if (object && [object isKindOfClass:[NSString class]] && !error) { dict = [NSJSONSerialization JSONObjectWithData:[object dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError]; } if (!jsonError && dict) { handler(dict[@"id"],[dict[@"deleted"] boolValue],error); } else if(jsonError) handler(nil,NO,jsonError); else handler(nil,NO,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); } } ); }); Parse.Cloud.define("stripeDeleteCardFromCustomer", function(request, response) { Stripe.initialize(STRIPE_SECRET_KEY); Parse.Cloud.httpRequest({ method:"DELETE", //STRIPE_SECRET_KEY will be your stripe secrect key obviously, this is different from the public key that you will use in your iOS/Android side. // STRIPE_API_BASE_URL = 'api.stripe.com/v1' url: "https://" + STRIPE_SECRET_KEY + ':@' + STRIPE_API_BASE_URL + "/customers/" + request.params.customerId + "/cards/" + request.params.cardId, success: function(httpResponse) { response.success(httpResponse.text); }, error: function(httpResponse) { response.error('Request failed with response code ' + httpResponse.status); } }); }); 

The iOS code for charging a client or token notifies you of the necessary parameters in the dictionary: the amount in cents is not dollars, the currency, and then either the client or the token. Please note that a customer may have many credit cards, but one of them is an active credit card. An active card is a card that will be charged when paying a client:

 //Will attempt to charge customer, if no customer exists, or it fails to charge the custoemr it will attempt to charge a card token directly; //*********Warning: This is the final step it will APPLY A CHARGE TO THE ACCOUNT.*************** -(void)processChargeThroughStripeWithCompletionHandler:(STPChargeCompletionHandler)handler { if (![self validForCardProcessing] && ![self validForCustomerProcessing]) { handler(nil,[NSError errorWithDomain:MY_ERROR_DOMAIN code:elErrorCodeNoCustomerOrTokenID userInfo:[NSDictionary dictionary]]); return; } [self processChargeThroughStripeUsingCustomerWithCompletionHandler:^(STPCharge *charge, NSError *error) { if (!error) handler(charge,error); else{ [self processChargeThroughStripeUsingCardWithCompletionHandler:^(STPCharge *charge, NSError *error) { handler(charge, error); }]; } }]; } //Process payment using a customer to their active card. No token is required if customer exists with a card on record. //*********Warning: This is the final step it will APPLY A CHARGE TO THE ACCOUNT.*************** -(void)processChargeThroughStripeUsingCustomerWithCompletionHandler:(STPChargeCompletionHandler)handler { if (!self.validForCustomerProcessing) { handler(self,[NSError errorWithDomain:MY_ERROR_DOMAIN code:elErrorCodeNoCustomerID userInfo:[NSDictionary dictionary]]); return; } [PFCloud callFunctionInBackground:@"chargeToken" withParameters:[STPCharge dictionaryFromSTPChargeForProccessingUsingCustomer:self] block:^(id object, NSError *error) { if (!error) { [self initSelfWithDictionary:object]; NSLog(@"object:%@",object); } handler(self,error); }]; } //Process payment using a token that is attached to the charge, when complete self will be updated with the new charge information //*********Warning: This is the final step it will APPLY A CHARGE TO THE ACCOUNT.*************** -(void)processChargeThroughStripeUsingCardWithCompletionHandler:(STPChargeCompletionHandler)handler { if (!self.validForCardProcessing) { handler(self,[NSError errorWithDomain:MY_ERROR_DOMAIN code:elErrorCodeNoTokenID userInfo:[NSDictionary dictionary]]); return; } [PFCloud callFunctionInBackground:@"chargeToken" withParameters:[STPCharge dictionaryFromSTPChargeForProccessingUsingCard:self] block:^(id object, NSError *error) { if (!error) { [self initSelfWithDictionary:object]; } handler(self,error); }]; } + (NSDictionary *)dictionaryFromSTPChargeForProccessingUsingCard:(STPCharge *)charge { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; dictionary[@"amount"] = charge.amountInCents; dictionary[@"currency"] = charge.currency; dictionary[@"card"] = charge.token.tokenId; return dictionary; } + (NSDictionary *)dictionaryFromSTPChargeForProccessingUsingCustomer:(STPCharge *)charge { NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; dictionary[@"amount"] = charge.amountInCents; dictionary[@"currency"] = charge.currency; dictionary[@"customer"] = charge.customer.identifier; return dictionary; } 

Cloud code for charging the client / token:

 Parse.Cloud.define("chargeToken",function(request,response) { Stripe.initialize(STRIPE_SECRET_KEY); Stripe.Charges.create ( request.params, { success:function(results) { response.success(results); }, error:function(error) { response.error("Error:" +error); } } ); }); 
+9
source

How do you store your CC information to charge it later? Before continuing, you need to know if it is PCI compatible or not. At best, the only thing you need to save is the expiration date, the last 4 digits, and the associated record object that Parse Stripe gives you, which corresponds to this CC. Do not try to keep full CC.

Regarding other issues:

Typically, you need to know the web language to do something like this. Here is an example of a possible stack that I saw in this situation:

IOS application β†’ sends a request to the server (rails, python, php, etc.) β†’ Sends a request to a third-party site

Third-party site response β†’ Server β†’ iOS app.

The server point is the interception of a call from a mobile application in Parse and the response of Parse back to the mobile application. The reason for this is that you can have a β€œmaster” db of transactions / states and can recover if the application is ever reinstalled on a user phone. It will also allow you to store an identifier pointing to the user's CC in the parsing bar (I assume).

You really need to understand GET / POST, as they become a very basic function of any iOS application. This is just how you get / insert records from the server. Given that almost all popular applications have some kind of network connection built into them, it really is the main part of iOS IMO programming.

+1
source

Source: https://habr.com/ru/post/970201/


All Articles