承接 origami/stripe 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

origami/stripe

最新稳定版本:2.3.0

Composer 安装命令:

composer require origami/stripe

包简介

Stripe PaymentIntents helper package

README 文档

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

  1. 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'),
        ]
    ],
  1. 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);
}

Author

Papertank Limited

License

MIT License

统计信息

  • 总下载量: 3.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-09-10