From f34dba2448a2f4f41d3d8ba0454b08064bb3edef Mon Sep 17 00:00:00 2001 From: "nicolas.dorier" Date: Sat, 26 Jan 2019 13:56:34 +0900 Subject: [PATCH] Add taxIncluded field --- examples/tutorial/IPNlogger.php | 3 ++- src/Bitpay/Client/Client.php | 11 ++--------- src/Bitpay/Invoice.php | 22 ++++++++++++++++++++++ src/Bitpay/InvoiceInterface.php | 2 ++ src/Bitpay/Item.php | 26 ++++++++++++++++++++++++++ src/Bitpay/ItemInterface.php | 5 +++++ 6 files changed, 59 insertions(+), 10 deletions(-) diff --git a/examples/tutorial/IPNlogger.php b/examples/tutorial/IPNlogger.php index 588718f..2051989 100644 --- a/examples/tutorial/IPNlogger.php +++ b/examples/tutorial/IPNlogger.php @@ -62,8 +62,9 @@ $invoiceId = $invoice->getId(); $invoiceStatus = $invoice->getStatus(); $invoiceExceptionStatus = $invoice->getExceptionStatus(); $invoicePrice = $invoice->getPrice(); +$taxIncluded = $invoice->getTaxIncluded(); -fwrite($myfile, $date . " : IPN received for BitPay invoice ".$invoiceId." . Status = " .$invoiceStatus." / exceptionStatus = " . $invoiceExceptionStatus." Price = ". $invoicePrice. "\n"); +fwrite($myfile, $date . " : IPN received for BitPay invoice ".$invoiceId." . Status = " .$invoiceStatus." / exceptionStatus = " . $invoiceExceptionStatus." Price = ". $invoicePrice." Tax Included = ". $taxIncluded."\n"); fwrite($myfile, "Raw IPN: ". $raw_post_data."\n"); //Respond with HTTP 200, so BitPay knows the IPN has been received correctly diff --git a/src/Bitpay/Client/Client.php b/src/Bitpay/Client/Client.php index 716f4b1..23ff666 100644 --- a/src/Bitpay/Client/Client.php +++ b/src/Bitpay/Client/Client.php @@ -126,6 +126,7 @@ class Client implements ClientInterface ->setStatus($data['status']) ->setBtcPrice(array_key_exists('btcPrice', $data) ? $data['btcPrice'] : '') ->setPrice($data['price']) + ->setPrice($data['taxIncluded']) ->setCurrency(new \Bitpay\Currency($data['currency'])) ->setOrderId(array_key_exists('orderId', $data) ? $data['orderId'] : '') ->setInvoiceTime($invoiceTime) @@ -162,6 +163,7 @@ class Client implements ClientInterface $body = array( 'price' => $item->getPrice(), + 'taxIncluded' => $item->getTaxIncluded(), 'currency' => $currency->getCode(), 'posData' => $invoice->getPosData(), 'notificationURL' => $invoice->getNotificationUrl(), @@ -701,14 +703,5 @@ class Client implements ClientInterface protected function checkPriceAndCurrency($price, $currency) { - $decimalPosition = strpos($price, '.'); - if ($decimalPosition == 0) { - $decimalPrecision = 0; - } else { - $decimalPrecision = strlen(substr($price, $decimalPosition + 1)); - } - if (($decimalPrecision > 2 && $currency != 'BTC') || $decimalPrecision > 6) { - throw new \Exception('Incorrect price format or currency type.'); - } } } diff --git a/src/Bitpay/Invoice.php b/src/Bitpay/Invoice.php index 792277b..8c3dfdf 100644 --- a/src/Bitpay/Invoice.php +++ b/src/Bitpay/Invoice.php @@ -164,6 +164,14 @@ class Invoice implements InvoiceInterface return $this->getItem()->getPrice(); } + /** + * @inheritdoc + */ + public function getTaxIncluded() + { + return $this->getItem()->getTaxIncluded(); + } + /** * @param float $price * @@ -178,6 +186,20 @@ class Invoice implements InvoiceInterface return $this; } + /** + * @param float $taxIncluded + * + * @return InvoiceInterface + */ + public function setTaxIncluded($taxIncluded) + { + if (!empty($taxIncluded)) { + $this->getItem()->setTaxIncluded($taxIncluded); + } + + return $this; + } + /** * @inheritdoc */ diff --git a/src/Bitpay/InvoiceInterface.php b/src/Bitpay/InvoiceInterface.php index 3b187ab..5bd16e1 100644 --- a/src/Bitpay/InvoiceInterface.php +++ b/src/Bitpay/InvoiceInterface.php @@ -84,6 +84,8 @@ interface InvoiceInterface */ public function getPrice(); + public function getTaxIncluded(); + /** * This is the currency code set for the price setting.  The pricing currencies * currently supported are USD, EUR, BTC, and all of the codes listed on this page: diff --git a/src/Bitpay/Item.php b/src/Bitpay/Item.php index 45a5b03..6b238a4 100644 --- a/src/Bitpay/Item.php +++ b/src/Bitpay/Item.php @@ -29,6 +29,11 @@ class Item implements ItemInterface */ protected $price; + /** + * @var float + */ + protected $taxIncluded; + /** * @var integer */ @@ -96,6 +101,16 @@ class Item implements ItemInterface return $this->price; } + /** + * @inheritdoc + * + * @return float + */ + public function getTaxIncluded() + { + return $this->taxIncluded; + } + /** * @param mixed $price A float, integer, or en_US formatted numeric string * @@ -112,6 +127,17 @@ class Item implements ItemInterface return $this; } + public function setTaxIncluded($taxIncluded) + { + if (is_string($taxIncluded)) { + $this->checkPriceFormat($taxIncluded); + } + + $this->taxIncluded = (float)$taxIncluded; + + return $this; + } + /** * @inheritdoc */ diff --git a/src/Bitpay/ItemInterface.php b/src/Bitpay/ItemInterface.php index 36e2dc5..f8b83c3 100644 --- a/src/Bitpay/ItemInterface.php +++ b/src/Bitpay/ItemInterface.php @@ -34,6 +34,11 @@ interface ItemInterface */ public function getPrice(); + /** + * @return string + */ + public function getTaxIncluded(); + /** * @return string */