PHP code example of yoco / yoco-php

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

    

yoco / yoco-php example snippets





// variables from your HTTP request
$token = $_POST['token']; // the token generated by the frontend API
$amountInCents = $_POST['amountInCents']; // the amount (in cents to be charged)
$currency = $_POST['currency']; // the currency of the amount

// Initialize the client with your keys.
$client = new \Yoco\YocoClient('your_secret_key', 'your_public_key');

try{
    // Charge the card with the YocoClient
    $client->charge($token, $amountInCents, $currency)
} catch (\Yoco\Exceptions\DeclinedException $e) {
    // Catch the declined exception
    error_log("Failed to charge card with token $token, amount $currency $amountInCents : " . $e->getMessage());
    // Inform the requester if the bad request and pass the error back
    Header("HTTP/1.1 400 Bad Request");
    print(json_encode(['charge_error' => $e]));
    exit;
} catch (\Yoco\Exceptions\InternalException $e) {
    // Catch the general exception
    error_log("Failed to charge card with token $token, amount $currency $amountInCents : " . $e->getMessage());
    // Inform the requester if the bad request and pass the error back
    Header("HTTP/1.1 400 Bad Request");
    print(json_encode(['charge_error' => $e]));
    exit;
}

bash
composer