hakito/cakephp-sofortcom-plugin

CakePHP Sofort.com支付插件

v2.0 2020-06-21 07:26 UTC

This package is auto-updated.

Last update: 2024-09-23 01:13:25 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

CakePHP-SofortCom-Plugin

Build Status Coverage Status

CakePHP 4.x Sofort.com支付插件

安装

如果你使用composer,只需使用以下命令添加插件

composer require hakito/sofortcom-plugin

否则,将插件下载到app/Plugin/SofortCom。将PSR-4兼容的自动加载器添加到你的bootstrap。

在bootstrap中加载插件

public function bootstrap()
{
    // Call parent to load bootstrap from files.
    parent::bootstrap();

    $this->addPlugin(\SofortCom\Plugin::class, ['routes' => true]);
}

创建表

使用以下命令创建数据库表

bin/cake migrations migrate -p SofortCom

配置

在你的app.local.php中为SofortCom添加一个条目

[
  'SofortCom' => [
    // enter your configuration key
    // you only can create a new configuration key by
    // creating a new Gateway project in your account at sofort.com
    'configkey' => 'dummy:key',

    // Encryption key for sending encrypted data to SofortCom
    'encryptionKey' => 'A_SECRET_KEY_MUST_BE_32_BYTES_LONG',

    // Default CurrencyCode.
    // You can override this when preparing the payment request.
    'currency' => 'EUR',

    // The conditions are used if you use the
    // SofortlibComponent::NeutralizeFee function
    'conditions' => [
        'fee' => 25,              // sofort.com fixed fee in cents
        'fee_relative' => '0.009' // relative sofort.com fee
    ]
  ]
];

使用

在你的支付处理控制器中

    // Load the component
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('SofortCom.Sofortlib');
    }

    // Sample checkout
    private function _checkoutSofortCom($orderId)
    {
        $this->Sofortlib->setAmount(12.34);
        $this->Sofortlib->setCurrencyCode('EUR');
        $this->Sofortlib->setReason('Buying Great Stuff', 'Order ' . $orderId); // Displayed as payment reason to the user
        $this->Sofortlib->setSuccessUrl('http://your-shop.example.com/success'); // The URL your clients are redirected upon success
        $this->Sofortlib->setAbortUrl('http://your-shop.example.com/abort'); // The URL your clients are redirected upon abort
        $this->Sofortlib->setShopId($orderId);

        try
        {
            $this->Sofortlib->PaymentRedirect();
        } catch (SofortLibException $ex) {
            Log::error(sprintf('SofortCom payment redirect errors: "%s"', var_export($ex->errors, true)));
            $this->Flash->set(__('Could not redirect to online banking (Error {0}). Please choose another payment method.', $ex->getMessage()));
            return $this->redirect('http://your-shop.example.com/recover');
        }
    }

事件处理器

你必须实现以下事件处理器之一

SofortCom.Notify

\Cake\Event\EventManager::instance()->on('SofortCom.Notify',
function ($event, $args)
{
  // $args =
  // [
  //   'shop_id' => 'order123',                   // Some id defined by you upon payment initialization
  //   'notifyOn' => 'pending',                   // SofortCom notification URL suffix
  //   'transaction' => '99999-53245-5483-4891',  // SofortCom transaction id
  //   'time' => '2010-04-14T19:01:08+02:00',     // SofortCom timestamp of notification
  //   'data' => {object},                        // Instance of Sofort\SofortLib\TransactionData
  // ]


  return ['handled' => true]; // If you don't set the handled flag to true
                              // the plugin will throw an UnhandledNotificationException
});

SofortCom.NewTransaction

此事件是可选的,在用户被重定向到支付URL之前触发。它提供以下参数

$args =
[
  'transaction' => '99999-53245-5483-4891',    // SofortCom transaction id
  'payment_url' => 'http://sofort.com/example' // SofortCom payment redirect url
]