/
front_payment_cc.php
200 lines (175 loc) · 6.74 KB
/
front_payment_cc.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
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* Front Payment CC and Terminal Readers support.
*
* @package OpenEMR
* @link http://www.open-emr.org
* @author Jerry Padgett <sjpadgett@gmail.com>
* @copyright Copyright (c) 2021 Jerry Padgett <sjpadgett@gmail.com>
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*/
$ignoreAuth = false;
require_once(__DIR__ . "/../globals.php");
use OpenEMR\Billing\PaymentGateway;
use OpenEMR\Common\Crypto\CryptoGen;
use Stripe\Customer;
use Stripe\PaymentIntent;
use Stripe\Stripe;
use Stripe\Terminal\ConnectionToken;
use Stripe\Terminal\Location;
if ($_POST['mode'] == 'AuthorizeNet') {
$form_pid = $_POST['form_pid'];
$pay = new PaymentGateway("AuthorizeNetApi_Api");
$transaction['amount'] = $_POST['payment'];
$transaction['currency'] = "USD";
$transaction['opaqueDataDescriptor'] = $_POST['dataDescriptor'];
$transaction['opaqueDataValue'] = $_POST['dataValue'];
try {
$response = $pay->submitPaymentToken($transaction);
if (is_string($response)) {
echo $response;
exit();
}
$r = $response->getParsedData();
$cc = array();
$cc["cardHolderName"] = $_POST["cardHolderName"];
$cc['status'] = $response->isSuccessful() ? "ok" : "failed";
$cc['authCode'] = $r->transactionResponse->authCode;
$cc['transId'] = $r->transactionResponse->transId;
$cc['cardNumber'] = $r->transactionResponse->accountNumber;
$cc['cc_type'] = $r->transactionResponse->accountType;
$cc['zip'] = $_POST["zip"];
$ccaudit = json_encode($cc);
} catch (\Exception $ex) {
return $ex->getMessage();
}
if (!$response->isSuccessful()) {
echo $response->getMessage();
exit();
}
echo text($ccaudit);
exit();
}
if ($_POST['mode'] == 'Stripe') {
$pd = sqlQuery("SELECT " .
"p.fname, p.mname, p.lname, p.pubpid, p.pid, i.copay " .
"FROM patient_data AS p " .
"LEFT OUTER JOIN insurance_data AS i ON " .
"i.pid = p.pid AND i.type = 'primary' " .
"WHERE p.pid = ? ORDER BY i.date DESC LIMIT 1", array($pid));
$pay = new PaymentGateway("Stripe");
$transaction['amount'] = $_POST['payment'];
$transaction['currency'] = "USD";
$transaction['token'] = $_POST['stripeToken'];
$transaction['description'] = $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'];
$transaction['metadata'] = [
'Patient' => $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'],
'MRN' => $pd['pubpid'],
'Invoice Items (date encounter)' => $_POST['encs'],
'Invoice Total' => $transaction['amount']
];
try {
$response = $pay->submitPaymentToken($transaction);
if (is_string($response)) {
echo $response;
exit();
}
$r = $response->getSource();
$cc = array();
$cc["cardHolderName"] = $_POST["cardHolderName"];
$cc['status'] = $response->isSuccessful() ? "ok" : "failed";
$cc['authCode'] = $r['fingerprint'];
$cc['transId'] = $response->getTransactionReference();
$cc['cardNumber'] = "******** " . $r['last4'];
$cc['cc_type'] = $r['brand'];
$cc['zip'] = $r->address_zip;
$ccaudit = json_encode($cc);
} catch (\Exception $ex) {
echo $ex->getMessage();
}
if (!$response->isSuccessful()) {
echo $response;
exit();
}
echo $ccaudit;
exit();
}
if ($_GET['mode'] == 'terminal_token') {
$cryptoGen = new CryptoGen();
$apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
Stripe::setApiKey($apiKey);
header('Content-Type: application/json');
try {
$connectionToken = ConnectionToken::create();
echo json_encode(array('secret' => $connectionToken->secret), JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR);
}
}
if ($_GET['mode'] == 'cancel_intent') {
$cryptoGen = new CryptoGen();
$apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
Stripe::setApiKey($apiKey);
header('Content-Type: application/json');
try {
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$intent = PaymentIntent::retrieve($json_obj->id);
$rtn = $intent->cancel();
echo json_encode(['status' => (string)$rtn->status]);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
}
if ($_GET['mode'] == 'terminal_capture') {
$cryptoGen = new CryptoGen();
$apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
Stripe::setApiKey($apiKey);
header('Content-Type: application/json');
try {
// retrieve JSON from POST body
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$intent = PaymentIntent::retrieve($json_obj->id);
$intent = $intent->capture();
echo json_encode($intent);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR);
}
}
if ($_GET['mode'] == 'terminal_create') {
$cryptoGen = new CryptoGen();
$apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
Stripe::setApiKey($apiKey);
header('Content-Type: application/json');
try {
$json_str = file_get_contents('php://input');
$json_obj = json_decode($json_str);
$pd = sqlQuery("SELECT " .
"p.fname, p.mname, p.lname, p.pubpid,p.pid, p.email, i.copay " .
"FROM patient_data AS p " .
"LEFT OUTER JOIN insurance_data AS i ON " .
"i.pid = p.pid AND i.type = 'primary' " .
"WHERE p.pid = ? ORDER BY i.date DESC LIMIT 1", array($pid));
$intent = PaymentIntent::create([
'amount' => $json_obj->amount,
'currency' => 'usd',
'payment_method_types' => ['card_present'],
'capture_method' => 'manual',
'description' => $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'],
'metadata' => [
'Patient' => $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'],
'MRN' => $pd['pubpid'],
'Invoice Items (date encounter)' => $json_obj->encs,
'Invoice Total' => number_format(($json_obj->amount / 100), 2, '.', '')
]
]);
echo json_encode(array('client_secret' => $intent->client_secret), JSON_THROW_ON_ERROR);
} catch (\Exception $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR);
}
}