Skip to content

Commit

Permalink
Merge pull request #6 from TendoPayPlugins/develop
Browse files Browse the repository at this point in the history
sync develop to master
  • Loading branch information
pmkay authored Dec 4, 2020
2 parents 5e065e9 + 17062e4 commit 33dcd05
Show file tree
Hide file tree
Showing 29 changed files with 2,613 additions and 1,014 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://
179 changes: 155 additions & 24 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 All @@ -14,46 +20,171 @@ 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=
MERCHANT_SECRET=
```bash
## Client Credentials
CLIENT_ID=
CLIENT_SECRET=

## 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
```

- 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 = [
'CLIENT_ID' => '',
'CLIENT_SECRET' => '',
'REDIRECT_URL' => '',
'TENDOPAY_SANDBOX_ENABLED' => false,
];
$client = new TendoPayClient($config);
```

## Dependencies
- guzzle
- phpdotenv

### Make Payment

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\Models\Payment;
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'];
$redirectUrl = $_POST['tp_redirect_url'] ?? '';
### 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)
->setCurrency('PHP')
->setRedirectUrl($redirectUrl);


$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\V2\TendoPayClient;

$client = new TendoPayClient();

try {
if (TendoPayClient::isCallBackRequest($_REQUEST)) {
$transaction = $client->verifyTransaction(new VerifyTransactionRequest($_REQUEST));

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

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();
} catch (Exception $e) {
echo 'Runtime Error:'.$e->getMessage();
}
```

### Cancel Payment

```php
use TendoPay\SDK\Exception\TendoPayConnectionException;
use TendoPay\SDK\V2\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\V2\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();
}
```
Loading

0 comments on commit 33dcd05

Please sign in to comment.