hakito / cakephp-sofortcom-plugin
CakePHP Sofort.com payment plugin
Installs: 181
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 2
Open Issues: 2
Type:cakephp-plugin
Requires
- cakephp/cakephp: ^3.7
- sofort/sofortlib-php: ^3.3
- spomky-labs/base64url: ^2.0
Requires (Dev)
- hakito/publisher: ^1.2.0
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-12-23 01:54:03 UTC
README
CakePHP-SofortCom-Plugin
CakePHP 4.x Sofort.com payment plugin
Installation
If you are using composer simply add the plugin using the command
composer require hakito/sofortcom-plugin
Otherwise download the plugin to app/Plugin/SofortCom. Add a PSR-4 compatible autoloader to your bootstrap.
Load the plugin in your bootstrap:
public function bootstrap() { // Call parent to load bootstrap from files. parent::bootstrap(); $this->addPlugin(\SofortCom\Plugin::class, ['routes' => true]); }
Creating tables
Create the database tables with the following command:
bin/cake migrations migrate -p SofortCom
Configuration
In your app.local.php add an entry for 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 ] ] ];
Usage
In your payment handling controller:
// 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'); } }
Event handlers
You must implement at least the following eventhandler:
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
This event is optional and fired before the user is redirected to the payment URL. It provides the following arguments:
$args = [ 'transaction' => '99999-53245-5483-4891', // SofortCom transaction id 'payment_url' => 'http://sofort.com/example' // SofortCom payment redirect url ]