SafeCharge REST API SDK for PHP.
SafeChargeās REST API SDK for PHP provides developer tools for accessing Safecharge's REST API. SafeChargeās REST API is a simple, easy to use, secure and stateless API, which enables online merchants and service providers to process consumer payments through SafeChargeās payment gateway. The API supports merchants of all levels of PCI certification, from their online and mobile merchant applications, and is compatible with a large variety of payment options, i.e. payment cards, alternative payment methods, etc. For SafeCharge REST API documentation, please see: https://www.safecharge.com/docs/api/, (*1)
Requirements
PHP 5.4 or later., (*2)
Installation
Installation via Composer
composer require grandmasterx/safecharge
Manual
If you do not wish to use Composer, you can download the latest release. Then include the init.php
file., (*3)
require_once('/path/to/safecharge-sdk/init.php');
Dependencies
The PHP SDK require the following extension in order to work properly:, (*4)
Configuration
Client
$client = new \grandmasterx\safecharge\api\RestClient([
'environment' => \grandmasterx\safecharge\api\Environment::TEST,
'merchantId' => '<your merchantId>',
'merchantSiteId' => '<your merchantSiteId>',
'merchantSecretKey' => '<your merchantSecretKey>',
]);
Or, (*5)
$client = new \grandmasterx\safecharge\api\RestClient();
$config = $client->getConfig();
$config->setEnvironment(\grandmasterx\safecharge\api\Environment::TEST);
$config->setMerchantId('<your merchantId>');
$config->setMerchantSiteId('<your merchantSiteId>');
$config->setMerchantSecretKey('<your merchantSecretKey>');
Logger
Logger can be configured with a PSR-3 compatible logger ., (*6)
Example with Monolog
$logger = new Monolog\Logger('safecharge-php-sdk');
$logger->pushHandler(new Monolog\Handler\StreamHandler('path/to/log', Monolog\Logger::DEBUG));
$client->setLogger($logger);
Example
Safecharge's PHP SDK appends merchantId, merchantSiteId, timestamp and checksum in the request., (*7)
<?php
$client = new \grandmasterx\safecharge\api\RestClient([
'environment' => \grandmasterx\safecharge\api\Environment::TEST,
'merchantId' => '<your merchantId>',
'merchantSiteId' => '<your merchantSiteId>',
'merchantSecretKey' => '<your merchantSecretKey>',
]);
$authenticationService = new \grandmasterx\safecharge\api\services\AuthenticationManagement($client);
$authenticationResponse = $authenticationService->getSessionToken([
'clientRequestId' => '1'
]);
$openOrderParams = [
'sessionToken' => $authenticationResponse['sessionToken'],
'currency' => 'USD',
'amount' => "10",
'amountDetails' => [
"totalShipping" => "0",
"totalHandling" => "0",
"totalDiscount" => "0",
"totalTax" => "0"
],
'items' => [
[
"id" => "1",
"name" => "name",
"price" => "10",
"quantity" => "1"
]
],
];
$orderService = new \grandmasterx\safecharge\api\services\OrdersManagement($client);
$openOrderResponse = $orderService->openOrder($openOrderParams);