origami / stripe
Stripe PaymentIntents helper package
Installs: 2 714
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^7.2|^8.0.2
- illuminate/contracts: ^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- moneyphp/money: ^3.2
- stripe/stripe-php: ^7.0
README
This package is a helper for Laravel projects using Stripe's PaymentIntents API and manual confirmation SCA setup.
It was inspired by the SCA updates and logic on the Laravel Cashier package.
Installation
Install this package through Composer.
composer require origami/stripe
Requirements
This package is designed to work with Laravel >= 6.0 projects currently.
Setup
- You should add a your stripe keys to your
config/services.php
file:
'stripe' => [ 'secret' => env('STRIPE_SECRET'), 'key' => env('STRIPE_KEY'), 'webhook' => [ 'secret' => env('STRIPE_WEBHOOK_SECRET'), ] ],
- Update your
.env
file with the key and secret:
STRIPE_KEY=
STRIPE_SECRET=
STRIPE_WEBHOOK_SECRET=
Usage
Payment object
// Make a Payment from a payment_intent ID. $payment = Origami\Stripe\Payment::find($id); // Or create a Payment as a new payment_intent $payment = Origami\Stripe\Payment::create([ 'amount' => 1000, 'currency' => 'gbp', 'payment_method' => $method, 'capture_method' => 'manual', 'payment_method_types' => ['card'], 'confirmation_method' => 'manual', 'confirm' => true, ]));
Available Methods
Get amount
Returns amount as Money\Money
object using moneyphp/money
$payment->amount();
Check status
Returns a boolean:
public function hasStatus($status)
You can pass an array or a string:
$payment->hasStatus(['requires_confirmation','requires_capture']); $payment->hasStatus('requires_confirmation');
There are also helper methods for the PaymentIntent statuses:
$payment->requiresConfirmation(); $payment->requiresPaymentMethod(); $payment->requiresCapture(); $payment->requiresAction(); $payment->isCancelled(); $payment->isSucceeded(); $payment->isSuccessful(); // Alias for above
Validate PaymentIntent
See https://stripe.com/docs/payments/payment-intents/web-manual#creating-with-manual-confirmation
try { $payment = Origami\Stripe\Payment::create([ 'amount' => 1000, 'currency' => 'gbp', 'payment_method' => $method, 'capture_method' => 'manual', 'payment_method_types' => ['card'], 'confirmation_method' => 'manual', 'confirm' => true, ])); if (!$payment) { throw new Exception('Error fetching Stripe payment'); } $payment->validate(); // PaymentIntent is valid // capture_method: manual above means we need to capture in another controller // capture_method: automatic above means the payment was successully taken return response([ 'payment_intent_id' => $payment->id, 'success' => true, ]); } catch (Origami\Stripe\PaymentActionRequired $e) { // Action is required on the client end - see Stripe docs. return response()->json([ 'requires_action' => true, 'payment_intent_client_secret' => $e->payment->clientSecret(), 'success' => false, ], 200); } catch (Origami\Stripe\PaymentFailed $e) { // Payment failed - handle on the client end. return response()->json([ 'error' => $e->getMessage(), 'success' => false, ], 500); } catch (Stripe\Exception\CardException $e) { // Don't forget to handle Stripe's exceptions for declined cards, etc. return response()->json([ 'error' => $e->getMessage(), 'success' => false, ], 500); } catch (Exception $e) { // Something else went wrong. Log::error($e); return response()->json(['error' => 'Unexpected error', 'success' => false], 500); }