Skip to content

Commit

Permalink
SP-750 Code Examples: PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
mwarzybok-sumoheavy committed Dec 21, 2023
1 parent 7a9c359 commit 6023da3
Show file tree
Hide file tree
Showing 19 changed files with 1,066 additions and 450 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"autoload": {
"psr-4": {
"BitPaySDK\\": "src/BitPaySDK"
"BitPaySDK\\": "src/BitPaySDK",
"BitPaySDKexamples\\": "examples"
}
},
"autoload-dev": {
Expand Down
727 changes: 303 additions & 424 deletions composer.lock

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions examples/ClientProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace BitPaySDKexamples;

use BitPaySDK\Client;
use BitPaySDK\PosClient;

class ClientProvider
{
public static function create(): Client
{
// to create private key, tokens & private key secret run setup/ConfigGenerator.php from CLI before

return Client::createWithFile(__DIR__ . '/myConfigFile.json');
}

public static function createPos(): Client
{
return new PosClient('myPosToken');
}
}
65 changes: 65 additions & 0 deletions examples/General/UseLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace BitPaySDKexamples\General;

use BitPaySDK\Logger\BitPayLogger;
use BitPaySDK\Logger\LoggerProvider;
use BitPaySDKexamples\ClientProvider;

final class UseLogger
{
/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function execute(): void
{
LoggerProvider::setLogger(
new class() implements BitPayLogger
{
public function logRequest(string $method, string $endpoint, ?string $json): void
{
echo $this->getLogMessage('Request', $method, $endpoint, $json);
$this->newLine();
}

public function logResponse(string $method, string $endpoint, ?string $json): void
{
echo $this->getLogMessage('Response', $method, $endpoint, $json);
$this->newLine();
}

public function logError(?string $message): void
{
echo $message;
$this->newLine();
}

private function getLogMessage(string $type, string $method, string $endpoint, ?string $json): string
{
$array = [
'type' => $type,
'method' => $method,
'endpoint' => $endpoint,
'json' => $json
];

return json_encode($array, JSON_THROW_ON_ERROR | JSON_ERROR_NONE);
}

private function newLine(): void
{
echo "\r\n";
}
}
);

// for monolog implementation you can use code from src/BitPaySDK/Logger/MonologLoggerExample.php

$client = ClientProvider::create();

$invoice = $client->getInvoiceByGuid("someGuid"); // requests/response will be logged
}
}
74 changes: 74 additions & 0 deletions examples/Merchant/BillRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace BitPaySDKexamples\Merchant;

use BitPaySDK\Model\Bill\Bill;
use BitPaySDK\Model\Bill\Item;
use BitPaySDKexamples\ClientProvider;

final class BillRequests
{
/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function createBill(): void
{
$client = ClientProvider::create();

$bill = new Bill(null, 'USD');
$bill->setName('someName');
$bill->setEmail('[email protected]');
$bill->setAddress1('SomeAddress');
$bill->setCity('MyCity');
// ...

$client->createBill($bill);
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function getBill(): void
{
$client = ClientProvider::create();

$bill = $client->getBill('someBillId');

$bills = $client->getBills('draft');
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function updateBill(): void
{
$client = ClientProvider::create();

$item = new Item();
$item->setPrice(12.34);
$item->setQuantity(5);
$item->setDescription('someDecription');

$bill = new Bill();
$bill->setEmail('[email protected]');
$bill->setItems([$item]);

$client->updateBill($bill, $bill->getId());
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function deliverBillViaEmail(): void
{
$client = ClientProvider::create();

$client->deliverBill('someBillId', 'myBillToken');
}
}
92 changes: 92 additions & 0 deletions examples/Merchant/InvoiceRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

declare(strict_types=1);

namespace BitPaySDKexamples\Merchant;

use BitPaySDK\Model\Invoice\Buyer;
use BitPaySDK\Model\Invoice\Invoice;
use BitPaySDKexamples\ClientProvider;

final class InvoiceRequests
{
/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function createInvoice(): void
{
$invoice = new Invoice(10.0, "USD");
$invoice->setFullNotifications(true);
$invoice->setExtendedNotifications(true);
$invoice->setNotificationURL("https://test/lJnJg9WW7MtG9GZlPVdj");
$invoice->setRedirectURL("https://test/lJnJg9WW7MtG9GZlPVdj");
$invoice->setNotificationEmail("[email protected]");
$invoice->setBuyerSms('+12223334445');

$buyer = new Buyer();
$buyer->setName("Test");
$buyer->setEmail("[email protected]");
$buyer->setAddress1("168 General Grove");
$buyer->setCountry("AD");
$buyer->setLocality("Port Horizon");
$buyer->setNotify(true);
$buyer->setPhone("+990123456789");
$buyer->setPostalCode("KY7 1TH");
$buyer->setRegion("New Port");

$invoice->setBuyer($buyer);

$client = ClientProvider::create();

$createdInvoice = $client->createInvoice($invoice);
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function getInvoice(): void
{
$client = ClientProvider::create();

$invoiceById = $client->getInvoice("myInvoiceId");
$invoiceByGuid = $client->getInvoiceByGuid("someGuid"); // we can add a GUID during the invoice creation
$invoices = $client->getInvoices("2023-04-14", "2023-04-17");
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function updateInvoice(): void
{
$client = ClientProvider::create();

$invoice = $client->updateInvoice("someId", "123321312", null, null);
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function cancelInvoice(): void
{
$client = ClientProvider::create();

$client->cancelInvoice('invoiceId');

$client->cancelInvoiceByGuid('invoiceGuid');
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function requestInvoiceWebhookToBeResent(): void
{
$client = ClientProvider::create();

$client->requestInvoiceNotification('someInvoiceId');
}
}
32 changes: 32 additions & 0 deletions examples/Merchant/LedgerRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace BitPaySDKexamples\Ledger;

use BitPaySDKexamples\ClientProvider;

final class LedgerRequests
{
/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function getLedgers(): void
{
$client = ClientProvider::create();

$ledgers = $client->getLedgers();
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function getLedgerEntries(): void
{
$client = ClientProvider::create();

$ledgerEntries = $client->getLedgerEntries('USD', '2023-08-14', '2023-08-22');
}
}
62 changes: 62 additions & 0 deletions examples/Merchant/RefundRequests.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace BitPaySDKexamples\Merchant;

use BitPaySDKexamples\ClientProvider;

final class RefundRequests
{
/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function createRefund(): void
{
$client = ClientProvider::create();

$refund = $client->createRefund("myInvoiceId", 12.34, "USD");
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
*/
public function updateRefund(): void
{
$client = ClientProvider::create();

$updatedRefund = $client->updateRefund('myRefundId','created');
$updatedRefundByGuid = $client->updateRefundByGuid('myRefundId','created');
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
* @throws \BitPaySDK\Exceptions\BitPayGenericException
*/
public function getRefund(): void
{
$client = ClientProvider::create();

$refund = $client->getRefund('someRefundId');
$refundByGuid = $client->getRefundByGuid('someGuid');
}

/**
* @throws \BitPaySDK\Exceptions\BitPayApiException
*/
public function cancelRefund(): void
{
$client = ClientProvider::create();

$cancelRefund = $client->cancelRefund('myRefundId');
$cancelRefundByGuid = $client->cancelRefundByGuid('someGuid');
}

public function requestRefundNotificationToBeResent(): void
{
$client = ClientProvider::create();

$client->sendRefundNotification('someRefundId');
}
}
Loading

0 comments on commit 6023da3

Please sign in to comment.