Various changes and fixes (#7424)
[openemr.git] / interface / patient_file / front_payment_cc.php
blob7abde7d653646d226bc15f6ca633fa76fa5a3beb
1 <?php
3 /**
4 * Front Payment CC and Terminal Readers support.
6 * @package OpenEMR
7 * @link http://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2021 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 $ignoreAuth = false;
14 require_once(__DIR__ . "/../globals.php");
16 use OpenEMR\Billing\PaymentGateway;
17 use OpenEMR\Common\Crypto\CryptoGen;
18 use Stripe\Customer;
19 use Stripe\PaymentIntent;
20 use Stripe\Stripe;
21 use Stripe\Terminal\ConnectionToken;
22 use Stripe\Terminal\Location;
24 if ($_POST['mode'] == 'AuthorizeNet') {
25 $form_pid = $_POST['form_pid'];
26 $pay = new PaymentGateway("AuthorizeNetApi_Api");
27 $transaction['amount'] = $_POST['payment'];
28 $transaction['currency'] = "USD";
29 $transaction['opaqueDataDescriptor'] = $_POST['dataDescriptor'];
30 $transaction['opaqueDataValue'] = $_POST['dataValue'];
31 try {
32 $response = $pay->submitPaymentToken($transaction);
33 if (is_string($response)) {
34 echo $response;
35 exit();
37 $r = $response->getParsedData();
38 $cc = array();
39 $cc["cardHolderName"] = $_POST["cardHolderName"];
40 $cc['status'] = $response->isSuccessful() ? "ok" : "failed";
41 $cc['authCode'] = $r->transactionResponse->authCode;
42 $cc['transId'] = $r->transactionResponse->transId;
43 $cc['cardNumber'] = $r->transactionResponse->accountNumber;
44 $cc['cc_type'] = $r->transactionResponse->accountType;
45 $cc['zip'] = $_POST["zip"];
46 $ccaudit = json_encode($cc);
47 } catch (\Exception $ex) {
48 return $ex->getMessage();
51 if (!$response->isSuccessful()) {
52 echo $response->getMessage();
53 exit();
56 echo text($ccaudit);
57 exit();
60 if ($_POST['mode'] == 'Stripe') {
61 $pd = sqlQuery("SELECT " .
62 "p.fname, p.mname, p.lname, p.pubpid, p.pid, i.copay " .
63 "FROM patient_data AS p " .
64 "LEFT OUTER JOIN insurance_data AS i ON " .
65 "i.pid = p.pid AND i.type = 'primary' " .
66 "WHERE p.pid = ? ORDER BY i.date DESC LIMIT 1", array($pid));
67 $pay = new PaymentGateway("Stripe");
68 $transaction['amount'] = $_POST['payment'];
69 $transaction['currency'] = "USD";
70 $transaction['token'] = $_POST['stripeToken'];
71 $transaction['description'] = $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'];
72 $transaction['metadata'] = [
73 'Patient' => $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'],
74 'MRN' => $pd['pubpid'],
75 'Invoice Items (date encounter)' => $_POST['encs'],
76 'Invoice Total' => $transaction['amount']
78 try {
79 $response = $pay->submitPaymentToken($transaction);
80 if (is_string($response)) {
81 echo $response;
82 exit();
84 $r = $response->getSource();
85 $cc = array();
86 $cc["cardHolderName"] = $_POST["cardHolderName"];
87 $cc['status'] = $response->isSuccessful() ? "ok" : "failed";
88 $cc['authCode'] = $r['fingerprint'];
89 $cc['transId'] = $response->getTransactionReference();
90 $cc['cardNumber'] = "******** " . $r['last4'];
91 $cc['cc_type'] = $r['brand'];
92 $cc['zip'] = $r->address_zip;
93 $ccaudit = json_encode($cc);
94 } catch (\Exception $ex) {
95 echo $ex->getMessage();
98 if (!$response->isSuccessful()) {
99 echo $response;
100 exit();
103 echo $ccaudit;
104 exit();
107 if ($_GET['mode'] == 'terminal_token') {
108 $cryptoGen = new CryptoGen();
109 $apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
110 Stripe::setApiKey($apiKey);
112 header('Content-Type: application/json');
114 try {
115 $connectionToken = ConnectionToken::create();
116 echo json_encode(array('secret' => $connectionToken->secret), JSON_THROW_ON_ERROR);
117 } catch (\Exception $e) {
118 http_response_code(500);
119 echo json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR);
122 if ($_GET['mode'] == 'cancel_intent') {
123 $cryptoGen = new CryptoGen();
124 $apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
125 Stripe::setApiKey($apiKey);
127 header('Content-Type: application/json');
129 try {
130 $json_str = file_get_contents('php://input');
131 $json_obj = json_decode($json_str);
133 $intent = PaymentIntent::retrieve($json_obj->id);
134 $rtn = $intent->cancel();
136 echo json_encode(['status' => (string)$rtn->status]);
137 } catch (\Exception $e) {
138 http_response_code(500);
139 echo json_encode(['error' => $e->getMessage()]);
143 if ($_GET['mode'] == 'terminal_capture') {
144 $cryptoGen = new CryptoGen();
145 $apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
146 Stripe::setApiKey($apiKey);
148 header('Content-Type: application/json');
150 try {
151 // retrieve JSON from POST body
152 $json_str = file_get_contents('php://input');
153 $json_obj = json_decode($json_str);
155 $intent = PaymentIntent::retrieve($json_obj->id);
156 $intent = $intent->capture();
158 echo json_encode($intent);
159 } catch (\Exception $e) {
160 http_response_code(500);
161 echo json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR);
165 if ($_GET['mode'] == 'terminal_create') {
166 $cryptoGen = new CryptoGen();
167 $apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
168 Stripe::setApiKey($apiKey);
170 header('Content-Type: application/json');
172 try {
173 $json_str = file_get_contents('php://input');
174 $json_obj = json_decode($json_str);
175 $pd = sqlQuery("SELECT " .
176 "p.fname, p.mname, p.lname, p.pubpid,p.pid, p.email, i.copay " .
177 "FROM patient_data AS p " .
178 "LEFT OUTER JOIN insurance_data AS i ON " .
179 "i.pid = p.pid AND i.type = 'primary' " .
180 "WHERE p.pid = ? ORDER BY i.date DESC LIMIT 1", array($pid));
182 $intent = PaymentIntent::create([
183 'amount' => $json_obj->amount,
184 'currency' => 'usd',
185 'payment_method_types' => ['card_present'],
186 'capture_method' => 'manual',
187 'description' => $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'],
188 'metadata' => [
189 'Patient' => $pd['lname'] . ' ' . $pd['fname'] . ' ' . $pd['mname'],
190 'MRN' => $pd['pubpid'],
191 'Invoice Items (date encounter)' => $json_obj->encs,
192 'Invoice Total' => number_format(($json_obj->amount / 100), 2, '.', '')
195 echo json_encode(array('client_secret' => $intent->client_secret), JSON_THROW_ON_ERROR);
196 } catch (\Exception $e) {
197 http_response_code(500);
198 echo json_encode(['error' => $e->getMessage()], JSON_THROW_ON_ERROR);