PHP code example of winpay / core

1. Go to this page and download the library: Download winpay/core 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/ */

    

winpay / core example snippets


interface ConfigInterface
{
    public function getPartnerId(): string;
    public function getMerchantPrivateKey(): string;
    public function getChannelId(): string;
    public function getBaseUrl(): string;
    public function getWinpayPublicKey(): ?string;
    public function get(string $key, mixed $default = null): mixed;
    public function set(string $key, mixed $value): void;
    public function all(): array;
}

interface HttpClientInterface
{
    public function request(string $method, string $endpoint, ?array $body = null): WinpayResponse;
}

use Winpay\Core\WinpayClient;

// Langsung lempar array (otomatis pake WinpayConfig)
$client = new WinpayClient([
    'partner_id'          => 'your_partner_id',
    'merchant_private_key' => '-----BEGIN PRIVATE KEY-----\n...',
    'channel_id'          => 'WEB',
    'base_url'            => 'https://sandbox-api.bmstaging.id/snap',
    'winpay_public_key'   => '-----BEGIN PUBLIC KEY-----\n...',
    'checkout_key'        => 'your_checkout_key',
    'checkout_secret_key' => 'your_checkout_secret_key',
    'checkout_base_url'   => 'https://sandbox-checkout.winpay.id',
]);

// SNAP channel (RSA-SHA256)
$client->snap()->va()->create($payload);
$client->snap()->va()->inquiry($payload);
$client->snap()->va()->status($payload);
$client->snap()->va()->delete($payload);

$client->snap()->qris()->create($payload);
$client->snap()->qris()->status($payload);
$client->snap()->qris()->cancel($payload);

$client->snap()->ewallet()->create($payload);
$client->snap()->ewallet()->status($payload);
$client->snap()->ewallet()->cancel($payload);

$client->snap()->creditCard()->create($payload);
$client->snap()->creditCard()->status($payload);
$client->snap()->creditCard()->cancel($payload);

$client->snap()->retail()->create($payload);
$client->snap()->retail()->status($payload);
$client->snap()->retail()->cancel($payload);

$client->snap()->report()->balance($payload);
$client->snap()->report()->history($payload);
$client->snap()->report()->statement($payload);

// Checkout Page (HMAC-SHA256)
$client->snap()->checkout()->create($payload);
$client->snap()->checkout()->find($id);
$client->snap()->checkout()->findByRef($merchantRef);
$client->snap()->checkout()->update($id, $payload);
$client->snap()->checkout()->delete($id);
$client->snap()->checkout()->deleteByRef($merchantRef);

// Verifikasi callback (SNAP RSA signature)
$verified = $client->verifyCallback('POST', '/callback/path', $body, $timestamp, $signature);

$client = new WinpayClient(
    config: $myConfig,                        // ConfigInterface
    httpClient: $psr18Client,                 // ClientInterface
    requestFactory: $psr17RequestFactory,     // RequestFactoryInterface
    streamFactory: $psr17StreamFactory,       // StreamFactoryInterface
    onRequest: $callback,                     // ?callable (buat logging/monitoring)
    userAgent: 'my-wrapper/1.0',              // string
);

$onRequest = function (
    string $method,
    string $url,
    ?array $body,
    array $headers,
    string $stringToSign,
): void {
    // Catet atau monitor request
};
$client = new WinpayClient(config: [...], onRequest: $onRequest);
bash
php vendor/bin/phpunit