Skip to content

Commit c64d54c

Browse files
committed
Refactored implementation
1 parent f02eac2 commit c64d54c

11 files changed

+1297
-319
lines changed

.editorconfig

+871
Large diffs are not rendered by default.

CONTRIBUTING.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/vrajroham/laravel-bitpay).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ composer test
29+
```
30+
31+
32+
**Happy coding**!

src/Actions/ManageBills.php

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Vrajroham\LaravelBitpay\Actions;
4+
5+
use BitPaySDK\Model\Bill\Bill;
6+
7+
trait ManageBills
8+
{
9+
/**
10+
* Bills are payment requests addressed to specific buyers.
11+
* Bill line items have fixed prices, typically denominated in fiat currency.
12+
*
13+
* @link https://bitpay.com/api/#rest-api-resources-bills-resource
14+
* @return Bill
15+
*/
16+
public static function Bill(): Bill
17+
{
18+
return new Bill();
19+
}
20+
21+
/**
22+
* Create a BitPay Bill.
23+
*
24+
* @link https://bitpay.com/api/#rest-api-resources-bills-create-a-bill
25+
* @param $bill Bill A Bill object with request parameters defined.
26+
* @return Bill A BitPay generated Bill object.
27+
*/
28+
public static function createBill(Bill $bill): Bill
29+
{
30+
return (new self())->client->createBill($bill);
31+
}
32+
33+
/**
34+
* Retrieve a BitPay bill by its id.
35+
*
36+
* @link https://bitpay.com/api/#rest-api-resources-bills-retrieve-a-bill
37+
* @param $billId string The id of the bill to retrieve.
38+
* @return Bill A BitPay Bill object.
39+
*/
40+
public static function getBill(string $billId): Bill
41+
{
42+
return (new self())->client->getBill($billId);
43+
}
44+
45+
/**
46+
* Retrieve a collection of BitPay bills.
47+
*
48+
* @link https://bitpay.com/api/#rest-api-resources-bills-retrieve-bills-by-status
49+
* @param $status string|null The status to filter the bills.
50+
* @return array A list of BitPay Bill objects.
51+
*/
52+
public static function getBills(string $status = null): array
53+
{
54+
return (new self())->client->getBills($status);
55+
}
56+
57+
/**
58+
* Update a BitPay Bill.
59+
*
60+
* @link https://bitpay.com/api/#rest-api-resources-bills-update-a-bill
61+
* @param $bill Bill A Bill object with the parameters to update defined.
62+
* @param $billId string The ID of the Bill to update.
63+
* @return Bill An updated Bill object.
64+
*/
65+
public static function updateBill(Bill $bill, string $billId): Bill
66+
{
67+
return (new self())->client->updateBill($bill, $billId);
68+
}
69+
70+
/**
71+
* Deliver a BitPay Bill.
72+
*
73+
* @link https://bitpay.com/api/#rest-api-resources-bills-deliver-a-bill-via-email
74+
* @param $billId string The id of the requested bill.
75+
* @param $billToken string The token of the requested bill.
76+
* @return string A response status returned from the API.
77+
*/
78+
public static function deliverBill(string $billId, string $billToken): string
79+
{
80+
return (new self())->client->deliverBill($billId, $billToken);
81+
}
82+
}

src/Actions/ManageBuyers.php

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Vrajroham\LaravelBitpay\Actions;
4+
5+
use BitPaySDK\Model\Invoice\Buyer;
6+
7+
trait ManageBuyers
8+
{
9+
/**
10+
* @return Buyer
11+
*/
12+
public static function Buyer(): Buyer
13+
{
14+
return new Buyer();
15+
}
16+
}

src/Actions/ManageCurrencies.php

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace Vrajroham\LaravelBitpay\Actions;
4+
5+
use BitPaySDK\Model\Currency;
6+
7+
8+
trait ManageCurrencies
9+
{
10+
/**
11+
* @param string|null $code
12+
*
13+
* @return \BitpaySDK\Model\Currency
14+
* @link https://bitpay.com/api/#rest-api-resources-currencies
15+
*/
16+
public static function Currency(string $code = null): Currency
17+
{
18+
$currency = new Currency();
19+
if ($code) {
20+
$currency->setCode($code);
21+
}
22+
23+
return $currency;
24+
}
25+
26+
/**
27+
* Fetch the supported currencies.
28+
*
29+
* @link https://bitpay.com/api/#rest-api-resources-currencies-retrieve-the-supported-currencies
30+
* @return array A list of supported BitPay Currency objects.
31+
*/
32+
public static function getCurrencies(): array
33+
{
34+
return (new self())->client->getCurrencies();
35+
}
36+
37+
// TODO: Implement the following if/when upstream gets merged: https://github.com/bitpay/php-bitpay-client-v2/pull/67
38+
// /**
39+
// * Retrieve all the rates for a given cryptocurrency
40+
// *
41+
// * @link https://bitpay.com/api/#rest-api-resources-rates-retrieve-all-the-rates-for-a-given-cryptocurrency
42+
// * @param string $baseCurrency The cryptocurrency for which you want to fetch the rates.
43+
// * Current supported values are BTC, BCH, ETH, XRP, DOGE and LTC
44+
// * @return \BitPaySDK\Model\Rate\Rates A Rates object populated with the currency rates for the requested baseCurrency.
45+
// * @throws \BitPaySDK\Exceptions\BitPayException BitPayException class
46+
// */
47+
// public static function getCurrencyRates(string $baseCurrency): Rates
48+
// {
49+
// return (new self())->client->getCurrencyRates($baseCurrency);
50+
// }
51+
//
52+
// /**
53+
// * Retrieve the rate for a cryptocurrency / fiat pair
54+
// *
55+
// * @link https://bitpay.com/api/#rest-api-resources-rates-retrieve-the-rates-for-a-cryptocurrency-fiat-pair
56+
// * @param string $baseCurrency The cryptocurrency for which you want to fetch the fiat-equivalent rate.
57+
// * Current supported values are BTC, BCH, ETH, XRP, DOGE and LTC
58+
// * @param string $currency The fiat currency for which you want to fetch the baseCurrency rate
59+
// * @return \BitPaySDK\Model\Rate\Rate A Rate object populated with the currency rate for the requested baseCurrency.
60+
// * @throws \BitPaySDK\Exceptions\BitPayException BitPayException class
61+
// */
62+
// public static function getCurrencyPairRate(string $baseCurrency, string $currency): Rate
63+
// {
64+
// return (new self())->client->getCurrencyPairRate($baseCurrency, $currency);
65+
// }
66+
}

src/Actions/ManageInvoices.php

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Vrajroham\LaravelBitpay\Actions;
4+
5+
use BitPaySDK\Model\Invoice\Invoice;
6+
7+
trait ManageInvoices
8+
{
9+
/**
10+
* Get Bitpay Invoice instance.
11+
*
12+
* @link https://bitpay.com/api/#rest-api-resources-invoices-resource
13+
* @return Invoice
14+
*/
15+
public static function Invoice(): Invoice
16+
{
17+
return new Invoice();
18+
}
19+
20+
/**
21+
* Create a BitPay invoice.
22+
*
23+
* @link https://bitpay.com/api/#rest-api-resources-invoices-create-an-invoice
24+
* @param $invoice Invoice An Invoice object with request parameters defined.
25+
* @return Invoice $invoice A BitPay generated Invoice object.
26+
*/
27+
public static function createInvoice(Invoice $invoice): Invoice
28+
{
29+
if ('' == $invoice->getNotificationURL()) {
30+
$invoice->setNotificationURL(route('laravel-bitpay.webhook.capture'));
31+
}
32+
33+
return (new self())->client->createInvoice($invoice);
34+
}
35+
36+
/**
37+
* Retrieve a BitPay invoice by its id.
38+
*
39+
* @link https://bitpay.com/api/#rest-api-resources-invoices-retrieve-an-invoice
40+
* @param $invoiceId string The id of the invoice to retrieve.
41+
* @return Invoice A BitPay Invoice object.
42+
*/
43+
public static function getInvoice(string $invoiceId): Invoice
44+
{
45+
return (new self())->client->getInvoice($invoiceId);
46+
}
47+
48+
/**
49+
* Retrieve a collection of BitPay invoices.
50+
*
51+
* @link https://bitpay.com/api/#rest-api-resources-invoices-retrieve-invoices-filtered-by-query
52+
* @param $dateStart string The start of the date window to query for invoices. Format YYYY-MM-DD.
53+
* @param $dateEnd string The end of the date window to query for invoices. Format YYYY-MM-DD.
54+
* @param $status string|null The invoice status you want to query on.
55+
* @param $orderId string|null The optional order id specified at time of invoice creation.
56+
* @param $limit int|null Maximum results that the query will return (useful for paging results).
57+
* @param $offset int|null Number of results to offset (ex. skip 10 will give you results starting with the 11th result).
58+
* @return array A list of BitPay Invoice objects.
59+
*/
60+
public static function getInvoices(
61+
string $dateStart,
62+
string $dateEnd,
63+
string $status = null,
64+
string $orderId = null,
65+
int $limit = null,
66+
int $offset = null
67+
): array
68+
{
69+
return (new self())->client->getInvoices($dateStart, $dateEnd, $status, $orderId, $limit, $offset);
70+
}
71+
}

src/Actions/ManageItems.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Vrajroham\LaravelBitpay\Actions;
4+
5+
use BitPaySDK\Model\Bill\Item;
6+
7+
8+
trait ManageItems
9+
{
10+
/**
11+
* @return Item
12+
*/
13+
public static function Item(): Item
14+
{
15+
return new Item();
16+
}
17+
}

src/Actions/ManageRefunds.php

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
3+
namespace Vrajroham\LaravelBitpay\Actions;
4+
5+
use BitPaySDK\Model\Invoice\Invoice;
6+
use BitPaySDK\Model\Invoice\Refund;
7+
8+
trait ManageRefunds
9+
{
10+
/**
11+
* Get Bitpay refund instance.
12+
*
13+
* @return Refund
14+
*/
15+
public static function Refund(): Refund
16+
{
17+
return new Refund();
18+
}
19+
20+
/**
21+
* Create a BitPay refund.
22+
*
23+
* @link https://bitpay.com/api/#rest-api-resources-invoices-refund-an-invoice
24+
* @param $invoice Invoice A BitPay invoice object for which a refund request should be made. Must have
25+
* been obtained using the merchant facade.
26+
* @param $refundEmail string The email of the buyer to which the refund email will be sent
27+
* @param $amount float The amount of money to refund. If zero then a request for 100% of the invoice
28+
* value is created.
29+
* @param $currency string The three digit currency code specifying the exchange rate to use when
30+
* calculating the refund bitcoin amount. If this value is "BTC" then no exchange rate
31+
* calculation is performed.
32+
* @return bool True if the refund was successfully created, false otherwise.
33+
*/
34+
public static function createRefund(
35+
Invoice $invoice,
36+
string $refundEmail,
37+
float $amount,
38+
string $currency
39+
): bool
40+
{
41+
return (new self())->client->createRefund($invoice, $refundEmail, $amount, $currency);
42+
}
43+
44+
/**
45+
* Retrieve all refund requests on a BitPay invoice.
46+
*
47+
* @link https://bitpay.com/api/#rest-api-resources-invoices-retrieve-all-refund-requests-on-an-invoice
48+
* @param $invoice Invoice The BitPay invoice having the associated refunds.
49+
* @return array An array of BitPay refund object with the associated Refund object updated.
50+
* @throws \BitPaySDK\Exceptions\BitPayException BitPayException class
51+
*/
52+
public static function getRefunds(Invoice $invoice): array
53+
{
54+
return (new self())->client->getRefunds($invoice);
55+
}
56+
57+
/**
58+
* Retrieve a previously made refund request on a BitPay invoice.
59+
*
60+
* @link https://bitpay.com/api/#rest-api-resources-invoices-retrieve-a-refund-request
61+
* @param $invoice Invoice The BitPay invoice having the associated refund.
62+
* @param $refundId string The refund id for the refund to be updated with new status.
63+
* @return Refund A BitPay refund object with the associated Refund object updated.
64+
*/
65+
public static function getRefund(Invoice $invoice, string $refundId): Refund
66+
{
67+
return (new self())->client->getRefund($invoice, $refundId);
68+
}
69+
70+
/**
71+
* Cancel a previously submitted refund request on a BitPay invoice.
72+
*
73+
* @link https://bitpay.com/api/#rest-api-resources-invoices-cancel-a-refund-request
74+
* @param $invoiceId string The refund id for the refund to be canceled.
75+
* @param $refund Refund The BitPay invoice having the associated refund to be canceled.
76+
* Must have been obtained using the merchant facade.
77+
* @return bool True if the refund was successfully canceled, false otherwise.
78+
*/
79+
public static function cancelRefund(string $invoiceId, Refund $refund): bool
80+
{
81+
return (new self())->client->cancelRefund($invoiceId, $refund);
82+
}
83+
}

0 commit comments

Comments
 (0)