Skip to content

Commit

Permalink
Merge pull request #4 from tangoslee/feature/TEN-1333
Browse files Browse the repository at this point in the history
Feature/ten 1333
  • Loading branch information
pmkay authored Oct 15, 2020
2 parents 021b187 + 612d1bd commit 8e95363
Show file tree
Hide file tree
Showing 20 changed files with 1,290 additions and 983 deletions.
10 changes: 3 additions & 7 deletions .env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@ MERCHANT_SECRET=
CLIENT_ID=
CLIENT_SECRET=

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

## Redirect URI when the transaction succeed
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://
160 changes: 143 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,25 @@ You can install the sdk via [Composer](http://getcomposer.org/). Run the followi
composer require tendopay/tendopay-sdk-php
```

then imports
## Run SDK Tester

```php
require_once 'vendor/autoload.php';
- Run a sample server
```bash
php -s localhost:8000 -t vendor/tendopay/tendopay-sdk-php/samples
```

## Run Sample code

- Copy sample page to in the local
- Open browser and goto
```bash
cp -av vendor/tendopay/tendopay-sdk-php/samples .
http://localhost:8000/
```

- Add some environment variables into .env
## 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=
Expand All @@ -38,22 +42,144 @@ MERCHANT_SECRET=
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
```

- Run a sample server
```bash
php -s localhost:8000 -t samples
```php
use TendoPay\SDK\TendoPayClient;

$client = new TendoPayClient();
```
- Open browser and goto
```bash
http://localhost:8000/cart.html

- 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();
}
```

## Dependencies
- guzzle
- phpdotenv
### 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();
}
```
6 changes: 1 addition & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
"role": "Developer"
}
],
"minimum-stability": "dev",
"prefer-stable": "true",
"require": {
"php": ">=7.1",
"vlucas/phpdotenv": "^3.3",
"guzzlehttp/guzzle": "^6.3"
"php": ">=7.1"
},
"require-dev": {
"phpunit/phpunit": "^8"
Expand Down
Loading

0 comments on commit 8e95363

Please sign in to comment.