Skip to content

Commit f02eac2

Browse files
Invoices, Bills, Rates & Currencies resources fully implemented, Readme updated
* Invoices, Bills, Rates & Currencies resources fully implemented, Readme updated
1 parent 87c42b8 commit f02eac2

File tree

5 files changed

+574
-30
lines changed

5 files changed

+574
-30
lines changed

README.md

Lines changed: 265 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,50 @@
55
[![Quality Score](https://img.shields.io/scrutinizer/g/vrajroham/laravel-bitpay.svg?style=for-the-badge)](https://scrutinizer-ci.com/g/vrajroham/laravel-bitpay)
66
[![Total Downloads](https://img.shields.io/packagist/dt/vrajroham/laravel-bitpay.svg?style=for-the-badge)](https://packagist.org/packages/vrajroham/laravel-bitpay)
77

8-
Accept Bitcoin and Bitcoin Cash for your business with your Laravel application and BitPay client.
8+
LaravelBitpay enables you and your business to transact in Bitcoin, Bitcoin Cash and 10+ other BitPay-supported cryptocurrencies within your Laravel application.
99

1010
> Requires PHP ^7.3
1111
12+
### Supported Resources
13+
14+
- :white_check_mark: [Invoices](https://bitpay.com/api/#rest-api-resources-invoices)
15+
- :hourglass_flowing_sand: [Settlements](https://bitpay.com/api/#rest-api-resources-settlements)
16+
- :hourglass_flowing_sand: [Ledgers](https://bitpay.com/api/#rest-api-resources-ledgers)
17+
- :hourglass_flowing_sand: [Recipients](https://bitpay.com/api/#rest-api-resources-recipients)
18+
- :hourglass_flowing_sand: [Payouts](https://bitpay.com/api/#rest-api-resources-payouts)
19+
- :white_check_mark: [Bills](https://bitpay.com/api/#rest-api-resources-bills)
20+
- :hourglass_flowing_sand: [Subscriptions](https://bitpay.com/api/#rest-api-resources-subscriptions)
21+
- :white_check_mark: [Rates](https://bitpay.com/api/#rest-api-resources-rates)
22+
- :hourglass_flowing_sand: [Sessions](https://bitpay.com/api/#rest-api-resources-sessions)
23+
- :white_check_mark: [Currencies](https://bitpay.com/api/#rest-api-resources-currencies)
24+
1225
## Contents
26+
1327
- [Installation](#installation)
1428
+ [Install Package](#install-package)
1529
+ [Publish config file](#publish-config-file)
1630
+ [Add configuration values](#add-configuration-values)
1731
+ [Add webhook event listener](#add-webhook-event-listener)
1832
+ [Connect to server and authenticate the client](#connect-to-server-and-authenticate-the-client)
1933
- [Examples](#examples)
20-
+ [Create Invoice and checkout (step wise)](#create-invoice-and-checkout)
34+
+ [Invoices](#invoices)
35+
+ [Create Invoice and checkout (step wise)](#create-invoice-and-checkout)
36+
+ [Retrieve an existing invoice](#retrieve-an-existing-invoice)
37+
+ [Retrieve a list of existing invoices](#retrieve-a-list-of-existing-invoices)
38+
+ [Refund an invoice](#refund-an-invoice)
39+
+ [Retrieve a refund request](#retrieve-a-refund-request)
40+
+ [Retrieve all refund requests on an invoice](#retrieve-all-refund-requests-on-an-invoice)
41+
+ [Cancel a refund request](#cancel-a-refund-request)
42+
+ [Bills](#bills)
43+
+ [Create a bill](#create-a-bill)
44+
+ [Retrieve a bill](#retrieve-a-bill)
45+
+ [Retrieve a list of existing bills](#retrieve-a-list-of-existing-bills)
46+
+ [Update a bill](#update-a-bill)
47+
+ [Deliver a bill via email](#deliver-a-bill-via-email)
48+
+ [Currencies](#currencies)
49+
+ [Retrieve the supported currencies](#retrieve-the-supported-currencies)
50+
+ [Rates](#rates)
51+
+ [Retrieve the exchange rate table maintained by BitPay](#retrieve-the-exchange-rate-table-maintained-by-bitpay)
2152
- [Changelog](#changelog)
2253
- [Contributing](#contributing)
2354
- [Security](#security)
@@ -26,22 +57,26 @@ Accept Bitcoin and Bitcoin Cash for your business with your Laravel application
2657

2758
## Installation
2859

29-
#### Install package
60+
### Install package
61+
3062
You can install the package via composer:
3163

3264
```bash
3365
composer require vrajroham/laravel-bitpay
3466
```
3567

36-
#### Publish config file
68+
### Publish config file
69+
3770
Publish config file with:
3871

3972
```bash
4073
php artisan vendor:publish --provider="Vrajroham\LaravelBitpay\LaravelBitpayServiceProvider"
4174
```
4275

43-
#### Add configuration values
44-
Add following keys to `.env` file and updated the details ([view more about configuration](https://support.bitpay.com/hc/en-us/articles/115003001063-How-do-I-configure-the-PHP-BitPay-Client-Library-)):
76+
### Add configuration values
77+
78+
Add following keys to `.env` file and updated the
79+
details ([view more about configuration](https://support.bitpay.com/hc/en-us/articles/115003001063-How-do-I-configure-the-PHP-BitPay-Client-Library-)):
4580

4681
```dotenv
4782
BITPAY_PRIVATE_KEY_PATH=/tmp/bitpay.pri
@@ -51,10 +86,14 @@ BITPAY_KEY_STORAGE_PASSWORD=SomeRandomePasswordForKeypairEncryption
5186
BITPAY_TOKEN=
5287
```
5388

54-
#### Add webhook event listener
55-
By default, package is capable of handling of webhook requests. Bitpay payment status updates are completely based on webhooks. Whenever webhook is received from server, `BitpayWebhookReceived` event is dispatched. You just need to provide a listener for this event.
89+
### Add webhook event listener
90+
91+
By default, package is capable of handling of webhook requests. Bitpay payment status updates are completely based on
92+
webhooks. Whenever webhook is received from server, `BitpayWebhookReceived` event is dispatched. You just need to
93+
provide a listener for this event.
5694

5795
You can add your listener as below,
96+
5897
```php
5998
<?php
6099

@@ -111,34 +150,44 @@ class EventServiceProvider extends ServiceProvider{
111150
}
112151
```
113152

114-
## Connect to server and authenticate the client
153+
### Connect to server and authenticate the client
115154

116155
- Create keypairs and pair your client(application) with BitPay server.
117156

118157
```bash
119158
php artisan laravel-bitpay:createkeypair
120159
```
121-
160+
122161
<p align="center"><a href="https://ibb.co/s9Z5jD6"><img src="https://i.ibb.co/DfZG468/Screenshot-2019-11-03-at-7-59-55-PM.png" alt="Screenshot-2019-11-03-at-7-59-55-PM" border="0"></a></p>
123162

124163
- What exactly above command do?
125-
+ Above command will create **Private and Public key**, encrypt your private key using bitpay secure storage class using your provided password.
126-
+ SIN (Service Identification Number) for your client will be created to uniquely identify requests from your server.
127-
+ By using SIN **new Token and Pairing Code** will be created for your client on bitpay server and will be shown on your console output.
164+
+ Above command will create **Private and Public key**, encrypt your private key using bitpay secure storage class
165+
using your provided password.
166+
+ SIN (Service Identification Number) for your client will be created to uniquely identify requests from your
167+
server.
168+
+ By using SIN **new Token and Pairing Code** will be created for your client on bitpay server and will be shown on
169+
your console output.
128170
+ Token will be used for all future request to bitpay and will automatically be copied to your `.env` file.
129-
+ Based on environment you set **TEST/LIVE**, command will provide URL to approve your client, and then you need to copy and search Pairing Code on bitpay server & approve it.
171+
+ Based on environment you set **TEST/LIVE**, command will provide URL to approve your client, and then you need to
172+
copy and search Pairing Code on bitpay server & approve it.
130173

131174
- You are all set. :golf:
132175

133-
### Examples
176+
## Examples
177+
178+
### Invoices
134179

135-
##### Create Invoice and checkout
180+
Invoices are time-sensitive payment requests addressed to specific buyers. An invoice has a fixed price, typically
181+
denominated in fiat currency. It also has an equivalent price in the supported cryptocurrencies, calculated by BitPay,
182+
at a locked exchange rate with an expiration time of 15 minutes.
183+
184+
#### Create Invoice and checkout
136185

137186
Let's go step by step.
138187
139188
- Create your internal system order and then initiate the workflow by creating bitpay invoice as below,
140189
141-
``` php
190+
```php
142191
use Illuminate\Support\Facades\Redirect;
143192
use Vrajroham\LaravelBitpay\LaravelBitpay;
144193
@@ -166,7 +215,7 @@ public function createInvoice()
166215
$invoice->setBuyer($buyer);
167216
168217
// Set currency
169-
$invoice->setCurrency('USD');
218+
$invoice->setCurrency(Currency::USD); // Always use the BitPay Currency model to prevent typos
170219
171220
// Set redirect url to get back after completing the payment. GET Request
172221
$invoice->setRedirectURL(route('bitpay-redirect-back'));
@@ -189,18 +238,215 @@ public function createInvoice()
189238
}
190239
```
191240
192-
- Once you get the invoice url for payment, redirect user to that particular url. Use will see something like below on browser.
241+
- Once you get the invoice url for payment, redirect user to that particular url. Use will see something like below on
242+
browser.
193243
194244
<p align="center"><a href="https://ibb.co/X8JhftX"><img src="https://i.ibb.co/FV7Skz6/Screenshot-2019-11-03-at-5-31-33-PM.png" alt="Screenshot-2019-11-03-at-5-31-33-PM" border="0"></a></p>
195245
196246
- Next, open your bitpay wallet, scan the code and make a payment. Something like below,
247+
197248
<p align="center"><a href="https://ibb.co/FY4G4gZ"><img src="https://i.ibb.co/WzvSvg8/IMG-3639.png" alt="IMG-3639" border="1"></a></p>
198249
199250
- Once payment is done, success screen will be displayed and user needs to click on **Return to Shop Name**.
251+
200252
<p align="center"><a href="https://ibb.co/8M21RBv"><img src="https://i.ibb.co/Jn2Dbd6/Screenshot-2019-11-03-at-5-32-05-PM.png" alt="Screenshot-2019-11-03-at-5-32-05-PM" border="0"></a></p>
201253
202254
- Payment done! Now you need to wait for webhook to get notification regarding status of payment.
203255
256+
#### Retrieve an existing invoice
257+
258+
```php
259+
$invoice = LaravelBitpay::getInvoice('invoiceId_sGsdVsgheF');
260+
```
261+
262+
#### Retrieve a list of existing invoices
263+
264+
In this example we retrieve all MTD (Month-To-Date) invoices:
265+
266+
```php
267+
$startDate = date('Y-m-d', strtotime('first day of this month'));
268+
$endDate = date('Y-m-d');
269+
270+
$invoices = LaravelBitpay::getInvoices($startDate, $endDate);
271+
```
272+
273+
#### Refund an invoice
274+
275+
The item Jane purchased was dead on arrival. Give back the lady her crypto:
276+
277+
```php
278+
$invoice = LaravelBitpay::getInvoice('invoiceId_sGsdVsgheF');
279+
280+
$refundRequested = LaravelBitpay::createRefund($invoice, '[email protected]', 0.016, 'ETH');
281+
282+
if ($refundRequested) {
283+
// Don't just sit there. Do something!
284+
}
285+
```
286+
287+
#### Retrieve a refund request
288+
289+
Let's periodically retrieve (and check the status of) Jane's refund request:
290+
291+
```php
292+
$invoice = LaravelBitpay::getInvoice('invoiceId_sGsdVsgheF');
293+
294+
$refund = LaravelBitpay::getRefund($invoice, 'refundId_pUdhjwGjsg');
295+
```
296+
297+
#### Retrieve all refund requests on an invoice
298+
299+
In this example we retrieve all refund requests related to Jane's invoice:
300+
301+
```php
302+
$invoice = LaravelBitpay::getInvoice('invoiceId_sGsdVsgheF');
303+
304+
$refundRequests = LaravelBitpay::getRefunds($invoice);
305+
```
306+
307+
#### Cancel a refund request
308+
309+
Turns out Jane didn't initially follow the instruction manual. The item works and she no longer wants a refund:
310+
311+
```php
312+
$invoice = LaravelBitpay::getInvoice('invoiceId_sGsdVsgheF');
313+
314+
$refundRequestCancelled = LaravelBitpay::cancelRefund($invoice->getId(), 'refundId_pUdhjwGjsg');
315+
```
316+
317+
### Bills
318+
319+
Bills are payment requests addressed to specific buyers. Bill line items have fixed prices, typically denominated in
320+
fiat currency.
321+
322+
#### Create a bill
323+
324+
In the following example, we create a bill that's due in 10 days:
325+
326+
```php
327+
// Initialize Bill
328+
$billData = LaravelBitpay::Bill();
329+
$billData->setNumber('bill1234-EFGH');
330+
$billData->setCurrency(Currency::USD); // Always use the BitPay Currency model to prevent typos
331+
$dueDate = date('Y-m-d\TH:i:s\Z', strtotime('+10 days')); // ISO-8601 formatted date
332+
$billData->setDueDate($dueDate);
333+
$billData->setPassProcessingFee(true); // Let the recipient shoulder BitPay's processing fee
334+
335+
// Prepare Bill recipient's data
336+
$billData->setName('John Doe');
337+
$billData->setAddress1('2630 Hegal Place');
338+
$billData->setAddress2('Apt 42');
339+
$billData->setCity('Alexandria');
340+
$billData->setState('VA');
341+
$billData->setZip(23242);
342+
$billData->setCountry('US');
343+
$billData->setEmail('[email protected]');
344+
$billData->setCc(['[email protected]']);
345+
$billData->setPhone('555-123-456');
346+
347+
// Prepare Bill's line item(s)
348+
$itemUno = LaravelBitpay::Item();
349+
$itemUno->setDescription('Squid Game "Front Man" Costume');
350+
$itemUno->setPrice(49.99);
351+
$itemUno->setQuantity(2);
352+
353+
$itemDos = LaravelBitpay::Item();
354+
$itemDos->setDescription('GOT "House Stark" Sterling Silver Pendant');
355+
$itemDos->setPrice(35);
356+
$itemDos->setQuantity(1);
357+
358+
$billData->setItems([$itemUno, $itemDos]);
359+
360+
// Create Bill
361+
$bill = LaravelBitpay::createBill($billData);
362+
363+
// Store the Bill's BitPay ID and URL for future reference
364+
$billId = $bill->getId();
365+
$billPaymentUrl = $bill->getUrl();
366+
367+
// OR
368+
369+
// Redirect the recipient to BitPay's hosted Bill payment page
370+
Redirect::to($billPaymentUrl);
371+
```
372+
373+
#### Retrieve a bill
374+
375+
```php
376+
$bill = LaravelBitpay::getBill('bill1234-EFGH');
377+
```
378+
379+
#### Retrieve a list of existing bills
380+
381+
You can narrow down the retrieved list by specifying a Bill status:
382+
383+
```php
384+
// Status can be "draft", "sent", "new", "paid", or "complete"
385+
$status = 'paid';
386+
387+
$paidBills = LaravelBitpay::getBills($status);
388+
```
389+
390+
#### Update a bill
391+
392+
We managed to upsell a product to our client. Let's add an extra line item to their existing Bill:
393+
394+
```php
395+
$existingBill = LaravelBitpay::getBill('bill1234-EFGH');
396+
$existingItems = $existingBill->getItems();
397+
398+
$billData = LaravelBitpay::Bill();
399+
400+
$itemTres = LaravelBitpay::Item();
401+
$itemTres->setDescription('The Tomorrow War "White Spike" Life-Size Wax Figure');
402+
$itemTres->setPrice(189.99);
403+
$itemTres->setQuantity(1);
404+
405+
$billData->setItems(array_merge($existingItems, [$itemTres]));
406+
407+
// Update Bill
408+
$updatedBill = LaravelBitpay::updateBill($billData, 'bill1234-EFGH');
409+
```
410+
411+
#### Deliver a bill via email
412+
413+
```php
414+
$bill = LaravelBitpay::getBill('bill1234-EFGH');
415+
416+
$billId = $bill->getId();
417+
$billToken = $bill->getToken();
418+
419+
$billDelivery = json_decode(LaravelBitpay::deliverBill($billId, $billToken));
420+
421+
if (isset($billDelivery->data) && $billDelivery->data == 'Success') {
422+
// Bill delivered successfully. Do something about that... or not.
423+
}
424+
```
425+
426+
### Currencies
427+
428+
Currencies are fiat currencies supported by BitPay.
429+
430+
#### Retrieve the supported currencies
431+
432+
In this example, we retrieve the list of supported BitPay Currency objects.
433+
434+
```php
435+
$supportedCurrencies = LaravelBitpay::getCurrencies();
436+
```
437+
438+
### Rates
439+
440+
Rates are exchange rates, representing the number of fiat currency units equivalent to one BTC.
441+
442+
#### Retrieve the exchange rate table maintained by BitPay
443+
444+
```php
445+
$rates = LaravelBitpay::getRates();
446+
447+
$btcToUsdRate = $rates->getRate(Currency::USD); // Always use the BitPay Currency model to prevent typos
448+
```
449+
204450
## Changelog
205451

206452
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

0 commit comments

Comments
 (0)