Skip to content

Instantly share code, notes, and snippets.

@offsky
Last active April 24, 2020 16:36
Show Gist options
  • Save offsky/48acb77287205489e5673568d01f63e3 to your computer and use it in GitHub Desktop.
Save offsky/48acb77287205489e5673568d01f63e3 to your computer and use it in GitHub Desktop.
Laravel Cashier optimized for 3D Secure cards
//Add these lines to Cashier's Concerns/ManagesPaymentMethods.php
use Stripe\PaymentIntent as StripePaymentIntent;
...
public function createPaymentIntent(array $options = [])
{
return StripePaymentIntent::create(
$options, $this->stripeOptions()
);
}
cardButton.addEventListener('click', function(ev) {
ev.preventDefault();
ev.stopPropagation();
if(validate_payment_form()) {
disable_form();
stripe.confirmCardPayment(clientSecret, {
payment_method: {
card: cardElement,
billing_details: {
name: cardholderName.value,
email: cardholderEmail.value
},
}
}
).then(function(result) {
if(result.error) {
enable_form();
if(result.error.message) {
$('#card-element').addClass("is-invalid");
$('#card-error').text(result.error.message);
}
} else { //success
enable_form();
$('#intent').val(result.paymentIntent.payment_method);
container.submit();
disable_form();
}
});
}
});
public function startPurchase()
{
$user = auth()->user();
$stripeCustomer = $user->createOrGetStripeCustomer();
$intent = $user->createPaymentIntent([
'amount' => 1000,
'currency' => 'usd',
'customer' => $stripeCustomer->id,
'setup_future_usage' => 'off_session'
]);
return view('subscription.purchase',compact("intent"));
}
public function finishPurchase(Request $request)
{
$user = auth()->user();
...set $plan_id...
try {
$ret = $user->newSubscription('default', $plan_id)->trialDays(30)->create($data['intent']);
} catch(IncompletePayment $e) {
return redirect()->route(
'cashier.payment',
[$e->payment->id, 'redirect' => route('subscription.success')]
);
} catch(PaymentFailure $e) {
return redirect()->back()->withErrors([$e->getMessage()]);
}
return view('subscription.success');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment