Skip to content

Instantly share code, notes, and snippets.

@ademcan
Created March 11, 2020 13:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ademcan/0b1a95ddabeb34dd6956712109241262 to your computer and use it in GitHub Desktop.
Save ademcan/0b1a95ddabeb34dd6956712109241262 to your computer and use it in GitHub Desktop.
#import <Foundation/Foundation.h>
#import "StripeBridge.h"
#import <Stripe/Stripe.h>
#import "CheckoutViewController.h"
@implementation StripeBridge
RCT_EXPORT_MODULE();
// createPaymentIntent
RCT_EXPORT_METHOD(createPayment:(NSString*)clientSecret whitCc:(NSString*)cc withMo:(NSString*)month withYe:(NSString*)year withCvc:(NSString*)cvc callback:(RCTResponseSenderBlock)callback ){
NSNumber *num1 = @([month intValue]);
NSNumber *num2 = @([year intValue]);
STPPaymentMethodCardParams *cardParams = [STPPaymentMethodCardParams new];
cardParams.number = cc;
cardParams.expMonth = num1;
cardParams.expYear = num2;
cardParams.cvc = cvc;
STPPaymentMethodParams *paymentMethodParams = [STPPaymentMethodParams paramsWithCard:cardParams billingDetails:nil metadata:nil];
STPPaymentIntentParams *paymentIntentParams = [[STPPaymentIntentParams alloc] initWithClientSecret:clientSecret ];
paymentIntentParams.paymentMethodParams = paymentMethodParams;
CheckoutViewController *authContext = [[CheckoutViewController alloc] init];
[[STPPaymentHandler sharedHandler] confirmPayment:paymentIntentParams withAuthenticationContext:authContext completion:^(STPPaymentHandlerActionStatus status, STPPaymentIntent * paymentIntent, NSError * error) {
switch (status) {
case STPPaymentHandlerActionStatusSucceeded:
NSLog(@"PAYMENTINTENT: %@", paymentIntent.stripeId);
callback(@[[NSNull null], @"SUCCESS", paymentIntent.stripeId]);
break;
// Payment succeeded
case STPPaymentHandlerActionStatusCanceled:
callback(@[[NSNull null], @"CANCEL" ]);
break;
// Handle cancel
case STPPaymentHandlerActionStatusFailed:
NSLog(@"ERROR: %@", error);
callback(@[[NSNull null], @"ERROR", @"NULL" ]);
break;
// Handle error
}
}];
}
@end
@hamzaDv
Copy link

hamzaDv commented Jun 17, 2020

Hey,

I would like to trigger 3D Secure with Stripe subscription if could explain to me how,

Here is my back-end code:

In other words, where should I implement Stripe Modal for authentication?

Thank you,

`// Create Subscription 3D Secure
router.post('/handleSubscription', function (req, res) {
// CREATE TOKEN BY CARD
try {
stripe.tokens
.create({
card: {
number: ccnumber,
exp_month: month,
exp_year: year,
cvc: cvc,
}
})
// CREATE CUSTOMER BY TOKEN
.then(token => {
stripe.customers
.create({
description: description,
email: email,
metadata: metadata,
coupon: coupon,
source: token.id
})
// CREATE SUBSCRIPTION BY CUSTOMER
.then(customer => {
stripe.subscriptions
.create({
customer: customer.id,
items: [{
plan: 'plan_******'
}],
expand: ["latest_invoice.payment_intent"]
}).then(subscription => {
res.send({
subscription
})
}).catch(err => {
res.send({
err
})
})
})
})

}`

@ademcan
Copy link
Author

ademcan commented Jun 21, 2020

The issue here is that you are dealing with the CC numbers on your back-end. You should not do this for 2 reasons:

  • security (the CC info shouldn't touch your server)
  • dealing with 3D secure, the only way to deal with 3D secure is via the front-end.

What you need to do is instead of stripe.token.create with the CC details you should on the mobile with React Native create a paymentIntent and then use stripe.customers.create on your back-end to create a new user with the generated paymentIntent.

Here are some basic examples:
createPaymentIntent (Obj-C to add to the RN bridge): https://gist.github.com/ademcan/6dd706e9debeedff5ef45ff7343e6d87
stripe.customer.create (JS for the back-end): https://gist.github.com/ademcan/a2e86c7eaa6936aad78ff5f7a3f47929

I hope it helps!

@hamzaDv
Copy link

hamzaDv commented Jun 21, 2020

Oh
But yes, I see but the issue is how can I handle subscription after the paymentIntent because paymentIntent create charges. however, a subscription is a monthly payment?

@hamzaDv
Copy link

hamzaDv commented Jun 21, 2020

I thought about creating setupIntent (frontend) and then generate PaymentMethod and createCustomer then create Subscription (backend) is this right?

Thank you Adem!

@ademcan
Copy link
Author

ademcan commented Jun 21, 2020

Sorry my bad, you are right it's setupIntent and the gist here is correct:
https://gist.github.com/ademcan/6dd706e9debeedff5ef45ff7343e6d87
I just updated the name of the function, as you can see on L26 it creates a SetupIntent :)

@alejandrocolorado
Copy link

Hey ademcan,

Thanks so much for this, they were quite useful for the iOS stripe implementation on the app I'm working on.

Small question, do you happen to have a Java version of these (StripeBridge.m/StripeBridge.h) or any pointers on how to migrate them to Java?, I'm pretty new to these integrations so I'm a little bit lost.

Thanks in advance!

@ademcan
Copy link
Author

ademcan commented Feb 16, 2021

I haven't work on the Android version but you can check the following repo to get some idea: https://github.com/Fitpassu/react-native-stripe-payments. And be aware that Stripe is working on an official RN lib so if you are not in a hurry I would rather wait for it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment