PHP code example of beyntech / safecharge-php

1. Go to this page and download the library: Download beyntech/safecharge-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

beyntech / safecharge-php example snippets




$client = new \SafeCharge\Api\RestClient([
    'environment'       => \SafeCharge\Api\Environment::TEST,
    'merchantId'        => '<your merchantId>',
    'merchantSiteId'    => '<your merchantSiteId>',
    'merchantSecretKey' => '<your merchantSecretKey>',
]);

$client = new \SafeCharge\Api\RestClient();
$config = $client->getConfig();
$config->setEnvironment(\SafeCharge\Api\Environment::TEST);
$config->setMerchantId('<your merchantId>');
$config->setMerchantSiteId('<your merchantSiteId>');
$config->setMerchantSecretKey('<your merchantSecretKey>');

$config->setHashAlgorithm('md5');

$logger = new Monolog\Logger('safecharge-php-sdk');
$logger->pushHandler(new Monolog\Handler\StreamHandler('path/to/log', Monolog\Logger::DEBUG));
$client->setLogger($logger);



use SafeCharge\Api\RestClient;
use SafeCharge\Tests\SimpleData;
use SafeCharge\Tests\TestCaseHelper;

tests/SimpleData.php';

$config = [
    'environment'       => \SafeCharge\Api\Environment::TEST,
    'merchantId'        => '<your merchantId>',
    'merchantSiteId'    => '<your merchantSiteId>',
    'merchantSecretKey' => '<your merchantSecretKey>',
    'hashAlgorithm'     => '<sha256 or md5>'
];

$safecharge = new \SafeCharge\Api\SafeCharge();
$safecharge->initialize($config);
$paymentResponse = $safecharge->getPaymentService()->initPayment([
    'currency'       => 'EUR',
    'amount'         => '10',
    'userTokenId'    => '<user token id>',
    'paymentOption'  => [
        'card' => [
            'cardNumber'      => '<card number>',
            'cardHolderName'  => 'card name',
            'expirationMonth' => '<expiration month>',
            'expirationYear'  => '<expiration year>',
            'CVV'             => '<cvv>',
        ]
    ],
    'billingAddress' => [
        "firstName" => "<first name>",
        "lastName"  => "<last name>",
        "address"   => "<address>",
        "phone"     => "<phone number>",
        "zip"       => "<zip code>",
        "city"      => "<city>",
        'country'   => "<country ISO 3166-1 alpha-2>",
        "state"     => "<state>",
        "email"     => "<email address>",
        "county"    => "<county>",
    ]
]);

print_r($paymentResponse);

$openOrderRequest = $safecharge->getPaymentService()->openOrder([
    'userTokenId'       => '<user token id>',
    'clientUniqueId'    => '',
    'clientRequestId'   => '',
    'currency'          => SimpleData::getCurrency(),
    'amount'            => SimpleData::getAmount(),
    'amountDetails'     => SimpleData::getAmountDetails(),
    'items'             => SimpleData::getItems(),
    'deviceDetails'     => SimpleData::getDeviceDetails(),
    'userDetails'       => SimpleData::getUserDetails(),
    'shippingAddress'   => SimpleData::getShippingAddress(),
    'billingAddress'    => SimpleData::getBillingAddress(),
    'dynamicDescriptor' => SimpleData::getDynamicDescriptor(),
    'merchantDetails'   => SimpleData::getMerchantDetails(),
    'addendums'         => SimpleData::getAddEndUms(),
]);

print_r($openOrderRequest);
bash
composer