PHP code example of joeymckenzie / givebutter-php

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

    

joeymckenzie / givebutter-php example snippets


/** @var string $apiKey */
$apiKey = $_ENV['GIVEBUTTER_API_KEY'];
$client = Givebutter::client($apiKey);

// Create a campaign
$createdCampaign = $client
    ->campaigns()
    ->create([
        'description' => 'This is a test campaign.',
        'end_at' => CarbonImmutable::now()->toIso8601String(),
        'goal' => 1000,
        'subtitle' => 'subtitle',
        'slug' => md5(uniqid('', true)),
        'title' => 'title',
        'type' => 'collect',
    ]);

// Get a campaign
$campaign = $client
    ->campaigns()
    ->get(441507);

// Get all campaigns
$campaigns = $client
    ->campaigns()
    ->list();

// Update a campaign
$updatedCampaign = $client
    ->campaigns()
    ->update($campaign->id, [
        'description' => 'This is a test campaign.',
        'goal' => 1500,
    ]);

// Delete a campaign
$deleteResponse = $client
    ->campaigns()
    ->delete($campaign->id);

$createdCampaign = $client
    ->campaigns()
    ->create([
        'description' => 'This is a test campaign.',
        'end_at' => CarbonImmutable::now()->toIso8601String(),
        'goal' => 1000,
        'subtitle' => 'subtitle',
        'slug' => md5(uniqid('', true)),
        'title' => 'title',
        'type' => 'collect',
    ]);

if ($createdCampaign->hasErrors()) {
    // Do some error handling...
} else {
    // Safely dereference response properties
    assert($campaign->id !== null);

    // Update a campaign
    $updatedCampaign = $client
        ->campaigns()
        ->update($createdCampaign->id, [
            'description' => 'This is another test campaign.',
            'goal' => 1500,
        ]);
}

$response = $client
    ->campaigns()
    ->create([
        'title' => 'Campaign title',
        'description' => 'Campaign description.',
        'end_at' => CarbonImmutable::now()->toIso8601String(),
        'goal' => 10000,
        'subtitle' => 'Campaign subtitle',
        'slug' => 'campaignSlug123',
        'type' => 'collect',
    ]);

echo $response->data(); // GetCampaignResponse::class
echo $response->id; // 42
echo $response->title; // 'Campaign title'
echo $response->goal; // 10000
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->campaigns()
    ->list();

echo $response->data(); // array<int, GetCampaignResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 42, ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->campaigns()
    ->get(42);

echo $response->data(); // GetCampaignResponse::class
echo $response->id; // 42
echo $response->title; // 'Campaign title'
echo $response->goal; // 10000
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->campaigns()
    ->update(42, [
        'description' => 'Updated campaign description.',
        'goal' => 20000,
    ]);

echo $response->data(); // GetCampaignResponse::class
echo $response->id; // 42
echo $response->title; // 'Campaign title'
echo $response->goal; // 20000
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->campaigns()
    ->delete(42);

echo $response->getStatusCode(); // 200

$response = $client
    ->campaigns()
    ->members()
    ->list(123);

echo $response->data(); // array<int, GetCampaignMembersResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 123, ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->campaigns()
    ->members()
    ->get(123, 42);

echo $response->data(); // GetCampaignMemberResponse::class
echo $response->id; // 123
echo $response->firstName; // 'John'
echo $response->lastName; // 'Smith'
echo $response->raised; // 10000
echo $response->toArray(); // ['id' => 123, ...]

$response = $client
    ->campaigns()
    ->members()
    ->delete(123, 42);

echo $response->getStatusCode(); // 200

$response = $client
    ->campaigns()
    ->teams()
    ->list(123);

echo $response->data(); // array<int, GetCampaignTeamsResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 123, ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->campaigns()
    ->teams()
    ->get(123, 42);

echo $response->data(); // GetCampaignTeamResponse::class
echo $response->id; // 123
echo $response->name; // 'Team 1'
echo $response->logo; // 'https://domain.com/photo123'
echo $response->raised; // 10000
echo $response->toArray(); // ['id' => 123, ...]

$response = $client
    ->contacts()
    ->create([
        'first_name' => 'Michael',
        'middle_name' => 'Gary',
        'last_name' => 'Scott',
        'email' => [
            [
                'type' => 'work',
                'value' => '[email protected]',
            ],
        ],
        'phones' => [
            [
                'type' => 'work',
                'value' => '+15303567734',
            ],
        ],
        'addresses' => [
            [
                'address_1' => '123 Paper St.',
                'city' => 'Scranton',
                'state' => 'PA',
                'zipcode' => '18507',
                'country' => 'US',
            ],
        ],
        'tags' => [
            'paper',
            'dunder mifflin',
        ],
        'dob' => '03/15/1965',
        'company' => 'Dunder Mifflin',
        'title' => 'Regional Manager',
        'twitter_url' => 'https://twitter.com/dundermifflin',
        'linkedin_url' => 'https://linkedin.com/in/dundermifflin',
        'facebook_url' => 'https://facebook.com/dundermifflin',
    ]);

echo $response->data(); // GetContactResponse::class
echo $response->id; // 42
echo $response->firstName; // 'Michael'
echo $response->lastName; // 'Scott'
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->contacts()
    ->list();

echo $response->data(); // array<int, GetContactResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 42, ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->contact()
    ->get(42);

echo $response->data(); // GetContactResponse::class
echo $response->id; // 42
echo $response->firstName; // 'Michael'
echo $response->lastName; // 'Scott'
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->campaigns()
    ->update(42, [
        'first_name' => 'Michael',
        'last_name' => 'Scarn',
        'company' => 'CIA',
        'title' => 'Secret Agent'
    ]);

echo $response->data(); // GetContactResponse::class
echo $response->firstName; // 'Michael'
echo $response->lastName; // 'Scarn'
echo $response->company; // 'CIA'
echo $response->title; // 'Secret Agent'
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->contacts()
    ->archive(42);

echo $response->getStatusCode(); // 200

$response = $client
    ->contacts()
    ->restore(42);

echo $response->data(); // GetContactResponse::class
echo $response->id; // 42
echo $response->firstName; // 'Michael'
echo $response->lastName; // 'Scott'
echo $response->toArray(); // ['id' => 42, ...]

$response = $client
    ->tickets()
    ->list();

echo $response->data(); // array<int, GetTicketsResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 'abc123', ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->tickets()
    ->get('ab123');

echo $response->data(); // GetTicketResponse::class
echo $response->firstName; // 'Michael'
echo $response->lastName; // 'Scott'
echo $response->price; // 100
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->transactions()
    ->create([
        'amount' => 100.00,
        'method' => 'cash',
        'transacted_at' => CarbonImmutable::now()->addHour()->format('m/d/y'),
        'campaign_code' => 'DEF456',
        'first_name' => 'Micahel',
        'last_name' => 'Scott',
    ]);

echo $response->data(); // GetTransactionResponse::class
echo $response->amount; // 100.00
echo $response->method; // 'cash'
echo $response->campaignCode; // 'DEF456'
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->transactions()
    ->list();

echo $response->data(); // array<int, GetTransactionsResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 'abc123', ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->transactions()
    ->get('ab123');

echo $response->data(); // GetTransactionResponse::class
echo $response->amount; // 100.00
echo $response->method; // 'cash'
echo $response->campaignCode; // 'DEF456'
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->payouts()
    ->list();

echo $response->data(); // array<int, GetPayoutResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 'abc123', ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->payout()
    ->get('ab123');

echo $response->data(); // GetPayoutResponse::class
echo $response->amount; // 100.00
echo $response->fee; // 3.00
echo $response->tip; // 1.00
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->plans()
    ->list();

echo $response->data(); // array<int, GetPlanResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 'abc123', ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->plans()
    ->get('ab123');

echo $response->data(); // GetPlanResponse::class
echo $response->amount; // 100.00
echo $response->firstName; // 'Michael'
echo $response->lastName; // 'Scarn'
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->funds()
    ->create("Scott's Tots");

echo $response->data(); // GetFundResponse::class
echo $response->id; // 'abc123'
echo $response->name; // "Scott's Tots"
echo $response->supporters; // 0
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->funds()
    ->list();

echo $response->data(); // array<int, GetFundsResponse::class>
echo $response->meta; // Meta::class
echo $response->links; // Links::class
echo $response->toArray(); // ['data' => ['id' => 'abc123', ...], 'meta' => [...], 'links' => [...]]

$response = $client
    ->funds()
    ->get('abc123');

echo $response->data(); // GetFundResponse::class
echo $response->id; // 'abc123'
echo $response->name; // "Scott's Tots"
echo $response->supporters; // 0
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->campaigns()
    ->update('abc123', "Scott's Tots", "ST");

echo $response->data(); // GetFundResponse::class
echo $response->id; // 'abc123'
echo $response->name; // "Scott's Tots"
echo $response->code; // 'ST'
echo $response->supporters; // 0
echo $response->toArray(); // ['id' => 'abc123', ...]

$response = $client
    ->funds()
    ->delete('abc123');

echo $response->getStatusCode(); // 200

use Givebutter\Testing\ClientFake;
use Givebutter\Responses\Campaigns\GetCampaignResponse;

$fake = new ClientFake([
    GetCampaignResponse::fake(GetCampaignFixture::class),
]);

$campaign = $fake
    ->campaigns()
    ->create([
        'description' => 'This is a test campaign.',
        'end_at' => CarbonImmutable::now()->toIso8601String(),
        'goal' => 1000,
        'subtitle' => 'subtitle',
        'slug' => md5(uniqid('', true)),
        'title' => 'title',
        'type' => 'collect',
    ]);

expect($campaign->description)->toBe('This is a test campaign.');

// Assert completion create request was sent
$fake->assertSent(CampaignsResource::class, function (string $method, array $parameters): bool {
    return $method === 'create' &&
        $parameters[0]['title'] === 'title' &&
        $parameters[0]['description'] === 'This is a test campaign.';
});

// Assert 2 completion create requests were sent
$fake->assertSent(CampaignsResource::class, 2);

// Assert no completion create requests were sent
$fake->assertNotSent(CampaignsResource::class);

// Assert no requests were sent
$fake->assertNothingSent();

$fake = new ClientFake([
    new Exception('Oops, something bad happened!')
]);

// the `Exception` will be thrown
$campaign = $fake
    ->campaigns()
    ->create([
        'description' => 'This is a test campaign.',
        'end_at' => CarbonImmutable::now()->toIso8601String(),
        'goal' => 1000,
        'subtitle' => 'subtitle',
        'slug' => md5(uniqid('', true)),
        'title' => 'title',
        'type' => 'collect',
    ]);
bash
composer