简体   繁体   中英

Execute a charge from existing customer with card in Stripe out of iOS

I have a very hard time figuring out how to do this from the iOS app programmatically.

Im using (besides other) the following classes from github: https://github.com/hybrdthry911/ELStripe

Class: ELCharge.m

+(ELCharge *)charge{
   return [[ELCharge alloc]init];
}

(*****************code in between ************************)

 //Will attempt to charge either a customer or card. Exactly one must exist   
per charge. If multiple or neither exist an exception will be raised.
//Warning: This is the final step it will APPLY A CHARGE TO THE 
ACCOUNT.

-(void)createChargeWithCompletion:(ELChargeCompletionBlock)handler{
    [ELCharge createCharge:self completion:handler];
}

+(void)createCharge:(ELCharge *)charge completion
 (ELChargeCompletionBlock)handler{
 NSError *chargeError;
     if (![charge validForProcessingWithError:&chargeError]) {
          handler(nil,chargeError);
          return;
     }

     if (!chargeError) {
          [ELStripe executeStripeCloudCodeWithMethod:@"POST"    
           prefix:@"charges" parameters:[ELCharge 
           dictionaryForCreatingCharge:charge] completionHandler:^(id 
           jsonObject, NSError *error) {
           handler([ELCharge 
           chargeFromParseStripeDictionary:jsonObject],error);
       }];
   }
}

In the iOS class, I do the following in order to create a test charge:

//create an automatic charge in stripe from an existing customer with card   
attached to him
-(void) executeChargeInStripe:(UIButton*)sender
{

     ELCharge *charge = [ELCharge charge];

    //Assign the charge properties
    charge.customerID = @"cus_72xvQI6Q5IC9it";
    charge.currency = @"USD";
    NSNumber *chargeAmount = [NSNumber numberWithInt:111];
    charge.amountInCents = chargeAmount;


//Call ChargeMethod from Github Framework
[ELCharge createCharge:charge completion:^(ELCharge *charge, NSError 
*error)    {
    if (!error)  {
    //code for normal handling
        NSLog(@"Charge has been made successfully");
    } else {
        // error code handling
        NSLog(@"Charge NOT made");

    }
}];

}

Im passing this to the following clould code:

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'];

 //call from parse to stripe done by http request as parse/stripe api 
   uncomplete
   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);
        }
  });
});

Im now getting the following error: Charge NOT made (my error message from the IOS class I created).

Im really surprised that I recieve neither an error in Parse Cloud nor in Stripe. If for example, I use an already used token instead of the customerID, I have an error in both. So the connection seems to work I assume, maybe there is something which I do wrong in the iOS class. Thank you!

have you reverted your Parse JavaScript SDK to 1.5.0? Anything past 1.5.0 is no longer supported.

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