mouadziani/laravel-mercanet

BnpParibas Mercanet支付网关的laravel包装器

v1.3.0 2021-11-07 18:54 UTC

This package is not auto-updated.

Last update: 2024-09-27 04:40:09 UTC


README

Logo Laravel Mercanet

Laravel Mercanet

BnpParibas Mercanet提供一个laravel包装器,它提供了一个轻量级的公共API,可以从您的laravel应用程序处理在线支付。

License

安装

您可以通过composer安装此包

composer require mouadziani/laravel-mercanet

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

php artisan vendor:publish --provider="Mouadziani\Mercanet\MercanetServiceProvider"

配置

作为第一步,您需要更改位于config/mercanet.php文件中的以下凭据,使用您从mercanet账户获得的自己的凭据。

return [
    /**
     * Can only be 'TEST' Or 'Production'. If empty or invalid, 'TEST' will be used.
     */
    'mode' => env('MERCANET_MODE', 'TEST'),

    // Credentials of testing environment
    'test' => [
        'merchant_id' => env('MERCANET_TEST_MERCHANT_ID', ''), // Required
        'key_version' => env('MERCANET_TEST_KEY_VERSION', '1'),
        'secret_key' => env('MERCANET_TEST_SECRET_KEY', ''), // Required
    ],

    // Credentials of production environment
    'production' => [
        'merchant_id' => env('MERCANET_PRODUCTION_MERCHANT_ID', ''), // Required
        'key_version' => env('MERCANET_PRODUCTION_KEY_VERSION', '1'), // Required
        'secret_key' => env('MERCANET_PRODUCTION_SECRET_KEY', ''), // Required
    ],

    'currency' => env('MERCANET_CURRENCY', 'EUR'),

    // Should be replaced with a url of your callback post route,
    // which will be invoked by mercanet service after processing of any payment.
    'normal_return_url' => env('MERCANET_NORMAL_RETURN_URL', 'https://example.com/payments/callback'),

    /**
    * You can set the default locale that you need to be used to translate the mercanet payment page
     * Allowed languages 'nl', 'fr', 'de', 'it', 'es', 'cy', 'en'
     */
    'language' => env('MERCANET_LOCALE', 'en'),
];

使用方法

以下是一些启动支付过程的方式

// Import the class namespaces first, before using it directly
use Mouadziani\Mercanet\Mercanet;

// You can either create new instance from Mercanet
$mercanet = new Mercanet();

// Or through static constructor boot.
$mercanet = Mercanet::boot();

准备和处理支付请求

为了处理新的支付,您需要从现有的mercanet实例调用newPaymentRequest(),然后设置以下参数

$mercanet->newPaymentRequest();

// Required
$mercanet->setTransactionReference('123456789'); 

// By default the currency used is EUR. If you wish to change it,
// you may call setCurrency method to set a different currency before calling pay() method
$mercanet->setCurrency('EUR');

// Optionally, You can also call setLanguage method to change the default locale of payment page
$mercanet->setLanguage('fr');

// Required and it should be integer 
// Make sure to multiply the original amount * 100 (eg: 199.00 * 100 = 19000)
$mercanet->setAmount(19000);

// Optional
$mercanet->setBillingContactFirstname('John');

 // Optional
$mercanet->setBillingContactLastname('Doe');

 // Optional
$mercanet->setCustomerContactEmail('john@doe.com');

// Then you can call pay() method to redirect user to the payment page of mercanet website.
$mercanet->pay();

除了从Mercanet类创建新实例并单独调用方法外,您还可以使用静态构造函数,如下例所示

use Mouadziani\Mercanet\Mercanet;

Mercanet::boot()
    ->newPaymentRequest()
    ->setTransactionReference('123456789')
    ->setCurrency('EUR')
    ->setLanguage('fr')
    ->setAmount(19000)
    ->setBillingContactFirstname('John')
    ->setBillingContactLastname('Doe')
    ->setCustomerContactEmail('john@doe.com')
    ->pay();

验证回调请求中的支付事务

为了从给定的回调请求中检索交易参考和支付状态,您可以使用以下方法。

use Mouadziani\Mercanet\Mercanet;

// Create new instance or call the static constructor from Mercanet class 
// and then call fromRequest() method and pass request parameters into it. 
$paymentResponse = Mercanet::boot()->fromResponse(request()->all());

// Then you can check if the given payment response is successfully passed by calling isSuccessfullyPassed() method
if($paymentResponse->isSuccessfullyPassed()) {
    // The payment is accepted.
    
    // You can get the transaction reference from the initialized payment request object
    $transactionReference = $paymentResponse->getTransactionReference();
    
    // Then you can do what you want, eg. change the status of the order related to the transaction reference, or mark it as paid...
    App\Order::query()
        ->where('transaction_reference', $transactionReference)
        ->update([
            'is_paid' => true
        ]);
} else {
    // The payment is failed 
}

测试

composer test

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

致谢

许可证

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

赞助商

特色仓库