Skip to content

Commit

Permalink
Merge pull request #3 from TendoPayPlugins/develop
Browse files Browse the repository at this point in the history
[feature] Add Notification
  • Loading branch information
tangoslee authored Aug 27, 2019
2 parents c9cb8a8 + 021b187 commit 5e065e9
Show file tree
Hide file tree
Showing 6 changed files with 332 additions and 3 deletions.
4 changes: 4 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,9 @@ REDIRECT_URL=https://
## Redirect URI when the transaction fails
ERROR_REDIRECT_URL=https://

## Personal Access Token if you use $client->getTransactionDetail(), this token should be set.
## See https://app.tendopay.ph/merchants/api-settings
MERCHANT_PERSONAL_ACCESS_TOKEN=

## If you have own sandbox
#SANDBOX_HOST_URL=https://
57 changes: 57 additions & 0 deletions samples/notify.php
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);
}


51 changes: 50 additions & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ class Constants
public const AUTHORIZATION_ENDPOINT_URI = 'payments/api/v1/authTokenRequest';
public const DESCRIPTION_ENDPOINT_URI = 'payments/api/v1/paymentDescription';
public const BEARER_TOKEN_ENDPOINT_URI = 'oauth/token';
public const ORDER_STATUS_TRANSITION_ENDPOINT_URL = 'payments/api/v1/orderUpdate';
public const ORDER_STATUS_TRANSITION_ENDPOINT_URI = 'payments/api/v1/orderUpdate';
/**
* Gets the transaction detail endpoint uri
*/
public const TRANSACTION_DETAIL_ENDPOINT_URI = '/merchants/api/v1/transactions/{transactionNumber}';

public const TENDOPAY_ICON = 'https://s3.ca-central-1.amazonaws.com/candydigital/images/tendopay/tp-icon-32x32.png';
public const TENDOPAY_FAQ = 'https://tendopay.ph/page-faq.html';


/**
* Below public constant names are used as keys of data send to or received from TP API
*/
Expand All @@ -53,6 +58,37 @@ class Constants
public const STATUS_SUCCESS = 'success';
public const STATUS_FAILURE = 'failure';

/**
* Notification Parameters
*/
public const TRANSACTION_STATUS = 'status';
public const NOTIFIED_AT = 'notified_at';
public const MERCHANT_ID = 'merchant_id';
public const MERCHANT_ORDER_ID = 'merchant_order_id';
public const AMOUNT = 'amount';
public const CREATED_AT = 'created_at';


/**
* Purchase Transaction successfully completed
*/
public const PURCHASE_TRANSACTION_SUCCESS = 'PTOK';

/**
* Purchase Transaction not successfully completed
*/
public const PURCHASE_TRANSACTION_FAILURE = 'PTNG';

/**
* Purchase Transaction has canceled
*/
public const PURCHASE_TRANSACTION_CANCELED = 'PTCA';

/**
* Cancel previous purchase transaction successfully completed
*/
public const CANCEL_TRANSACTION_SUCCESS = 'CTOK';

/**
* Below public constants are the keys of description object that is being sent during request to Description Endpoint
*/
Expand Down Expand Up @@ -153,4 +189,17 @@ private static function is_sandbox_enabled(): bool
return (bool)getenv('TENDOPAY_SANDBOX_ENABLED', true);
}

/**
* Gets the transaction detail endpoint uri
* @param string $transactionNumber
* @return string
*/
public static function getTransactionDetailEndpointURI($transactionNumber): string
{
return str_replace(
'{transactionNumber}',
$transactionNumber,
self::TRANSACTION_DETAIL_ENDPOINT_URI
);
}
}
82 changes: 82 additions & 0 deletions src/Models/NotifyRequest.php
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;
}
}
98 changes: 98 additions & 0 deletions src/Models/Transaction.php
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;
}

}
Loading

0 comments on commit 5e065e9

Please sign in to comment.