Skip to content

Commit

Permalink
Moved charges to Charge producer & fixed exception with empty charge_id.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterborodatyy committed Sep 4, 2017
1 parent e4935d2 commit fe7828d
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
12 changes: 10 additions & 2 deletions src/Mintance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

/**
Expand Down
50 changes: 50 additions & 0 deletions src/Producers/Charge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Mintance\Producers;

use Mintance\Exceptions\Exception;

/**
* Class Charge
* @package Mintance\Producers
*/
class Charge extends Event {

/**
* 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 track($amount, array $params = []) {

$response = $this->_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);
}
}
24 changes: 0 additions & 24 deletions src/Producers/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit fe7828d

Please sign in to comment.