Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/workflows/pull_request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ jobs:
- uses: actions/upload-artifact@v4
id: sdk-upload
with:
name: 'tpay-openapi'
name: 'tpay-openapi-php'
path: 'sdk/'

- uses: mshick/add-pr-comment@v2
with:
message: |
Tpay Open API SDK - ${{ steps.sdk-upload.outputs.artifact-url }}
Tpay OpenAPI SDK - ${{ steps.plugin-upload.outputs.artifact-url }}

Binary file added examples/TPAY_LOGO_RGB.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions examples/TransactionsApi/TransactionQRCodeExample.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Tpay\Example\TransactionsApi;

use Tpay\Example\ExamplesConfig;
use Tpay\OpenApi\Api\TpayApi;

final class TransactionQRCodeExample extends ExamplesConfig
{
private $TpayApi;

public function __construct()
{
parent::__construct();
$this->TpayApi = new TpayApi(self::MERCHANT_CLIENT_ID, self::MERCHANT_CLIENT_SECRET, true, 'read');
}

public function runAllExamples()
{
$transaction = $this->createBasicTransaction(150);
$this->getTransactionQR($transaction['transactionId']);
$this->getTransactionQR($transaction['transactionId'], 'S', __DIR__.'/../TPAY_LOGO_RGB.png');
$this->getTransactionQR($transaction['transactionId'], 'XL', __DIR__.'/../TPAY_LOGO_RGB.png', 'image/jpeg');
$this->getTransactionQR($transaction['transactionId'], 'M', null, 'image/svg+xml');
}

// Create transaction with a specified bank group id
public function createBasicTransaction($bankGroupId)
{
$transactionParameters = $this->getTransactionParameters();
$transactionParameters['pay'] = ['groupId' => $bankGroupId];
$newTransaction = $this->TpayApi->transactions()->createTransaction($transactionParameters);
echo '<hr />';
echo '<pre>';
echo json_encode($newTransaction, JSON_PRETTY_PRINT);
echo '</pre>';

return $newTransaction;
}

protected function getTransactionParameters()
{
return [
'amount' => 0.10,
'description' => 'test transaction',
'hiddenDescription' => 'order_213',
'payer' => [
'email' => 'john.doe@example.com',
'name' => 'John Doe',
'phone' => '123456789',
'address' => 'Long Street 123/2',
'code' => '11-123',
'city' => 'New York',
'country' => 'US',
'taxId' => 'PL3774716081',
],
'lang' => 'en',
'callbacks' => [
'notification' => [
'url' => 'https://example.com/notification',
'email' => 'merchant@example.com',
],
'payerUrls' => [
'success' => 'https://example.com/success',
'error' => 'https://example.com/error',
],
],
];
}

protected function getTransactionQR($transactionUid, $size = 'M', $logoPath = null, $desiredType = 'image/png')
{
$image = $this->TpayApi->transactions()->getTransactionQR($transactionUid, $size, $logoPath, $desiredType);
echo '<hr />';
echo '<image src="data:'.$desiredType.';base64, '.base64_encode($image).'" />';
}
}

(new TransactionQRCodeExample())->runAllExamples();
25 changes: 25 additions & 0 deletions src/Api/Transactions/TransactionsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,31 @@ public function getTransactionById($transactionId)
return $this->run(static::GET, sprintf('/transactions/%s', $transactionId));
}

public function getTransactionQR($transactionId, $size = 'M', $logoPath = null, $desiredType = 'image/png')
{
$image = null;
$imageType = null;
if (null !== $logoPath && file_exists($logoPath) && is_readable($logoPath)) {
$image = base64_encode(file_get_contents($logoPath));
$imageType = mime_content_type($logoPath);
}

$fields = [
'size' => $size,
'logo' => $image,
'logoType' => $imageType,
'outputType' => $desiredType,
];

return $this
->sendRequest(
sprintf('/transactions/%s/qr', $transactionId),
static::POST,
$fields
)
->getRequestResult(false);
}

/**
* @param string $transactionId
* @param array $queryFields
Expand Down