Skip to content

Commit

Permalink
Merge pull request #5 from tangoslee/feature/TEN-1162
Browse files Browse the repository at this point in the history
Add draft SDK for v2
  • Loading branch information
pmkay authored Dec 4, 2020
2 parents 8e95363 + 0e94f51 commit 17062e4
Show file tree
Hide file tree
Showing 20 changed files with 1,404 additions and 113 deletions.
51 changes: 28 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# TendoPay SDK for PHP
# TendoPay SDK for PHP (v2)

If you find a document for v1, please go to [TendoPay SDK for PHP (v1)](./README_v1.md)

## Requirements

PHP 7.0 and later.

## Upgrade

[UPGRADE from v1](./UPGRADE.md)

## Installation

### Using Composer
Expand Down Expand Up @@ -34,22 +40,15 @@ http://localhost:8000/
> MERCHANT_ID,MERCHANT_SECRET for test can get them at [TendoPay Sandbox](https://sandbox.tendopay.ph)
```bash
## Merchant Credentials
MERCHANT_ID=
MERCHANT_SECRET=

## Client Credentials
CLIENT_ID=
CLIENT_SECRET=

## Enable Sandbox, it must be false in production
TENDOPAY_SANDBOX_ENABLED=false

## Redirect URI when the transaction succeed
REDIRECT_URL=https://localhost:8000/purhase.php

## Redirect URI when the transaction fails
ERROR_REDIRECT_URL=https://localhost:8000/purchase.php
## Enable Sandbox, it must be false in production
TENDOPAY_SANDBOX_ENABLED=false
```

```php
Expand All @@ -64,12 +63,9 @@ $client = new TendoPayClient();
use TendoPay\SDK\TendoPayClient;

$config = [
'MERCHANT_ID' => '',
'MERCHANT_SECRET' => '',
'CLIENT_ID' => '',
'CLIENT_SECRET' => '',
'REDIRECT_URL' => '',
'ERROR_REDIRECT_URL' => '',
'TENDOPAY_SANDBOX_ENABLED' => false,
];
$client = new TendoPayClient($config);
Expand All @@ -81,13 +77,13 @@ $client = new TendoPayClient($config);
```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\Payment;
use TendoPay\SDK\TendoPayClient;
use TendoPay\SDK\V2\TendoPayClient;

### S:Merchant set proper values
$merchant_order_id = $_POST['tp_merchant_order_id'];
$request_order_amount = $_POST['tp_amount'];
$request_order_title = $_POST['tp_description'];
$_SESSION['merchant_order_id'] = $merchant_order_id;
$redirectUrl = $_POST['tp_redirect_url'] ?? '';
### E:Merchant set proper values

$client = new TendoPayClient();
Expand All @@ -96,7 +92,10 @@ try {
$payment = new Payment();
$payment->setMerchantOrderId($merchant_order_id)
->setDescription($request_order_title)
->setRequestAmount($request_order_amount);
->setRequestAmount($request_order_amount)
->setCurrency('PHP')
->setRedirectUrl($redirectUrl);


$client->setPayment($payment);

Expand All @@ -114,21 +113,27 @@ try {
```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\VerifyTransactionRequest;
use TendoPay\SDK\TendoPayClient;
use TendoPay\SDK\V2\TendoPayClient;

$client = new TendoPayClient();

try {
if (TendoPayClient::isCallBackRequest($_REQUEST)) {
$merchant_order_id = $_SESSION['merchant_order_id'] ?? null;
$transaction = $client->verifyTransaction($merchant_order_id, new VerifyTransactionRequest($_REQUEST));
$transaction = $client->verifyTransaction(new VerifyTransactionRequest($_REQUEST));

if (!$transaction->isVerified()) {
throw new UnexpectedValueException('Invalid signature for the verification');
}

// Save $transactionNumber here
// Proceed merchant post order process
if ($transaction->getStatus() == \TendoPay\SDK\V2\ConstantsV2::STATUS_SUCCESS) {
// PAID
// Save $transactionNumber here
// Proceed merchant post order process
} else if ($transaction->getStatus() == \TendoPay\SDK\V2\ConstantsV2::STATUS_FAILURE) {
// FAILED
// do something in failure case
// error message $transaction->getMessage()
}
}
} catch (TendoPayConnectionException $e) {
echo 'Connection Error:'.$e->getMessage();
Expand All @@ -141,7 +146,7 @@ try {

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\TendoPayClient;
use TendoPay\SDK\V2\TendoPayClient;

$client = new TendoPayClient();

Expand All @@ -161,7 +166,7 @@ try {

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\TendoPayClient;
use TendoPay\SDK\V2\TendoPayClient;

$client = new TendoPayClient();

Expand Down
185 changes: 185 additions & 0 deletions README_v1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
# TendoPay SDK for PHP

## Requirements

PHP 7.0 and later.

## Installation

### Using Composer

You can install the sdk via [Composer](http://getcomposer.org/). Run the following command:

```bash
composer require tendopay/tendopay-sdk-php
```

## Run SDK Tester

- Run a sample server
```bash
php -s localhost:8000 -t vendor/tendopay/tendopay-sdk-php/samples
```

- Open browser and goto
```bash
http://localhost:8000/
```

## Code Examples

### Create TendoPayClient

- Using .env
> MERCHANT_ID,MERCHANT_SECRET for test can get them at [TendoPay Sandbox](https://sandbox.tendopay.ph)
```bash
## Merchant Credentials
MERCHANT_ID=
MERCHANT_SECRET=

## Client Credentials
CLIENT_ID=
CLIENT_SECRET=

## Enable Sandbox, it must be false in production
TENDOPAY_SANDBOX_ENABLED=false

## Redirect URI when the transaction succeed
REDIRECT_URL=https://localhost:8000/purhase.php

## Redirect URI when the transaction fails
ERROR_REDIRECT_URL=https://localhost:8000/purchase.php
```

```php
use TendoPay\SDK\TendoPayClient;

$client = new TendoPayClient();
```

- Using $config variable

```php
use TendoPay\SDK\TendoPayClient;

$config = [
'MERCHANT_ID' => '',
'MERCHANT_SECRET' => '',
'CLIENT_ID' => '',
'CLIENT_SECRET' => '',
'REDIRECT_URL' => '',
'ERROR_REDIRECT_URL' => '',
'TENDOPAY_SANDBOX_ENABLED' => false,
];
$client = new TendoPayClient($config);
```


### Make Payment

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\Payment;
use TendoPay\SDK\TendoPayClient;

### S:Merchant set proper values
$merchant_order_id = $_POST['tp_merchant_order_id'];
$request_order_amount = $_POST['tp_amount'];
$request_order_title = $_POST['tp_description'];
$_SESSION['merchant_order_id'] = $merchant_order_id;
### E:Merchant set proper values

$client = new TendoPayClient();

try {
$payment = new Payment();
$payment->setMerchantOrderId($merchant_order_id)
->setDescription($request_order_title)
->setRequestAmount($request_order_amount);

$client->setPayment($payment);

$redirectURL = $client->getAuthorizeLink();
header('Location: '.$redirectURL);
} catch (TendoPayConnectionException $e) {
echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
echo 'Runtime Error:'.$e->getMessage();
}
```

### Callback (redirected page)

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\VerifyTransactionRequest;
use TendoPay\SDK\TendoPayClient;

$client = new TendoPayClient();

try {
if (TendoPayClient::isCallBackRequest($_REQUEST)) {
$merchant_order_id = $_SESSION['merchant_order_id'] ?? null;
$transaction = $client->verifyTransaction($merchant_order_id, new VerifyTransactionRequest($_REQUEST));

if (!$transaction->isVerified()) {
throw new UnexpectedValueException('Invalid signature for the verification');
}

// Save $transactionNumber here
// Proceed merchant post order process
}
} catch (TendoPayConnectionException $e) {
echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
echo 'Runtime Error:'.$e->getMessage();
}
```

### Cancel Payment

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\TendoPayClient;

$client = new TendoPayClient();

try {
$client->cancelPayment($transactionNumber);
// merchant process here

} catch (TendoPayConnectionException $e) {
echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
echo 'Runtime Error:'.$e->getMessage();
}
```


### Show Transaction Detail

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\TendoPayClient;

$client = new TendoPayClient();

try {

$transaction = $client->getTransactionDetail($transactionNumber);

// merchant process here
// $transaction->getMerchantId();
// $transaction->getMerchantOrderId();
// $transaction->getAmount();
// $transaction->getTransactionNumber();
// $transaction->getCreatedAt();
// $transaction->getStatus();

} catch (TendoPayConnectionException $e) {
echo 'Connection Error:'.$e->getMessage();
} catch (Exception $e) {
echo 'Runtime Error:'.$e->getMessage();
}
```
31 changes: 31 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Upgrade Guide

## # Upgrading to v2 from 0.8.x

### Changes

##### New Client ID/ Client Secret
Merchant should create a new Client App(REST V2) in the Merchant Dashboard to use SDK 2.x
Existing credentils is not compatible with SDK 2.x

##### MERCHANT_ID/MERCHANT_SECRET
MERCHANT_ID/MERCHANT_SECRET has been removed

##### ERROR_REDIRECT_URL
ERROR_REDIRECT_URL has been removed.
All successful or unsuccessful response will be redirected to REDIRECT_URL

##### Backend Notification
When a merchant creates an app (REST V2),
if 'NOTIFICATION_URL' is set, TendoPay notifies some changes of transactions to the notification callback URL.
This callback is asynchronous back-end API request.
'PAID', 'FAILED', 'CANCELLED' events of transactions are triggered.
##### Change the namespace of TendoPayClient
```php
# TendoPayClient for v1
use TendoPay\SDK\TendoPayClient;

# TendoPayClient for v2
use TendoPay\SDK\V2\TendoPayClient;
````
20 changes: 6 additions & 14 deletions samples/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@

require_once __DIR__.'/common.php';

use TendoPay\SDK\TendoPayClient;
global $config;

/**
*
* @param mixed $response
* @param int $status
*/
function json($response = [], $status = 200)
{
echo header('Content-Type: application/json', true, $status);
echo json_encode($response);
}

## Main
$request = json_decode(file_get_contents('php://input'), false);
$job = $request->job ?? null;
Expand All @@ -31,13 +19,17 @@ function json($response = [], $status = 200)
case 'GET_TRANSACTIONS':
return json($_SESSION['transactions'] ?? []);
case 'GET_TRANSACTION':
$client = new TendoPayClient($config);
$client = ($config['TP_SDK_VERSION'] ?? null) === 'v2' ?
new \TendoPay\SDK\V2\TendoPayClient($config) :
new \TendoPay\SDK\TendoPayClient($config);
$response = $client->getTransactionDetail($request->transactionNumber);

return json($response->toArray());
case 'CANCEL_TRANSACTION':
$transactionNumber = $request->transactionNumber;
$client = new TendoPayClient($config);
$client = ($config['TP_SDK_VERSION'] ?? null) === 'v2' ?
new \TendoPay\SDK\V2\TendoPayClient($config) :
new \TendoPay\SDK\TendoPayClient($config);
$client->cancelPayment($transactionNumber);

$_SESSION['transactions'] = array_filter($_SESSION['transactions'] ?? [],
Expand Down
Loading

0 comments on commit 17062e4

Please sign in to comment.