-
Notifications
You must be signed in to change notification settings - Fork 5
/
Payment.php
executable file
·301 lines (260 loc) · 11.6 KB
/
Payment.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
<?php
class PointCheckout_PointCheckoutPay_Payment extends PointCheckout_PointCheckoutPay_Super
{
private static $instance;
private $pfConfig;
private $pfOrder;
private $log;
public function __construct()
{
parent::__construct();
$this->pfConfig = PointCheckout_PointCheckoutPay_Config::getInstance();
$this->pfOrder = new PointCheckout_PointCheckoutPay_Order();
$this->log = wc_get_logger();
}
/**
* @return PointCheckout_PointCheckoutPay_Payment
*/
public static function getInstance()
{
if (self::$instance === null) {
self::$instance = new PointCheckout_PointCheckoutPay_Payment();
}
return self::$instance;
}
public function getPaymentRequestParams()
{
$orderId = $this->pfOrder->getSessionOrderId();
$order = new WC_order($orderId);
$this->pfOrder->loadOrder($orderId);
$order->update_status($this->pfConfig->getNewOrderStatus());
$gatewayParams = array(
'referenceId' => $orderId,
);
$cartItems= $order->get_items();
$items = array();
$i = 0;
foreach ($cartItems as $item_id => $item_data){
$product =$item_data->get_product();
$item = (object) array(
'name'=> $product->get_name(),
'sku' => $product->get_sku(),
'quantity' => $item_data->get_quantity(),
'type'=>$product->get_type(),
'total' => $item_data->get_total());
//in case of bundles the bundle group item total is set to zero here to prevent conflict in totals
if($product->get_type()=='bundle'){
$item->total =0;
}
$items[$i++] = $item;
}
$gatewayParams['items']=array_values($items);
$gatewayParams['grandtotal']= $this->pfOrder->getTotal();
$gatewayParams['tax'] =$this->pfOrder->getTaxAmount();
$gatewayParams['shipping'] =$this->pfOrder->getShippingAmount();
$gatewayParams['subtotal'] =$this->pfOrder->getSubtotal();
$gatewayParams['discount'] =$this->pfOrder->getDiscountAmount();
$gatewayParams['currency'] =$this->pfOrder->getCurrencyCode();
$customer = array();
$billingAddress = array();
$billingAddress['name'] = $order->get_billing_first_name().' '.$order->get_billing_last_name();
$billingAddress['address1'] = $order->get_billing_address_1();
$billingAddress['address2'] = $order->get_billing_address_2();
$billingAddress['city'] = $order->get_billing_city();
$billingAddress['country'] = $order->get_billing_country();
$shippingAddress = array();
$shippingAddress['name'] = $order->get_shipping_first_name().' '.$order->get_shipping_last_name();
$shippingAddress['address1'] = $order->get_shipping_address_1();
$shippingAddress['address2'] = $order->get_shipping_address_2();
$shippingAddress['city'] = $order->get_shipping_city();
$shippingAddress['country'] = $order->get_shipping_country();
$customer['billingAddress'] = $billingAddress;
$customer['shippingAddress'] = $shippingAddress;
$customer['firstname'] = $order->get_billing_first_name();
$customer['lastname'] = $order->get_billing_last_name();
$customer['email'] = $order->get_billing_email();
$customer['phone'] = $order->get_billing_phone();
$gatewayParams['customer'] = $customer;
return $gatewayParams;
}
/**
* build payment form
*/
public function getPaymentRequestForm()
{
if(!$this->pfConfig->isEnabled()){
return null;
}
$paymentRequestParams = $this->getPaymentRequestParams();
$response = $this->PointCheckoutApiCall($paymentRequestParams);
if (($response->success == 'true' && $response->result->checkoutKey != null )) {
$actionUrl = $this->getGatewayUrl().$response->result->checkoutKey;
$returnUrl = get_site_url().'?wc-api=wc_gateway_pointcheckout_process_response';
WC()->session->set('checkoutId',$response->result->checkoutId);
}else{
$this->paymentLog('Failed while sending first request to pointchckout resone: '.$response->error);
wc_add_notice( sprintf( __( 'Failed to process payment please try again later', 'error' )));
$actionUrl = get_site_url().'/index.php/checkout';
}
$this->pfOrder->clearSessionCurrentOrder();
$form = '<form style="display:none" name="frm_pointcheckout_payment" id="frm_pointcheckout_payment" method="GET" action="' . $actionUrl . '">';
$form .= '<input type="submit">';
$formArray = array(
'form' => $form,
'response' => $response
);
return $formArray;
}
/**
* first call that to get checkout key from pointcheckout
*/
public function PointCheckoutApiCall($paymentRequestParams){
$info = json_encode($paymentRequestParams);
try {
// create a new cURL resource
$headers = array(
'Content-Type: application/json',
'Api-Key:'.$this->pfConfig->getApiKey(),
'Api-Secret:'.$this->pfConfig->getApiSecret()
);
$_BASE_URL=$this->getGatewayApiUrl();
$ch = curl_init($_BASE_URL);
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_POSTFIELDS ,json_encode($paymentRequestParams));
// excecute first call
$response = curl_exec($ch);
}catch(Exception $e){
$this->paymentLog('Failed to connect pointchckout resone: '.$e->getMessage());
throw $e;
}
return json_decode($response);
}
public function PointCheckoutSecoundCall(){
try {
// create a new cURL resource
$headers = array(
'Content-Type: application/json',
'Api-Key:'.$this->pfConfig->getApiKey(),
'Api-Secret:'.$this->pfConfig->getApiSecret()
);
WC()->session->set('pointCheckoutCurrentOrderId',$_REQUEST['reference']);
$_BASE_URL=$this->getGatewayApiUrl().$_REQUEST['checkout'];
$ch = curl_init($_BASE_URL);
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
//execute secound call
$response = curl_exec($ch);
}catch(Exception $e){
$this->paymentLog('Failed to connect to pointchckout resone: '.$e->getMessage());
throw $e;
}
return $response;
}
public function getGatewayApiUrl(){
$liveMode = $this->pfConfig->isLiveMode();
if ($liveMode) {
return $gatewayUrl = 'https://pay.pointcheckout.com/api/v1.0/checkout/';
}elseif($this->pfConfig->isStagingMode()){
return $gatewayUrl = 'https://pay.staging.pointcheckout.com/api/v1.0/checkout/';
}else {
return $gatewayUrl = 'https://pay.test.pointcheckout.com/api/v1.0/checkout/';
}
}
public function getGatewayUrl(){
$liveMode = $this->pfConfig->isLiveMode();
if ($liveMode) {
return $gatewayUrl = 'https://pay.pointcheckout.com/checkout/';
}elseif($this->pfConfig->isStagingMode()){
return $gatewayUrl = 'https://pay.staging.pointcheckout.com/checkout/';
}else {
return $gatewayUrl = 'https://pay.test.pointcheckout.com/checkout/';
}
}
/**
* @retrun array
*/
public function handlePointCheckoutResponse()
{
$response = $this->PointCheckoutSecoundCall();
$order = new WC_Order($_REQUEST['reference']);
if (!empty($order)) {
$response_info = json_decode($response);
if (!$response || $response_info->success != true){
$order->update_status('canceled');
$errorMsg = isset($response_info->error)?$response_info->error:'connecting to pointcheckout failed';
$note = __("[ERROR] order canceled :".$errorMsg);
// Add the note
$order->add_order_note( $note );
// Save the data
$order->save();
$this->paymentLog('ERROR '.$errorMsg);
return array(
'success' => false,
'referenceId' => isset($response_info->referenceId)?$response_info->referenceId:''
);
}
if ($response_info->success == true && $response_info->result->status != 'PAID' ){
$order->update_status('canceled');
$note = __($this->getPointCheckoutOrderHistoryMessage($response_info->result->checkoutId,0,$response_info->result->status ,$response_info->result->currency));
// Add the note
$order->add_order_note( $note );
// Save the data
$order->save();
return array(
'success' => false,
'referenceId' => $response_info->referenceId
);
}
$note = __($this->getPointCheckoutOrderHistoryMessage($response_info->result->checkoutId,$response_info->result->cod,$response_info->result->status ,$response_info->result->currency));
// Add the note
$order->add_order_note( $note );
// Save the data
$order->save();
return array(
'success' => true,
'referenceId' => $response_info->referenceId
);
}
}
public function paymentLog($messages, $forceDebug = false)
{
if ( ! class_exists( 'WC_Logger' ) ) {
include_once( 'class-wc-logger.php' );
}
if ( empty( $this->log ) ) {
$this->log = new WC_Logger();
}
$this->log->add( 'pointcheckout_pay', $messages );
}
public function getPointCheckoutOrderHistoryMessage($checkout,$codAmount,$orderStatus,$currency) {
switch($orderStatus){
case 'PAID':
$color='style="color:green;"';
break;
case 'PENDING':
$color='style="color:blue;"';
break;
default:
$color='style="color:red;"';
}
$message = 'PointCheckout Status: <b '.$color.'>'.$orderStatus.'</b><br/>PointCheckout Transaction ID: <a href="'.$this->getAdminUrl().'/merchant/transactions/'.$checkout.'/read " target="_blank"><b>'.$checkout.'</b></a>'."\n" ;
if($codAmount>0){
$message.= '<b style="color:red;">[NOTICE] </b><i>COD Amount: <b>'.$codAmount.' '.$this->session->data['currency'].'</b></i>'."\n";
}
return $message;
}
private function getAdminUrl(){
if ($this->pfConfig->isStagingMode()){
$_ADMIN_URL='https://admin.staging.pointcheckout.com';
}elseif($this->pfConfig->isLiveMode()){
$_ADMIN_URL='https://admin.pointcheckout.com';
}else{
$_ADMIN_URL='https://admin.test.pointcheckout.com';
}
return $_ADMIN_URL;
}
}
?>