PHP code example of andriichuk / bluesnap-php-sdk

1. Go to this page and download the library: Download andriichuk/bluesnap-php-sdk 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/ */

    

andriichuk / bluesnap-php-sdk example snippets


use Andriichuk\BlueSnap\BlueSnapClient;
use Andriichuk\BlueSnap\Configuration;
use Andriichuk\BlueSnap\Environment;
use GuzzleHttp\Client;
use GuzzleHttp\Psr7\HttpFactory;

$httpClient = new Client();
$httpFactory = new HttpFactory();

$blueSnap = new BlueSnapClient(
    new Configuration(
        username: $_ENV['BLUESNAP_USERNAME'],
        password: $_ENV['BLUESNAP_PASSWORD'],
        merchantId: $_ENV['BLUESNAP_MERCHANT_ID'],
        environment: Environment::Sandbox,
    ),
    httpClient: $httpClient,
    requestFactory: $httpFactory,
    streamFactory: $httpFactory,
);

$response = $blueSnap->plans()->create([
    'name' => 'Pro Monthly',
    'currency' => 'USD',
    'chargeFrequency' => 'MONTHLY',
    'recurringChargeAmount' => '29.99',
    'trialPeriodDays' => 14,
], idempotencyKey: '5b68d40b-24ad-4fdd-99e9-a0386a35c5c0');

$plan = $blueSnap->plans()->retrieve($response->json()['planId'])->json();

$activePlans = $blueSnap->plans()->all([
    'status' => 'ACTIVE',
    'pagesize' => 50,
    'gettotal' => true,
])->json();

$response = $blueSnap->subscriptions()->create([
    'planId' => 2283845,
    'vaultedShopperId' => 21188039,
    'quantity' => 1,
], idempotencyKey: 'e341e759-51fd-4629-8881-b0f560e01311');

$subscriptionId = $response->json()['subscriptionId'];

$blueSnap->subscriptions()->update($subscriptionId, [
    'planId' => 2283849,
]);

// Cancel immediately.
$blueSnap->subscriptions()->cancel($subscriptionId);

// Or allow the current billing period to finish.
$blueSnap->subscriptions()->cancelAtPeriodEnd($subscriptionId);

$response = $blueSnap->paymentFieldsTokens()->create([
    'authentication

$url = $client->hostedPageUrl()->build(new HostedPageRequest(
    planId: 3173219,
    merchantTransactionId: 'account-42',
    enc: $encryptedToken,
));

use Andriichuk\BlueSnap\Exception\RateLimitException;
use Andriichuk\BlueSnap\Exception\ValidationException;

try {
    $blueSnap->plans()->create($payload, $idempotencyKey);
} catch (ValidationException $exception) {
    foreach ($exception->errors as $error) {
        // $error->code, $error->name, $error->description
    }
} catch (RateLimitException $exception) {
    // Retry later using the same idempotency key.
}
bash
composer