PHP code example of forecho / coinbase

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

    

forecho / coinbase example snippets


return [
    'apiKey' => env('COINBASE_API_KEY'),
    'apiVersion' => env('COINBASE_API_VERSION'),
    
    'webhookSecret' => env('COINBASE_WEBHOOK_SECRET'),
    'webhookJobs' => [
        // 'charge:created' => \App\Jobs\CoinbaseWebhooks\HandleCreatedCharge::class,
        // 'charge:confirmed' => \App\Jobs\CoinbaseWebhooks\HandleConfirmedCharge::class,
        // 'charge:failed' => \App\Jobs\CoinbaseWebhooks\HandleFailedCharge::class,
        // 'charge:delayed' => \App\Jobs\CoinbaseWebhooks\HandleDelayedCharge::class,
        // 'charge:pending' => \App\Jobs\CoinbaseWebhooks\HandlePendingCharge::class,
        // 'charge:resolved' => \App\Jobs\CoinbaseWebhooks\HandleResolvedCharge::class,
    ],
    'webhookModel' => Forecho\Coinbase\Models\CoinbaseWebhookCall::class,
];


$charges = Coinbase::getCharges();

$charge = Coinbase::createCharge([
    'name' => 'Name',
    'description' => 'Description',
    'local_price' => [
        'amount' => 100,
        'currency' => 'USD',
    ],
    'pricing_type' => 'fixed_price',
]);

$charge = Coinbase::getCharge($chargeId);

$charge = Coinbase::cancelCharge($chargeId);

$charge = Coinbase::resolveCharge($chargeId);

$checkouts = Coinbase::getCheckouts();

$checkout = Coinbase::createCheckout([
    'name' => 'Name',
    'description' => 'Description',
    'local_price' => [
        'amount' => 100,
        'currency' => 'USD',
    ],
    'pricing_type' => 'fixed_price',
]);

$checkout = Coinbase::getCheckout($checkoutId);

$checkout = Coinbase::updateCheckout($checkoutId, [
    'name' => 'New Name',
    'description' => 'New Description',
    'local_price' => [
        'amount' => 200,
        'currency' => 'USD',
    ],
    'requested_info' => [
        'name',
    ],
]);

$checkout = Coinbase::deleteCheckout($checkoutId);

$invoices = Coinbase::getInvoices();

$invoice = Coinbase::createInvoice([
    'business_name' => 'Business Name',
    'customer_email' => '[email protected]',
    'customer_name' => 'Customer Name',
    'local_price' => [
        'amount' => 100,
        'currency' => 'USD',
    ],
    'memo' => 'A memo/description for the invoice',
]);

$invoice = Coinbase::getInvoice($invoiceId);

$invoice = Coinbase::voidInvoice($invoiceId);

$invoice = Coinbase::resolveInvoice($invoiceId);

$events = Coinbase::getEvents();

$event = Coinbase::getEvent($eventId);



namespace App\Jobs\CoinbaseWebhooks;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Forecho\Coinbase\Models\CoinbaseWebhookCall;

class HandleCreatedCharge implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;
    
    /** @var \Forecho\Coinbase\Models\CoinbaseWebhookCall */
    public $webhookCall;

    public function __construct(CoinbaseWebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        // do your work here
        
        // you can access the payload of the webhook call with `$this->webhookCall->payload`
    }
}

// config/coinbase.php

'jobs' => [
    'charge:created' => \App\Jobs\CoinbaseWebhooks\HandleCreatedCharge::class,
],

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'coinbase::charge:created' => [
        App\Listeners\ChargeCreatedListener::class,
    ],
];



namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Forecho\Coinbase\Models\CoinbaseWebhookCall;

class ChargeCreatedListener implements ShouldQueue
{
    public function handle(CoinbaseWebhookCall $webhookCall)
    {
        // do your work here

        // you can access the payload of the webhook call with `$webhookCall->payload`
    }   
}

use Forecho\Coinbase\Models\CoinbaseWebhookCall;

CoinbaseWebhookCall::find($id)->process();

use Forecho\Coinbase\Models\CoinbaseWebhookCall;

class MyCustomWebhookCall extends CoinbaseWebhookCall
{
    public function process()
    {
        // do some custom stuff beforehand
        
        parent::process();
        
        // do some custom stuff afterwards
    }
}
bash
php artisan vendor:publish --provider="Forecho\Coinbase\CoinbaseServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Forecho\Coinbase\CoinbaseServiceProvider" --tag="migrations"
bash
php artisan migrate