gopay / payments-sdk-php
GoPay's PHP SDK for Payments REST API
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.7.0
- symfony/deprecation-contracts: ^3.3.0
Requires (Dev)
- hamcrest/hamcrest-php: *
- phpspec/prophecy: ~1.0
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: 9.3.7
- dev-master
- 1.10.1
- 1.10.0
- 1.9.0
- 1.8.0
- v1.7.3
- v1.7.2
- v1.7.1
- v1.7.0
- v1.6.2
- v1.6.1
- v1.6.0
- v1.5.7
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4.0
- v1.3.6
- v1.3.5
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3.0
- v1.2.3
- v1.2.2
- v1.2.1
- v1.2.0
- v1.1.1
- v1.1.0
- v1.0.1
- v1.0.0
- dev-hotfix/GPMAIN-7759-bnpl
- dev-hotfix/GPOMA-134-php-capture-na-nizsi-castku
- dev-supercash-batch
- dev-hotfix/GPOMA-70-php-api-revize
This package is not auto-updated.
Last update: 2024-11-19 08:46:17 UTC
README
Requirements
- PHP >= 8.1
- enabled extension
curl
,json
Installation
The simplest way to install SDK is to use Composer:
composer require gopay/payments-sdk-php
Basic usage
// minimal configuration $gopay = GoPay\Api::payments([ 'goid' => 'my goid', 'clientId' => 'my id', 'clientSecret' => 'my secret', 'gatewayUrl' => 'gateway url' ]); // full configuration $gopay = GoPay\Api::payments([ 'goid' => 'my goid', 'clientId' => 'my id', 'clientSecret' => 'my secret', 'gatewayUrl' => 'gateway url', 'scope' => GoPay\Definition\TokenScope::ALL, 'language' => GoPay\Definition\Language::CZECH, 'timeout' => 30 ]);
Configuration
Required fields
Optional fields
Available methods
SDK response? Has my call succeed?
SDK returns wrapped API response. Every method returns
GoPay\Http\Response
object. Structure of json/__toString
should be same as in documentation.
SDK throws no exception. Please create an issue if you catch one.
$response = $gopay->createPayment([/* define your payment */]); if ($response->hasSucceed()) { echo "hooray, API returned {$response}"; return $response->json['gw_url']; // url for initiation of gateway } else { // errors format: https://doc.gopay.com/en/?shell#http-result-codes echo "oops, API returned {$response->statusCode}: {$response}"; }
Are required fields and allowed values validated?
No. API validates fields pretty extensively so there is no need to duplicate validation in SDK. It would only introduce new type of error. Or we would have to perfectly simulate API error messages. That's why SDK just calls API which behavior is well documented in doc.gopay.com.
Advanced usage
Initiation of the payment gateway
// create payment and pass url to template $response = $gopay->createPayment([/* define your payment */]); if ($response->hasSucceed()) { $gatewayUrl => $response->json['gw_url'], $embedJs => $gopay->urlToEmbedJs() // render template }
Inline gateway
<form action="<?= $gatewayUrl ?>" method="post" id="gopay-payment-button"> <button name="pay" type="submit">Pay</button> <script type="text/javascript" src="<?= $embedJs ?>"></script> </form>
Redirect gateway
<form action="<?= $gatewayUrl ?>" method="post"> <button name="pay" type="submit">Pay</button> </form>
Asynchronous initialization using JavaScript
Enums (Code lists)
Instead of hardcoding bank codes string you can use predefined enums. Check using enums in create-payment example
Framework integration
Cache access token
Access token expires after 30 minutes so it's expensive to use new token for every request.
Unfortunately it's default behavior of GoPay\Token\InMemoryTokenCache
.
But you can implement your cache and store tokens in Memcache, Redis, files, ... It's up to you.
Your cache must implement GoPay\Token\TokenCache
interface.
Be aware that there are two scopes (TokenScope
) and
SDK can be used for different clients (clientId
, gatewayUrl
). So client
passed to
methods is unique identifier (string
) that is built for current environment.
Below you can see example implementation of caching tokens in file:
// register cache in optional service configuration $gopay = GoPay\payments( [/* your config */], ['cache' => new PrimitiveFileCache()] );
<?php use GoPay\Token\TokenCache; use GoPay\Token\AccessToken; class PrimitiveFileCache implements TokenCache { public function setAccessToken($client, AccessToken $t) { file_put_contents(__DIR__ . "/{$client}", serialize($t)); } public function getAccessToken($client) { $file = __DIR__ . "/{$client}"; if (file_exists($file)) { return unserialize(file_get_contents($file)); } return null; } }
Log HTTP communication
You can log every request and response from communication with API. Check available loggers
below. Or you can implement your own logger,
just implement GoPay\Http\Log\Logger
interface.
// register logger in optional service configuration $gopay = GoPay\payments( [/* your config */], ['logger' => new GoPay\Http\Log\PrintHttpRequest()] );
Contributing
Contributions from others would be very much appreciated! Send pull request/ issue. Thanks!
License
Copyright (c) 2015 GoPay.com. MIT Licensed, see LICENSE for details.