I am using Parse as my backend and trying to integrate Stripe to allow customers to save their payment information for future payments.
Customers are not being saved to Stripe.
I think it has to do with my URL on my Cloud Code ("https://" + "REMOVED SECRET KEY" + ':@' + "api.stripe.com/v1" + "/customers/" + request.params.customerId + "/cards/" + request.params.cardId,)
This is my iOS code
- (IBAction)save:(id)sender {
if (![self.paymentView isValid]){
return;
}
if (![Stripe defaultPublishableKey]) {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:@"No Publishable Key"
message:@"Please specify a Stripe Publishable Key in Constants.m"
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[message show];
return;
}
PTKCard* card = self.paymentView.card;
NSLog(@"Card last4: %@", card.last4);
NSLog(@"Card expiry: %lu/%lu", (unsigned long)card.expMonth, (unsigned long)card.expYear);
NSLog(@"Card cvc: %@", card.cvc);
[[NSUserDefaults standardUserDefaults] setValue:card.last4 forKey:@"card.last4"];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)hasError:(NSError *)error {
UIAlertView *message = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error", @"Error")
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
otherButtonTitles:nil];
[message show];
}
typedef void (^STPCardCompletionBlock)(STPCard *card,NSError *error);
+ (void)addTokenId:(NSString *)tokenId toCustomerId:(NSString *)customerId completion:(STPCardCompletionBlock)handler {
[PFCloud callFunctionInBackground:@"stripeUpdateCustomer" withParameters:@{@"customerId":customerId,@"data":@{@"card":tokenId}} block:^(id object, NSError *error) {
handler([[STPCard alloc]initWithAttributeDictionary:object],error);
}];
}
+ (void)createCustomerFromCard:(NSString *)tokenId completion:(PFIdResultBlock)handler {
[PFCloud callFunctionInBackground:@"createCustomer"
withParameters:@{@"tokenId":tokenId, }
block:^(id object, NSError *error) {
//Object is an NSDictionary that contains the stripe customer information, you can use this as is, or create an instance of your own customer class
handler(object,error);
}];
}
This is my Cloud Code
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(REMOVED 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://" + "REMOVED SECRET KEY" + ':@' + "api.stripe.com/v1" + "/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);
}
});
});
Parse.Cloud.define("chargeToken",function(request,response) {
Stripe.initialize(REMOVED SECRET KEY);
Stripe.Charges.create(
request.params,
{
success:function(results)
{
response.success(results);
},
error:function(error)
{
response.error("Error:" +error);
}
}
);
});