From fe7828d168337f799df2adf9bd623379cb46b0bc Mon Sep 17 00:00:00 2001 From: Peter Borodatyy Date: Mon, 4 Sep 2017 09:52:03 +0300 Subject: [PATCH] Moved charges to Charge producer & fixed exception with empty charge_id. --- src/Mintance.php | 12 ++++++++-- src/Producers/Charge.php | 50 ++++++++++++++++++++++++++++++++++++++++ src/Producers/Event.php | 24 ------------------- 3 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 src/Producers/Charge.php diff --git a/src/Mintance.php b/src/Mintance.php index 0241d69..a4fbf36 100644 --- a/src/Mintance.php +++ b/src/Mintance.php @@ -3,6 +3,7 @@ namespace Mintance; use Mintance\Exceptions\Exception; +use Mintance\Producers\Charge; use Mintance\Producers\Event; use Mintance\Producers\People; use Mintance\Transport\AbstractTransport; @@ -38,10 +39,15 @@ class Mintance { ]; /** - * @var \Mintance\Producers\Event Event producer object. + * @var \Mintance\Producers\Event Events producer object. */ protected $_event; + /** + * @var \Mintance\Producers\Charge Charges producer object. + */ + protected $_charge; + /** * @var \Mintance\Session\Session Session store object. */ @@ -73,6 +79,8 @@ public function __construct($token, array $options = []) { $this->_event = new Event($this->_transport); + $this->_charge = new Charge($this->_transport); + $this->_session = new Session($this->_transport); } @@ -110,7 +118,7 @@ public function track($name, array $params = []) { * @throws Exception If something wrong */ public function charge($amount, array $params = []) { - return $this->_event->charge($amount, $params); + return $this->_charge->track($amount, $params); } /** diff --git a/src/Producers/Charge.php b/src/Producers/Charge.php new file mode 100644 index 0000000..0fd1253 --- /dev/null +++ b/src/Producers/Charge.php @@ -0,0 +1,50 @@ +_push($this->_buildEvent('New Charge', 'charge', array_merge($params, [ + 'value' => $amount + ]))); + + if(!empty($response['charge_id'])) { + return $response['charge_id']; + } else { + throw new Exception('Charge sending error.'); + } + } + + /** + * Function send's event to mintance. + * + * @param array $event Event data. + * + * @return \Mintance\Transport\AbstractTransport + */ + protected function _push(array $event) { + + $this->_transport->setEndpoint('charges'); + + return $this->_transport->execute($event); + } +} \ No newline at end of file diff --git a/src/Producers/Event.php b/src/Producers/Event.php index 592483b..357ad82 100644 --- a/src/Producers/Event.php +++ b/src/Producers/Event.php @@ -30,30 +30,6 @@ public function track($name, array $params = []) { } } - /** - * Tracking Purchase event. - * - * @param float $amount Amount of money. - * @param array $params Purchase params - * Default params are: currency, products - * - * @return string Charge ID if success. - * - * @throws \Mintance\Exceptions\Exception Throws exception if something goes wrong. - */ - public function charge($amount, array $params = []) { - - $response = $this->_push($this->_buildEvent('Charge', 'charge', array_merge($params, [ - 'value' => $amount - ]))); - - if(!empty($response['charge_id'])) { - return $response['charge_id']; - } else { - throw new Exception('Charge sending error.'); - } - } - /** * Tracking form submission event. *