PHP code example of sonnenglas / yoco-php-sdk

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

    

sonnenglas / yoco-php-sdk example snippets


use Sonnenglas\Yoco\Client;
use Sonnenglas\Yoco\Dto\CreateCheckoutRequest;

$client = new Client(secretKey: getenv('YOCO_SECRET_KEY'));

$checkout = $client->checkouts()->create(new CreateCheckoutRequest(
    amount:     10000,                          // 100.00 ZAR — Yoco amounts are in cents
    currency:   'ZAR',
    successUrl: 'https://example.com/success',
    cancelUrl:  'https://example.com/cancel',
    metadata:   ['orderNumber' => 'ORD-100'],
));

header('Location: '.$checkout->redirectUrl);   // send the customer to Yoco

use Sonnenglas\Yoco\Webhook\SignatureVerifier;
use Sonnenglas\Yoco\Exceptions\SignatureVerificationException;

$verifier = new SignatureVerifier(getenv('YOCO_WEBHOOK_SECRET')); // whsec_...

try {
    $event = $verifier->verify(file_get_contents('php://input'), getallheaders());
} catch (SignatureVerificationException $e) {
    http_response_code(401);
    exit;
}

// $event->type === 'payment.succeeded' | 'payment.failed'
// $event->payload['metadata']['orderNumber']

$refund = $client->checkouts()->refund(
    checkoutId: 'ch_9LVKD8GnAj7f39DFbn4F16bE',
    amount:     2500,         // partial refund of 25.00 ZAR; omit for full refund
);

echo $refund->status;         // 'created' | 'succeeded' | ...
bash
composer