-
Notifications
You must be signed in to change notification settings - Fork 11
/
RecurrentPayment.php
65 lines (61 loc) · 1.87 KB
/
RecurrentPayment.php
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
<?php
namespace Tpay\Example\TransactionsApi;
use Tpay\Example\ExamplesConfig;
use Tpay\OpenApi\Api\TpayApi;
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,
],
],
];
$TpayApi = new TpayApi(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',
'order_123456',
'pl'
);