Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from omise/handle-charge-complete-event
- Loading branch information
Showing
12 changed files
with
548 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
if (! defined('_PS_VERSION_')) { | ||
exit(); | ||
} | ||
|
||
class OmiseLogger | ||
{ | ||
const ERROR = 3; | ||
const INFO = 1; | ||
const MAJOR_ISSUE = 4; | ||
const WARNING = 2; | ||
|
||
/** | ||
* Add log message to PrestaShop database. | ||
* The added log message will be displayed in PrestaShop back office. | ||
* | ||
* @param string $message | ||
* @param int $severity | ||
* | ||
* @return bool | ||
* | ||
* @see PrestaShopLoggerCore::addLog() | ||
*/ | ||
public function add($message, $severity = OmiseLogger::INFO) | ||
{ | ||
$allow_duplicate = true; | ||
$error_code = null; | ||
$object_id = null; | ||
$object_type = null; | ||
|
||
return Logger::addLog( | ||
$message, | ||
$severity, | ||
$error_code, | ||
$object_type, | ||
$object_id, | ||
$allow_duplicate | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
if (! defined('_PS_VERSION_')) { | ||
exit(); | ||
} | ||
|
||
class OmiseWebhooks | ||
{ | ||
/** | ||
* Return the request body that received from Omise server. | ||
* | ||
* @return mixed|null The null will be returned. For examples, | ||
* | ||
* - The request from Omise server is HTTP GET not HTTP POST. | ||
* - The request body is empty. | ||
* - The request body is invalid JSON format. | ||
*/ | ||
public function getRequestBody() | ||
{ | ||
$return_value_as_array = true; | ||
|
||
$data = file_get_contents('php://input'); | ||
|
||
return json_decode($data, $return_value_as_array); | ||
} | ||
|
||
public function sendRawHeaderAsBadRequest() | ||
{ | ||
$replace_previous_similar_header = true; | ||
|
||
header('HTTP/1.1 400 Bad Request', $replace_previous_similar_header, 400); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
if (! defined('_PS_VERSION_')) { | ||
exit(); | ||
} | ||
|
||
if (defined('_PS_MODULE_DIR_')) { | ||
require_once _PS_MODULE_DIR_ . 'omise/classes/omise_logger.php'; | ||
require_once _PS_MODULE_DIR_ . 'omise/classes/payment_order.php'; | ||
} | ||
|
||
abstract class OmiseBaseEvent | ||
{ | ||
protected $omise_logger; | ||
protected $omise_transaction_model; | ||
protected $payment_order; | ||
|
||
public function __construct() | ||
{ | ||
$this->omise_logger = new OmiseLogger(); | ||
$this->omise_transaction_model = new OmiseTransactionModel(); | ||
$this->payment_order = new PaymentOrder(); | ||
} | ||
|
||
public abstract function handle($event); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
if (! defined('_PS_VERSION_')) { | ||
exit(); | ||
} | ||
|
||
if (defined('_PS_MODULE_DIR_')) { | ||
require_once _PS_MODULE_DIR_ . 'omise/classes/omise_charge_class.php'; | ||
require_once _PS_MODULE_DIR_ . 'omise/events/omise_base_event.php'; | ||
} | ||
|
||
class OmiseEventChargeComplete extends OmiseBaseEvent | ||
{ | ||
const KEY = 'charge.complete'; | ||
|
||
/** | ||
* @param mixed $event The array of JSON decoded from Omise event object. | ||
* | ||
* @return bool | ||
*/ | ||
public function handle($event) | ||
{ | ||
$charge = $event['data']; | ||
|
||
$id_order = $this->omise_transaction_model->getIdOrder($charge['id']); | ||
$order = new Order($id_order); | ||
|
||
if (! Validate::isLoadedObject($order)) { | ||
return false; | ||
} | ||
|
||
$message = 'Omise Webhooks: an event charge.complete, ' . $event['id'] . ', has been caught.'; | ||
|
||
switch ($charge['status']) { | ||
case OmiseChargeClass::STATUS_FAILED: | ||
$this->payment_order->updateStateToBeCanceled($order); | ||
|
||
$message .= ' The status of order, ' . $id_order . ', has been updated to be Canceled.'; | ||
$this->omise_logger->add($message); | ||
break; | ||
|
||
case OmiseChargeClass::STATUS_SUCCESSFUL: | ||
$this->payment_order->updateStateToBeSuccess($order); | ||
|
||
$message .= ' The status of order, ' . $id_order . ', has been updated to be Payment accepted.'; | ||
$this->omise_logger->add($message); | ||
break; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
if (! defined('_PS_VERSION_')) { | ||
exit(); | ||
} | ||
|
||
if (defined('_PS_MODULE_DIR_')) { | ||
require_once _PS_MODULE_DIR_ . 'omise/events/omise_event_charge_complete.php'; | ||
} | ||
|
||
class OmiseEventHandler | ||
{ | ||
/** | ||
* Call another class to perform function for each event. The class to be used for handling event has been | ||
* determined by using the attribute named, key, of Omise event object. | ||
* | ||
* @param mixed $event The array of JSON decoded from Omise event object. | ||
* | ||
* @return bool | ||
*/ | ||
public function handle($event) | ||
{ | ||
switch ($event['key']) { | ||
case OmiseEventChargeComplete::KEY: | ||
$omise_event = new OmiseEventChargeComplete(); | ||
break; | ||
|
||
default: | ||
return false; | ||
} | ||
|
||
return $omise_event->handle($event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
use \Mockery as m; | ||
|
||
class OmiseLoggerTest extends Mockery\Adapter\Phpunit\MockeryTestCase | ||
{ | ||
private $omise_logger; | ||
|
||
public function setup() | ||
{ | ||
$this->omise_logger = new OmiseLogger(); | ||
} | ||
|
||
public function testAdd_addLogWithoutSeverityParameter_logMustBeAddedWithDefaultSeverity() | ||
{ | ||
$message = 'message'; | ||
|
||
m::mock('alias:\Logger') | ||
->shouldReceive('addLog') | ||
->with( | ||
$message, | ||
OmiseLogger::INFO, | ||
null, | ||
null, | ||
null, | ||
true | ||
); | ||
|
||
$this->omise_logger->add($message); | ||
} | ||
|
||
public function testAdd_addLogWithSeverityParameter_logMustBeAddedWithSpecifiedSeverityParameter() | ||
{ | ||
$message = 'message'; | ||
|
||
m::mock('alias:\Logger') | ||
->shouldReceive('addLog') | ||
->with( | ||
$message, | ||
OmiseLogger::ERROR, | ||
null, | ||
null, | ||
null, | ||
true | ||
); | ||
|
||
$this->omise_logger->add($message, OmiseLogger::ERROR); | ||
} | ||
} |
Oops, something went wrong.