PHP code example of simpay / ecommerce
1. Go to this page and download the library: Download simpay/ecommerce 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/ */
simpay / ecommerce example snippets
use SimPay\SDK\SimPay;
$simpay = new SimPay(
bearerToken: $config->get('simpay_bearer'),
serviceId: $config->get('simpay_service_id'),
signatureKey: $config->get('simpay_signature_key'),
platform: 'opencart',
platformVersion: VERSION
);
$payload = $simpay->transaction()
->setAmount((float) $order['total'], $order['currency_code'])
->setDescription('Zamówienie #' . $order['order_id'])
->setControl((string) $order['order_id'])
->setCustomer($name, $email, $ip, $countryIso)
->setReturnUrls($successUrl, $failureUrl)
->setBilling($firstName, $lastName, $street, $building, $city, $postCode, $countryIso)
->toArray();
$response = $simpay->client()->createTransaction($payload);
$redirectUrl = $response['data']['redirectUrl'];
use SimPay\SDK\SimPay;
use SimPay\SDK\AmountVerifier;
use SimPay\SDK\PaymentStatus;
$payload = json_decode(file_get_contents('php://input'), true);
try {
$ipn = $simpay->handleIpn(
payload: $payload,
userAgent: $_SERVER['HTTP_USER_AGENT'] ?? null,
remoteIp: $_SERVER['REMOTE_ADDR'] ?? null // null = pomiń sprawdzenie IP
);
} catch (\SimPay\SDK\Exception\IpNotAllowedException $e) {
http_response_code(403);
exit($e->getMessage());
} catch (\SimPay\SDK\Exception\IpnException $e) {
http_response_code(400);
exit($e->getMessage());
}
// Rozróżnianie eventów
if ($ipn->isTransactionEvent()) {
if (!$ipn->isPaid()) {
exit('OK'); // jeszcze nie opłacone, potwierdź odbiór
}
$orderId = $ipn->getControl();
$paidAmount = $ipn->getEffectiveAmount($orderCurrency);
if (!AmountVerifier::isAmountSufficient($orderTotal, $paidAmount)) {
http_response_code(400);
exit('Amount mismatch');
}
// Aktualizuj status zamówienia
}
if ($ipn->isRefundEvent()) {
// Obsłuż refund
$refundId = $ipn->data['id'];
$status = $ipn->getStatus();
}
exit('OK');
$simpay->client()->createRefund($transactionId); // pełny
$simpay->client()->createRefund($transactionId, 50.00); // częściowy
// Prosta płatność BLIK Level 0 (bez rejestracji aliasu)
$simpay->client()->sendBlikLevel0($transactionId, '123456');
use SimPay\SDK\BlikAlias;
$alias = BlikAlias::register(
label: 'Nazwa Twojego Sklepu', // Zgodna z certyfikacją BLIK
value: 'unikalne_id_klienta_123' // Twoje wewnętrzne ID klienta
);
$simpay->client()->sendBlikLevel0($transactionId, '123456', $alias);
$ipn = $simpay->handleIpn($payload, $userAgent, $remoteIp);
if ($ipn->isBlikAliasEvent() && $ipn->isAliasActive()) {
$aliasUuid = $ipn->getAliasId(); // UUID aliasu SimPay
// Zapisz $aliasUuid w bazie danych przy koncie klienta
$db->saveAliasUuid($userId, $aliasUuid);
}
$alias = BlikAlias::fromUuid(
uuid: $savedAliasUuid, // UUID z kroku 2
label: 'Nazwa Twojego Sklepu'
);
$simpay->client()->sendBlikOneClick($transactionId, $alias);
// HTTP 204 — klient dostaje push w aplikacji bankowej
$alias = BlikAlias::fromValue(
value: 'unikalne_id_klienta_123',
label: 'Nazwa Twojego Sklepu'
);
$simpay->client()->sendBlikOneClick($transactionId, $alias);
use SimPay\SDK\Exception\AliasNotUniqueException;
try {
$simpay->client()->sendBlikOneClick($transactionId, $alias);
} catch (AliasNotUniqueException $e) {
// Wyświetl klientowi wybór banku
$alternatives = $e->getAlternatives();
// Każda alternatywa: ['label' => 'PKO BP', 'blik_id' => 953824, ...]
// Po wyborze klienta — ponów z blik_id:
$selectedBlikId = $alternatives[$userChoice]['blik_id'];
$resolvedAlias = $alias->withBlikId($selectedBlikId);
$simpay->client()->sendBlikOneClick($transactionId, $resolvedAlias);
}
$channels = $simpay->client()->getChannels();
use SimPay\SDK\Http\HttpClientInterface;
use SimPay\SDK\Http\HttpResponse;
class WordPressHttpClient implements HttpClientInterface
{
public function request(string $method, string $url, array $headers = [], ?array $body = null, int $timeout = 30): HttpResponse
{
$response = wp_remote_request($url, [
'method' => $method,
'headers' => $headers,
'timeout' => $timeout,
'body' => $body !== null ? json_encode($body) : null,
]);
if (is_wp_error($response)) {
throw new \SimPay\SDK\Exception\HttpException($response->get_error_message());
}
return new HttpResponse(
wp_remote_retrieve_response_code($response),
wp_remote_retrieve_body($response)
);
}
}
$simpay = new SimPay($bearer, $serviceId, $signatureKey, 'wordpress', $version, new WordPressHttpClient());
use SimPay\SDK\CacheInterface;
class PrestaShopCache implements CacheInterface
{
public function get(string $key): mixed
{
$val = \Cache::getInstance()->get($key);
return $val !== false ? $val : null;
}
public function set(string $key, mixed $value, int $ttl = 3600): void
{
\Cache::getInstance()->set($key, $value, $ttl);
}
public function delete(string $key): void
{
\Cache::getInstance()->delete($key);
}
}
$simpay = new SimPay($bearer, $serviceId, $signatureKey, 'prestashop', _PS_VERSION_, null, new PrestaShopCache());
use SimPay\SDK\PaymentStatus;
PaymentStatus::isPaid($status); // transaction_paid | transaction_confirmed
PaymentStatus::isFinal($status); // paid, confirmed, canceled, expired, failure, fraud, refunded
PaymentStatus::isFailed($status); // canceled, expired, failure, fraud
// Stałe do użycia w match/switch:
PaymentStatus::TRANSACTION_PAID
PaymentStatus::TRANSACTION_CANCELED
PaymentStatus::TRANSACTION_EXPIRED
PaymentStatus::TRANSACTION_FAILURE
PaymentStatus::TRANSACTION_FRAUD
PaymentStatus::TRANSACTION_REFUNDED
// BLIK OneClick:
PaymentStatus::ALIAS_ACTIVE
PaymentStatus::ALIAS_INACTIVE
PaymentStatus::ALIAS_PENDING
// Typy eventów IPN:
PaymentStatus::TYPE_TRANSACTION_STATUS // 'transaction:status_changed'
PaymentStatus::TYPE_REFUND_STATUS // 'transaction_refund:status_changed'
PaymentStatus::TYPE_BLIK_ALIAS_STATUS // 'blik:alias_status_changed'
PaymentStatus::TYPE_BLIK_CODE_STATUS // 'transaction_blik_level0:code_status_changed'