First impression, update the look of the landing page (#3626)
[openemr.git] / src / Billing / PaymentGateway.php
blob2f41ba598053838b089d751c472e3e3fcaa2823d
1 <?php
3 /**
4 * Payment Gateways for credit card transactions
6 * @package OpenEMR
7 * @link https://www.open-emr.org
8 * @author Jerry Padgett <sjpadgett@gmail.com>
9 * @copyright Copyright (c) 2019 Jerry Padgett <sjpadgett@gmail.com>
10 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
13 namespace OpenEMR\Billing;
15 use Omnipay\Omnipay;
16 use Omnipay\Common\CreditCard;
17 use OpenEMR\Common\Crypto\CryptoGen;
19 class PaymentGateway
21 private $gateway;
22 private $card;
23 private $apiKey;
24 private $transactionKey;
25 private $production;
27 public function __construct($name)
29 $this->production = !$GLOBALS['gateway_mode_production'];
31 $cryptoGen = new CryptoGen();
32 $this->apiKey = $cryptoGen->decryptStandard($GLOBALS['gateway_api_key']);
33 $this->transactionKey = $cryptoGen->decryptStandard($GLOBALS['gateway_transaction_key']);
35 // Setup payment Gateway
36 $this->setGateway($name);
39 public function setApiKey($key)
41 $this->apiKey = $key;
44 public function setTransactionKey($key)
46 $this->transactionKey = $key;
49 public function setProduction($tf)
51 $this->production = $tf;
54 /**
55 * @param $card
56 * @return bool|string
57 * $card = [];
58 * $card['card'] = '';
59 * $card['expiremonth'] = '';
60 * $card['expireyear'] = '';
61 * $card['cvv'] = '';
63 public function setCard($card)
65 try {
66 $ccard = new CreditCard($card);
67 $ccard->validate();
68 $this->card = $card;
69 return true;
70 } catch (\Exception $ex) {
71 return $ex->getMessage();
75 /**
76 * @param $pay
77 * @return bool|string
79 public function submitPaymentCard($pay)
81 try {
82 // Send purchase request
83 $response = $this->gateway->purchase(
85 'amount' => $pay['amount'],
86 'currency' => $pay['currency'],
87 'card' => $this->card
89 )->send();
90 // Process response
91 if ($response->isSuccessful()) {
92 return $response;
93 } elseif ($response->isRedirect()) {
94 // Redirect to offsite payment gateway
95 return $response->getMessage();
96 } else {
97 // Payment failed
98 return $response->getMessage();
100 } catch (\Exception $ex) {
101 return $ex->getMessage();
106 * @param $pay
107 * @return bool|string
109 public function submitPaymentToken($pay)
111 try {
112 // Send purchase request with card token
113 $response = $this->gateway->purchase($pay)->send();
114 // Process response
115 if ($response->isSuccessful()) {
116 return $response;
117 } elseif ($response->isRedirect()) {
118 // Redirect to offsite payment gateway
119 return $response->getMessage();
120 } else {
121 // Payment failed
122 return $response->getMessage();
124 } catch (\Exception $ex) {
125 return $ex->getMessage();
130 * @param $which
131 * @return string
133 public function setGateway($which)
135 if (isset($this->gateway)) {
136 unset($this->gateway);
138 try {
139 if (stripos($which, "stripe") !== false) {
140 $gatewayName = 'Stripe';
141 $this->gateway = Omnipay::create($gatewayName);
142 $this->gateway->setApiKey($this->apiKey);
143 } else {
144 $gatewayName = 'AuthorizeNetApi_Api';
145 $this->gateway = Omnipay::create($gatewayName);
146 $this->gateway->setAuthName($this->apiKey);
147 $this->gateway->setTransactionKey($this->transactionKey);
148 $this->gateway->setTestMode($this->production);
150 } catch (\Exception $ex) {
151 return $ex->getMessage();