parkwayprojects/laravel-paywithbank3d

用于与 PayWithBank3D 交互的 Laravel 扩展包

1.0.0 2021-04-26 12:24 UTC

This package is auto-updated.

Last update: 2024-09-26 20:18:25 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

无缝使用 PayWithBank3D 的 Laravel 扩展包

安装

您可以通过 composer 安装此包

composer require parkwayprojects/laravel-paywithbank3d

如果您使用 Laravel >= 5.5,则可以跳过此步骤并直接进入 配置

  • Parkwayprojects\PayWithBank3D\PayWithBank3DServiceProvider::class

同时,按照以下方式注册 Facade

'aliases' => [
    ...
    'PayWithBank3D' => Parkwayprojects\PayWithBank3D\Facades\PayWithBank3DFacade::class,
    ...
]

配置

您可以使用此命令发布配置文件

php artisan vendor:publish --provider="Parkwayprojects\PayWithBank3D\PayWithBank3DServiceProvider"

一个名为 paywithbank3d.php 的配置文件,包含一些合理的默认值,将被放置在您的 config 目录中

<?php

/*
 * You can place your custom package configuration in here.
 */
return [
    /**
     * Public Key From PayWithBank3D.
     */
    'publicKey' => getenv('PWB3D_PUBLIC_KEY'),

    /**
     * Secret Key From PayWithBank3D.
     */
    'secretKey' => getenv('PWB3D_SECRET_KEY'),

    /**
     * switch to live or test.
     */
    'mode' => getenv('PWB3D_MODE', 'live'),

    /**
     * PayWithBank3D Test Payment URL.
     */
    'testUrl' => getenv('PWB3D_TEST_URL'),

    /**
     * PayWithBank3D Live Payment URL.
     */
    'liveURL' => getenv('PWB3D_LIVE_URL'),
];

使用

打开您的 .env 文件,并添加您的 PayWithBank3D 公钥、PayWithBank3D 密钥、PayWithBank3D 模式(默认为 live)、live url 和 test url,如下所示

PWB3D_PUBLIC_KEY=****
PWB3D_SECRET_KEY=****
PWB3D_MODE=test
PWB3D_TEST_URL=https://staging.paywithbank3d.com
PWB3D_LIVE_URL=https://paywithbank3d.com

设置路由和控制器方法,如下所示

// Laravel 5.1.17 and above
Route::post('/pay', 'PaymentController@redirectToGateway')->name('pay'); 

或者

Route::post('/pay', [
    'uses' => 'PaymentController@redirectToGateway',
    'as' => 'pay'
]);

Route::post('/pay2', [
    'uses' => 'PaymentController@PaymentData',
    'as' => 'data'
]);
Route::get('/payment/callback', 'PaymentController@handleGatewayCallback')->name('callback');

或者

// Laravel 5.0
Route::get('payment/callback', [
    'uses' => 'PaymentController@handleGatewayCallback'
]); 
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use PayWithBank3D;

class PaymentController extends Controller
{

    /**
     * Redirect the User to PayWithBank3D Payment Page
     * @return Url
     */
    public function redirectToGateway()
    {
        return PayWithBank3D::setUrl()->redirectNow();
    }

    /**
     * Get The PayWithBank3D Redirect Url
     * @return array
     */
    public function redirectUrl()
    {
        return  PayWithBank3D::getUrl();
    }

    /**
     * Obtain PayWithBank3D payment information
     * @return void
     */
    public function handleGatewayCallback()
    {
        $paymentDetails =  PayWithBank3D::getData();

        dd($paymentDetails);
        // Now you have the payment details,
        // you can then redirect or do whatever you want
    }
}

让我在这里解释一下这个包提供的流畅方法。

/**
 *  This fluent method does all the dirty work of sending a POST request with the form data
 *  to PayWithBank3D Api, then it gets the payment Url and redirects the user to PayWithBank3D
 *  Payment Page. I abstracted all of it, so you don't have to worry about that.
 *  Just eat your cookies while coding!
 */
PayWithBank3D::setUrl()->redirectNow();

/**
*  SetUrl can also accept an array instead of a request object and you are good to go, it will be in this format
 */

        $data = [
                    'reference' => time(),
                    'amount'=> 20000,
                    'currencyCode' => 'NGN',
                    'customer' => [
                        'name' => 'Edward Paul',
                        'email' => 'infinitypaul@live.com',
                        'phone' => '08170574789'
                    ],
                    'returnUrl' => route('verify'),
                    'metadata' => [
                        'orderId'=> '1234'
                    ]
                ];
       return PayWithBank3DFacade::setUrl($data)->redirectNow();


/**
 * This fluent method does all the dirty work of verifying that the just concluded transaction was actually valid,
 */
PayWithBank3D::getData();

/**
 * This method gets the return the redirect url in the case your frontend is detached from your backend
 * @returns array
 */
PayWithBank3D::setUrl()->getUrl();

一个示例表单将看起来像这样

<form method="POST" action="{{ route('pay') }}" accept-charset="UTF-8" class="form-horizontal" role="form">
        <div class="row" style="margin-bottom:40px;">
          <div class="col-md-8 col-md-offset-2">
            <p>
                <div>
                    Infinity Biscuit
                    ₦ 5,980
                </div>
            </p>

            <input type="hidden" name="email" value="infinitypaul@live"> {{-- required --}}
            <input type="hidden" name="name" value="Edward Paul">
<input type="hidden" name="phone" value="0702323463">
            <input type="hidden" name="amount" value="1000"> {{-- required --}}
           
            <input type="hidden" name="returnUrl" value="{{ route('callback') }}"> {{-- required --}}

          
            {{ csrf_field() }} {{-- works only when using laravel 5.1, 5.2 --}}

             <input type="hidden" name="_token" value="{{ csrf_token() }}"> {{-- employ this in place of csrf_field only in laravel 5.0 --}}

            <p>
              <button class="btn btn-success btn-lg btn-block" type="submit" value="Pay Now!">
              <i class="fa fa-plus-circle fa-lg"></i> Pay Now!
              </button>
            </p>
          </div>
        </div>
</form>

点击提交按钮后,客户将被重定向到 PayWithBank3D 网站。

因此,现在我们已经将客户重定向到 PayWithBank3D。客户在那里执行了一些操作(希望他或她已经支付了订单)并且现在被重定向回我们的网站。

一个请求被发送到我们的回调 URL(我们不希望冒充者错误地放置未支付订单)。

在处理来自支付提供商的请求的控制器中,我们有

PayWithBank3D::getData() - 此函数执行计算并确保它是一个有效的交易,否则会抛出异常。

致谢

许可

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件