-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginChipCallback.php
More file actions
133 lines (101 loc) · 4.21 KB
/
Copy pathPluginChipCallback.php
File metadata and controls
133 lines (101 loc) · 4.21 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
require_once 'modules/admin/models/PluginCallback.php';
require_once 'modules/billing/models/class.gateway.plugin.php';
require_once 'modules/billing/models/Invoice.php';
require_once __DIR__ . '/class.chip.api.php';
class PluginChipCallback extends PluginCallback
{
public function processCallback()
{
if (isset($_GET['invoice_number'])) {
$this->handle_redirect();
exit;
}
CE_Lib::log(4, 'CHIP callback invoked');
// Checking for request / verify x-signature
if (!isset($_SERVER['HTTP_X_SIGNATURE'])) {
CE_Lib::log(4, 'No X Signature received from headers');
die('No X Signature received from headers');
}
if (empty($content = file_get_contents('php://input'))) {
die('No input received');
}
$purchase = json_decode($content, true);
if ($purchase['status'] != 'paid') {
exit;
}
$brand_id = $this->settings->get('plugin_chip_Brand ID');
$public_key = $this->settings->get('plugin_chip_Public Key');
$public_key = str_replace($brand_id, '', $public_key);
// str_replace( '\n', "\n", $chip->public_key() );
if (openssl_verify($content, base64_decode($_SERVER['HTTP_X_SIGNATURE']), $public_key, 'sha256WithRSAEncryption') != 1) {
header('Forbidden', true, 403);
die('Invalid X Signature');
} else {
CE_Lib::log(4, 'Verifying X-Signature');
}
$invoice_number = abs((int)$purchase['reference']);
$this->lock($invoice_number);
$invoice = new Invoice($invoice_number);
if ($invoice->isPaid()) {
return;
}
if ($purchase['status'] == 'paid') {
$paid_amount = number_format($purchase['payment']['amount'] / 100, 2, '.', '');
$purchase_id = $purchase['id'];
$cPlugin = new Plugin($invoice_number, 'chip', $this->user);
$cPlugin->setTransactionID($purchase_id);
$cPlugin->setAmount($paid_amount);
$cPlugin->setAction('charge'); // charge or refund
$transaction = "CHIP: Purchase ID: {$purchase_id} with Invoice No: #{$invoice_number} was successfully PAID through callback";
CE_Lib::log(4, $transaction);
$cPlugin->PaymentAccepted($paid_amount, $transaction);
return;
}
}
private function handle_redirect()
{
$invoice_number = abs((int)$_GET['invoice_number']);
$clientExecURL = CE_Lib::getSoftwareURL();
$invoiceviewURLSuccess = $clientExecURL . "/index.php?fuse=billing&paid=1&controller=invoice&view=invoice&id=" . $invoice_number;
$this->lock($invoice_number);
$invoice = new Invoice($invoice_number);
if ($invoice->isPaid()) {
header('Location:' . $invoiceviewURLSuccess);
return;
}
$selectQuery = "SELECT * "
. "FROM `invoicetransaction` "
. "WHERE `accepted` = 1 "
. "AND `invoiceid` = ? "
. "AND `response` LIKE 'CHIP payment of%' "
. "AND `action` = 'NA' "
. "ORDER BY `id` ASC LIMIT 1 ";
$result = $this->db->query($selectQuery, $invoice_number);
$invoicetransaction = $result->fetch();
$purchase_id = $invoicetransaction['transactionid'];
$cPlugin = new Plugin($invoicetransaction['invoiceid'], 'chip', $this->user);
$brand_id = trim($cPlugin->GetPluginVariable('plugin_chip_Brand ID'));
$secret_key = trim($cPlugin->GetPluginVariable('plugin_chip_Secret Key'));
$chip = ChipApi::get_instance($secret_key, $brand_id);
$purchase = $chip->get_payment($purchase_id);
if ($purchase['status'] == 'paid') {
$paid_amount = number_format($purchase['payment']['amount'] / 100, 2, '.', '');
$cPlugin->setTransactionID($purchase['id']);
$cPlugin->setAmount($paid_amount);
$cPlugin->setAction('charge'); // charge or refund
$transaction = "CHIP: Purchase ID: {$purchase_id} with Invoice No: #{$invoice_number} was successfully PAID";
CE_Lib::log(4, $transaction);
$cPlugin->PaymentAccepted($paid_amount, $transaction);
header('Location:' . $invoiceviewURLSuccess);
return;
}
header('Location:' . $clientExecURL . "/index.php?fuse=billing&cancel=1&controller=invoice&view=invoice&id=" . $invoice_number);
return;
}
private function lock($invoice_number)
{
$result = $this->db->query("SELECT GET_LOCK('invoice_{$invoice_number}', 5);");
$result->fetch();
}
}