-
Notifications
You must be signed in to change notification settings - Fork 2
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 #3 from TendoPayPlugins/develop
[feature] Add Notification
- Loading branch information
Showing
6 changed files
with
332 additions
and
3 deletions.
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,57 @@ | ||
<?php | ||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
function dump() | ||
{ | ||
echo '<pre>'; | ||
array_map('print_r', func_get_args()); | ||
echo '</pre>'; | ||
} | ||
|
||
use TendoPay\SDK\Constants; | ||
use TendoPay\SDK\Models\NotifyRequest; | ||
use TendoPay\SDK\TendoPayClient; | ||
|
||
|
||
$client = new TendoPayClient(); | ||
$client->enableSandBox(); | ||
|
||
try { | ||
$notifyRequest = new NotifyRequest($_REQUEST); | ||
$transaction = $client->getTransactionDetail($notifyRequest->getTransactionNumber()); | ||
|
||
$merchant_order_id = $transaction->getMerchantOrderId(); | ||
$status = $transaction->getStatus(); | ||
$amount = $transaction->getAmount(); | ||
|
||
// dump(compact('merchant_order_id', 'status', 'amount')); | ||
// Search Merchant side transaction by $transaction->getMerchantOrderId() | ||
// Check if the transaction is already processed | ||
// The process should stop here if this transaction is already done. | ||
// return 200 if this is a duplicated notification | ||
|
||
|
||
switch ($status) { | ||
case Constants::PURCHASE_TRANSACTION_SUCCESS: | ||
// The transaction is successfully completed | ||
// Do merchant job here | ||
break; | ||
case Constants::PURCHASE_TRANSACTION_FAILURE: | ||
// The transaction is unsuccessfully completed. | ||
// Do merchant job here | ||
break; | ||
case Constants::CANCEL_TRANSACTION_SUCCESS: | ||
// the previous transaction is successfully cancelled | ||
// Do merchant job here | ||
break; | ||
} | ||
|
||
// After all merchant side process done, return 200 | ||
http_response_code(200); | ||
} catch (Exception $e) { | ||
// other wise return error | ||
dump($e->getMessage()); | ||
http_response_code(500); | ||
} | ||
|
||
|
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,82 @@ | ||
<?php | ||
|
||
|
||
namespace TendoPay\SDK\Models; | ||
|
||
use Prophecy\Doubler\Generator\Node\ClassNode; | ||
use TendoPay\SDK\Constants; | ||
use TendoPay\SDK\Exception\TendoPayParameterException; | ||
|
||
/** | ||
* "transactionNumber":1184, | ||
* "status":"PURCHASE_TRANSACTION_CANCELED", | ||
* "notifiedAt":"2019-08-23T23:21:36+08:00" | ||
* Class NotifyRequest | ||
* @package TendoPay\SDK\Models | ||
*/ | ||
class NotifyRequest | ||
{ | ||
/** | ||
* The transaction number that status changed | ||
* @var | ||
*/ | ||
protected $transactionNumber; | ||
|
||
/** | ||
* The transaction status | ||
* @var | ||
*/ | ||
protected $status; | ||
|
||
/** | ||
* Notification datetime | ||
* @var | ||
*/ | ||
protected $notifiedAt; | ||
|
||
/** | ||
* NotifyRequest constructor. | ||
* @param array $request | ||
* @throws TendoPayParameterException | ||
*/ | ||
public function __construct(array $request) | ||
{ | ||
$this->transactionNumber = $request[Constants::TRANSACTION_NO_PARAM] ?? ''; | ||
$this->status = $request[Constants::TRANSACTION_STATUS] ?? ''; | ||
$this->notifiedAt = $request[Constants::NOTIFIED_AT] ?? ''; | ||
|
||
if (!$this->transactionNumber || | ||
!$this->status || | ||
!$this->notifiedAt) { | ||
throw new TendoPayParameterException(sprintf('%s, %s, %s are required.', | ||
Constants::TRANSACTION_NO_PARAM, | ||
Constants::TRANSACTION_STATUS, | ||
Constants::NOTIFIED_AT | ||
)); | ||
} | ||
} | ||
|
||
/** | ||
* @return mixed|string | ||
*/ | ||
public function getTransactionNumber() | ||
{ | ||
return $this->transactionNumber; | ||
} | ||
|
||
/** | ||
* @return mixed|string | ||
*/ | ||
public function getStatus() | ||
{ | ||
return $this->status; | ||
} | ||
|
||
/** | ||
* @return mixed|string | ||
*/ | ||
public function getNotifiedAt() | ||
{ | ||
return $this->notifiedAt; | ||
} | ||
} |
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,98 @@ | ||
<?php | ||
|
||
|
||
namespace TendoPay\SDK\Models; | ||
|
||
use TendoPay\SDK\Constants; | ||
use TendoPay\SDK\Exception\TendoPayParameterException; | ||
|
||
/** | ||
* Class Transaction | ||
* @package TendoPay\SDK\Models | ||
* | ||
* #### Data Structure | ||
* | Field | Data Type | Description | | ||
* | --- | --- | --- | | ||
* | merchant_id | Number | Merchant ID | | ||
* | merchant_order_id | String | Merchant Order ID | | ||
* | amount | Number | Request Amount | | ||
* | tendopay_transaction_number | String | Transaction Number | | ||
* | created_at | ISO8601 Datetime | Datetime of the transaction | | ||
* | status | String | Status of the transaction | | ||
*/ | ||
class Transaction | ||
{ | ||
protected $merchantId; | ||
protected $merchantOrderId; | ||
protected $amount; | ||
protected $transactionNumber; | ||
protected $createdAt; | ||
protected $status; | ||
|
||
public function __construct(array $response = []) | ||
{ | ||
$this->merchantId = $response[Constants::MERCHANT_ID] ?? null; | ||
$this->merchantOrderId = $response[Constants::MERCHANT_ORDER_ID] ?? null; | ||
$this->amount = $response[Constants::AMOUNT] ?? null; | ||
$this->transactionNumber = $response[Constants::TRANSACTION_NO_PARAM] ?? null; | ||
$this->status = $response[Constants::TRANSACTION_STATUS] ?? null; | ||
$this->createdAt = $response[Constants::CREATED_AT] ?? null; | ||
|
||
if (!$this->transactionNumber || !$this->merchantOrderId) { | ||
throw new TendoPayParameterException(sprintf('%s, %s cannot be null', | ||
Constants::TRANSACTION_NO_PARAM, | ||
Constants::MERCHANT_ORDER_ID | ||
)); | ||
} | ||
} | ||
|
||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getMerchantId() | ||
{ | ||
return $this->merchantId; | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getMerchantOrderId() | ||
{ | ||
return $this->merchantOrderId; | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getAmount() | ||
{ | ||
return $this->amount; | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getTransactionNumber() | ||
{ | ||
return $this->transactionNumber; | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getCreatedAt() | ||
{ | ||
return $this->createdAt; | ||
} | ||
|
||
/** | ||
* @return null | ||
*/ | ||
public function getStatus() | ||
{ | ||
return $this->status; | ||
} | ||
|
||
} |
Oops, something went wrong.