Store credit card using Stripe + Parse with cloud code

不羁岁月 提交于 2019-12-04 22:58:16

Parse's stripe implementation is not as complete as it could be. In many cases you must use an HTTPRequest to perform stripe functions that Parse does not offer.

For those cases I use the following iOS method and CloudCode to execute all of my stripe HTTPRequests. It is very easy to code while following the stripe CURL API documentation. I then build off this method to perform most stripe tasks, IE create/update/delete customers, cards, charges etc.

I start by feeding it a Method, ie GET, POST, DELETE depending on whether you want to retrieve, create/update, or remove a stripe object.

I then feed it a combination of optional pre/suf/postfixes to create a url.

Example URL created:

Finally I give it the parameters, in the instance of creating a card and adding it to a customer this would only need to be a dictionary containing the tokenID.

+(void)executeStripeCloudCodeWithMethod:(NSString *)method
                                 prefix:(NSString *)prefix
                                 suffix:(NSString *)suffix
                                postfix:(NSString *)postfix
                          secondPostfix:(NSString *)secondPostfix
                             parameters:(NSDictionary *)params
                      completionHandler:(ELStripeCompletionBlock)handler
{
    NSDictionary *parameters = @{@"method":method,
                                 @"prefix":prefix?prefix:@"",
                                 @"suffix":suffix?suffix:@"",
                                 @"postfix":postfix?postfix:@"",
                                 @"secondPostfix":secondPostfix?secondPostfix:@"",
                                 @"params":params?params:[NSNull null]
                                 };

    [PFCloud callFunctionInBackground:@"stripeHTTPRequest"
                       withParameters:parameters
                                block:^(id object, NSError *error) {
        id jsonObject;
        if (!error) {
            NSError *jsonError = nil;
            //Turn the json string into an NSDictionary
            jsonObject = [NSJSONSerialization JSONObjectWithData:[object dataUsingEncoding:NSUTF8StringEncoding]
                                                         options:kNilOptions error:&jsonError];

        }
        handler(jsonObject,error);
    }];
}

The cloud code that is executed:

var STRIPE_SECRET_KEY = 'sk_test_your_test_code_here';
var STRIPE_API_BASE_URL = 'api.stripe.com/v1/'
Parse.Cloud.define("stripeHTTPRequest", function(request, response) 
{
    //Check for valid pre/suf/postfixes, if they are not there do not include them.
    var prefix = request.params["prefix"];
    var suffix = "";
    var postfix = "";
    var secondPostfix = "";
    if (!isEmpty(request.params["suffix"])) suffix = '/'+request.params['suffix'];  
    if (!isEmpty(request.params["postfix"])) postfix = '/'+request.params['postfix'];   
    if (!isEmpty(request.params["secondPostfix"])) secondPostfix = '/'+request.params['secondPostfix'];

    Parse.Cloud.httpRequest(
    {
            method: request.params["method"],
            //Create URL from base url and pre/suf/postfixes
            url: 'https://'+STRIPE_API_BASE_URL + prefix + suffix + postfix + secondPostfix,
            headers: {
                'Authorization': "Bearer " + STRIPE_SECRET_KEY
            },
            params:request.params["params"],
            success: function(httpResponse) 
            {
                //response text is a json dictionary
                response.success(httpResponse.text);
            },
            error: function(httpResponse) 
            {
                response.error(httpResponse.text);
            }
    });
});

Using the method above I can create individual methods to perform most of the stripe tasks I need.

Here is an example that will create a new card and attach it to a customer Stripe Card creation API

+ (void)createCardFromToken:(NSString *)tokenId customerId:(NSString *)customerId completionHandler:(ELCardCompletionBlock)handler
{

    [ELStripe executeStripeCloudCodeWithMethod:@"POST" //I use post here because we are creating a card. POST would also be used for updating a customer/card or refunding a charge for example
                                        prefix:@"customers" //If you look at the documentation and the example URL I use "customers" here as the prefix
                                        suffix:customerId //The customerID is the suffix, this will be the customer you are going to add the card to
                                       postfix:@"cards" //I believe this is "sources" now
                                 secondPostfix:nil //Not needed for this URL
                                    parameters:@{
                                                 @"card":tokenId  //Only parameter is a tokenId, and I wrap this inside an NSDictionary
                                                 }
                             completionHandler:^(id jsonObject, NSError *error) {
                                 if (error)
                                 {
                                     //Handle the error code here

                                     handler(nil,rejectError);
                                     return;
                                 }
                                 //If no error stripe returns a dictionary containing the card information. You can use this information to create a card object if so desired.
                                 handler([ELCard cardFromDictionary:jsonObject],error);
                             }];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!