-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathRecurrentPayment.php
More file actions
70 lines (66 loc) · 2.14 KB
/
RecurrentPayment.php
File metadata and controls
70 lines (66 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
namespace Tpay\Example\TransactionsApi;
use Doctrine\Common\Cache\FilesystemCache;
use PSX\Cache\SimpleCache;
use Tpay\Example\ExamplesConfig;
use Tpay\OpenApi\Api\TpayApi;
use Tpay\OpenApi\Utilities\Cache;
use Tpay\OpenApi\Utilities\TpayException;
final class RecurrentPayment extends ExamplesConfig
{
public function processPayment(
$saleDescription,
$clientToken,
$amount,
$clientName,
$clientEmail,
$orderId = null,
$language = 'pl'
) {
$request = [
'amount' => $amount,
'description' => $saleDescription,
'hiddenDescription' => $orderId,
'lang' => $language,
'payer' => [
'email' => $clientEmail,
'name' => $clientName,
],
'pay' => [
'groupId' => 103,
'cardPaymentData' => [
'token' => $clientToken,
],
],
];
// You can inject any of your PSR6 or PSR16 cache implementation
$cache = new Cache(null, new SimpleCache(new FilesystemCache(__DIR__.'/cache/')));
$TpayApi = new TpayApi($cache, self::MERCHANT_CLIENT_ID, self::MERCHANT_CLIENT_SECRET, true, 'read');
$transaction = $TpayApi->transactions()->createTransaction($request);
if (
isset($transaction['result'], $transaction['status'])
&& 'success' === $transaction['result']
&& 'correct' === $transaction['status']
) {
$this->setOrderAsConfirmed();
} else {
// Code your action when the recurrent payment fails
throw new TpayException('Unable to process payment. Response: '.json_encode($transaction));
}
}
private function setOrderAsConfirmed()
{
// Code updating order status as paid at your DB
// Save transaction Id for later use
}
}
(new RecurrentPayment())
->processPayment(
'payment for order xyz',
'5c87eb70c0e5060fb17da028c16011a840db2b83',
1.00,
'John Doe',
'customer@example.com',
'order_123456',
'pl'
);