laravel payment
3.0.0
? Laravel 的 Omnipay 服務提供者。
$ composer require overtrue/laravel-payment -v
更新composer後,如果您使用的laravel版本<5.5,則需要註冊服務提供者:
// config/app.php
' providers ' => [
//...
Overtrue LaravelPayment ServiceProvider::class,
],
並發布設定檔:
$ php artisan vendor:publish --provider=Overtrue \ LaravelPayment \ ServiceProvider
如果你想使用門面模式,你可以註冊一個你想要使用的門面名稱,例如LaravelPayment
:
// config/app.php
' aliases ' => [
' LaravelPayment ' => Overtrue LaravelPayment Facade::class, // This is default in laravel 5.5
],
// config/payments.php
// The default gateway name which configured in `gateways` section.
' default_gateway ' => ' paypal ' ,
// The default options for every gateways.
' default_options ' => [
' test_mode ' => true ,
// ...
],
/*
* The gateways, you can config option by camel case or snake_case name.
*
* the option name is followed from gateway class, for example:
*
* $gateway->setMchId('overtrue');
*
* you can configured as:
* 'mch_id' => 'overtrue',
* or:
* 'mchId' => 'overtrue',
*/
' gateways ' => [
' paypal ' => [
' driver ' => ' PayPal_Express ' ,
' options ' => [
' username ' => env ( ' PAYPAL_USERNAME ' ),
' password ' => env ( ' PAYPAL_PASSWORD ' ),
' signature ' => env ( ' PAYPAL_SIGNATURE ' ),
' test_mode ' => env ( ' PAYPAL_TEST_MODE ' ),
],
],
// other gateways
],
您需要安裝要使用的網關:omnipay# payment-gateways
網關實例:
LaravelPayment:: gateway ( ' GATEWAY NAME ' ); // GATEWAY NAME is key name of `gateways` configuration.
LaravelPayment:: gateway ( ' alipay ' );
LaravelPayment:: gateway ( ' paypal ' );
使用預設網關:
LaravelPayment:: purchase (...);
例子:
$ formData = [
' number ' => ' 4242424242424242 ' ,
' expiryMonth ' => ' 6 ' ,
' expiryYear ' => ' 2030 ' ,
' cvv ' => ' 123 '
];
$ response = LaravelPayment::purchase([
' amount ' => ' 10.00 ' ,
' currency ' => ' USD ' ,
' card ' => $ formData ,
))-> send ();
if ( $ response -> isRedirect ()) {
// redirect to offsite payment gateway
$ response -> redirect ();
} elseif ( $ response -> isSuccessful ()) {
// payment was successful: update database
print_r ( $ response );
} else {
// payment failed: display message to customer
echo $ response -> getMessage ();
}
更多關於Omnipay的使用請參考Omnipay官方主頁
想知道如何從零開始建立 PHP 擴充包?
請關注我的實戰課程,我會在本課程中分享一些擴充包開發經驗——《PHP擴充包實戰教學 - 從入門到發布》
麻省理工學院