Skip to content

Commit

Permalink
Added Callback
Browse files Browse the repository at this point in the history
  • Loading branch information
reactmore committed Sep 1, 2021
1 parent 95a04f2 commit 63f2c53
Show file tree
Hide file tree
Showing 7 changed files with 360 additions and 151 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor/*
.env
index.php
tests/*
callback-tripay.json
CallbackHandler.php
59 changes: 56 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Tripay Payment Gateway Client Library
For details please visit Official [Documentation](https://payment.tripay.co.id/developer).

## Instalation

```
composer require reactmore/tripay-payment-gateway
```
Expand Down Expand Up @@ -51,15 +52,66 @@ print_r($init->instructions()->get($payload));
echo '</pre>';
```

Callback Handler
```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Reactmore\Tripay\Main;

$data = new Main();

$init = $data->initCallback();

if ($init->validateSignature()) {

if ($init->callEvent() === 'payment_status') {
// Response Callback
$data = json_encode($init->get(), true);

// Get Status From Data
$status = $data['status'];

if ($status === 'PAID') {
// Your Logic
echo $status;
} elseif ($status === 'PAID') {
// Your Logic
echo $status;
} elseif ($status === 'UNPAID') {
// Your Logic
echo $status;
} elseif ($status === 'REFUND') {
// Your Logic
echo $status;
} elseif ($status === 'EXPIRED') {
// Your Logic
echo $status;
} else {
// Your Logic
echo $status; // Failed
}
}
// IF ERROR FROM SYSTEM THROW IN THIS SECTIONS

// PUT RESPONSE CALLBACK TO CALLBACK-TRIPAY.JSON
file_put_contents(__DIR__ . '/callback-tripay.json', $init->get() . PHP_EOL . PHP_EOL, FILE_APPEND | LOCK_EX);
} else {
echo 'Invalid Signature! ';
exit;
}
```

## Method

| API | Method | Endpoint | Status |
|---|---|---|---|
| `PAYMENT Method`| `initPayment()` | `instructions()` | OK |
| `MERCHANT Method`| `initMerchant()` | `paymentchannel()`, `calculator()`, `transactions` | OK |
| `TRANSACTIONS CLOSE`| `initTransactions()`| `transactions`, `detail` | OK |
| `TRANSACTIONS OPEN`| `initTransactionsOpen()` | | Progress |
| `CALLBACK`| `InitCallback()` | | Progress |
| `TRANSACTIONS CLOSE`| `initTransactions()`| `transactions()`, `detail()`, `create()` | OK |
| `TRANSACTIONS OPEN`| `initTransactionsOpen()` | `transactions()`, `detail()`, `create()` | OK |
| `CALLBACK`| `InitCallback()` | `signature()`, `callbackSignature()`, `validateSignature()`, `CallEvent()`| OK |
| `PPOB`| `InitPPOB()` | | Progress |

## For Handle Actions
Expand All @@ -80,6 +132,7 @@ Return Throw;
| `Content type must be array` | All request make from Array so code not pass if not in array



## License

Please see the [LICENSE](LICENSE) included in this repository for a full copy of the MIT license, which this project is licensed under.
Expand Down
7 changes: 6 additions & 1 deletion app/Main.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Reactmore\Tripay\Services\Transactions\Open\InitTransactionsOpen;
use Reactmore\Tripay\Helpers\FileHelper;
use Reactmore\Tripay\Helpers\Validations\MainValidator;

use Reactmore\Tripay\Services\Callback;

class Main implements MainInterface
{
Expand Down Expand Up @@ -85,4 +85,9 @@ public function initTransactionsOpen()
{
return new InitTransactionsOpen($this);
}

public function initCallback()
{
return new Callback($this);
}
}
49 changes: 49 additions & 0 deletions app/Services/Callback.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Reactmore\Tripay\Services;

use Symfony\Component\HttpFoundation\Request;

class Callback
{

public $request, $privateKey;


public function __construct($data)
{
$this->privateKey = $data->credential['privateKey'];
$this->request = Request::createFromGlobals();
}


public function get()
{
return $this->request->getContent();
}

public function signature()
{
return hash_hmac('sha256', $this->get(), $this->privateKey);
}


public function callbackSignature()
{
return isset($_SERVER['HTTP_X_CALLBACK_SIGNATURE']) ? $_SERVER['HTTP_X_CALLBACK_SIGNATURE'] : '';
}

public function callEvent()
{
return isset($_SERVER['HTTP_X_CALLBACK_EVENT']) ? $_SERVER['HTTP_X_CALLBACK_EVENT'] : '';
}

public function validateSignature(): bool
{
if ($this->callbackSignature() !== $this->signature()) {
return false;
}

return true;
}
}
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"require": {
"php": "^7.0 | ^8.0",
"guzzlehttp/guzzle": "^6.5 || ^7.0",
"vlucas/phpdotenv": "^4.2 || ^5.0"
"vlucas/phpdotenv": "^4.2 || ^5.0",
"symfony/http-foundation": "^5.3"
},
"require-dev": {
"mockery/mockery": "~1.3",
Expand All @@ -25,4 +26,4 @@
"vendor/bin/phpunit tests"
]
}
}
}
Loading

0 comments on commit 63f2c53

Please sign in to comment.