eseperio/ceca-omnipay

使用 omnipay 实现CECA支付网关

1.0.0 2024-04-10 10:05 UTC

This package is auto-updated.

Last update: 2024-09-11 09:20:45 UTC


README

Ceca 非官方的 Omnipay PHP 支付处理库驱动器

Omnipay 是一个不依赖于框架的多网关支付处理库,适用于 PHP 5.3+。此包实现了 Omnipay 的 RedSys(原名 Sermepa)支持。

安装

此包可通过 Composer/Packagist 获得。

运行以下命令进行安装

composer require eseperio/ceca-omnipay

基本用法

为了处理支付,您需要使用您的凭证创建一个 Gateway,然后创建一个 PurchaseRequest 并将其发送到网关。

创建网关

$gateway = Omnipay::create('Ceca');

$gateway->setMerchantId('your_merchant_id');
$gateway->setTerminalId('your_terminal_id');
$gateway->setAcquirerBin('your_acquirer_bin');
$gateway->setEncryptionKey('your_encryption_key');
$gateway->setTestMode(true); // skip this line to use production mode or set it to false
// IMPORTANT: this gateway use omnipay support for currency, so you must set the currenty
// using its ISO name instead of the currency code (978).
$gateway->setCurrency('EUR');

发送购买请求

$response = $gateway->purchase();
    ->setTransactionId('your_transaction_id')
    ->setAmount('10.00')
    ->setDescription('your_description')
    ->setURL_OK('https://yourdomain.com/ok')
    ->setURL_NOK('https://yourdomain.com/nok');

/**
 * @var $response \Omnipay\Ceca\Message\PurchaseResponse
 */
$response = $purchaseRequest->send();
if ($response->isRedirect()) {
    $response->redirect();
} else {
    Yii::error('Payment request failed', 'omnipay');
    Yii::error($response->getMessage(), 'omnipay');
    throw new Exception($response->getMessage());
}

如果请求成功,这将重定向用户到支付网关。如果失败,将抛出异常。

然后,如果用户处理支付,网关将重定向用户到您提供的 URL_OK 或 URL_NOK。

IMPORTANT: URL_OK and URL_NOK are not valid for order confirmation. You must use the notification URL to confirm the order.

处理通知

在您的应用程序中,您必须创建一个路由来处理通知。这个路由将由支付网关调用以确认订单。此库实现了 AcceptNotification 来处理此功能

    $gateway = Omnipay::create('Ceca');
    $gateway->setMerchantId('your_merchant_id');
    [...] // same settings as before

    $notification = $gateway->acceptNotification();
    if ($notification->getTransactionStatus() == NotificationInterface::STATUS_COMPLETED) {
            // Mark your order as paid
            return;
        } else {
            throw new \Exception($notification->getMessage());
        }

模拟网关的通知

为了提高本地开发的测试,您可以通过调用通知请求的 send 方法来模拟网关的通知。

    $gateway = Omnipay::create('Ceca');
    $gateway->setMerchantId('your_merchant_id');
    [...] // same settings as before

    $notification = $gateway->acceptNotification();
    $notification->send();