PHP code example of mupaybr / mupay-sdk

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

    

mupaybr / mupay-sdk example snippets




uPag\Sdk\MuPagClient;

$mupag = MuPagClient::test(getenv('MUPAG_API_KEY'));

$charge = $mupag->charges->create(
    [
        'amount_cents' => 9900,
        'payment_method' => 'pix',
        'customer' => [
            'name' => 'Ana Silva',
            'email' => '[email protected]',
            'tax_id' => '12345678901',
        ],
    ],
    idempotencyKey: 'order_123_charge_1',
);

echo $charge['charge_id'] . PHP_EOL;

use MuPag\Sdk\Http\RetryPolicy;
use MuPag\Sdk\MuPagClient;

$mupag = MuPagClient::test(
    apiKey: getenv('MUPAG_API_KEY'),
    retryPolicy: new RetryPolicy(maxRetries: 2, baseDelayMs: 200),
    timeoutSeconds: 10.0,
);

$charge = $mupag->charges->create(
    [
        'amount_cents' => 14990,
        'payment_method' => 'pix',
        'customer' => [
            'id' => 'customer_123',
            'name' => 'Ana Silva',
            'email' => '[email protected]',
            'tax_id' => '12345678901',
        ],
    ],
    idempotencyKey: 'order_123_charge_1',
);

foreach ($mupag->charges->all(['limit' => 50]) as $charge) {
    echo $charge['charge_id'] . PHP_EOL;
}

$subscription = $mupag->subscriptions->cancel(
    id: 'aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa',
    mode: 'immediate',
    reason: 'pedido do cliente',
    idempotencyKey: 'cancel_sub_123',
);

$payload = file_get_contents('php://input');
$signature = $_SERVER['HTTP_MUPAG_SIGNATURE'] ?? '';

$event = $mupag->webhooks->constructEvent(
    payload: $payload,
    signatureHeader: $signature,
    secret: getenv('MUPAG_WEBHOOK_SECRET'),
);

use MuPag\Sdk\Exception\ApiException;
use MuPag\Sdk\Exception\OutcomeUnknownException;
use MuPag\Sdk\Exception\RateLimitException;

try {
    $mupag->charges->create(
        [
            'amount_cents' => 9900,
            'payment_method' => 'pix',
            'customer' => [
                'name' => 'Cliente Exemplo',
                'email' => '[email protected]',
                'tax_id' => '00000000000',
            ],
        ],
        idempotencyKey: 'order_123_error_handling',
    );
} catch (OutcomeUnknownException $exception) {
    persist_for_reconciliation($exception->idempotencyKey());
} catch (RateLimitException $exception) {
    retry_later($exception->retryAfterSeconds());
} catch (ApiException $exception) {
    error_log($exception->apiCode() . ' request_id=' . $exception->requestId());
}

use Mupay\Sdk\Mupay;

$mupay = Mupay::test(getenv('MUPAY_API_KEY'));

use MuPag\Sdk\MuPagClient;