Skip to content

Commit

Permalink
Merge pull request #7 from yaser24276/master
Browse files Browse the repository at this point in the history
important changes
  • Loading branch information
max2u committed Sep 10, 2018
2 parents a78ccd0 + 4941a1d commit 0dc177d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 65 deletions.
Expand Up @@ -17,7 +17,8 @@ public function __construct()
}

// Define user set variables
$this->title = 'PointCheckout';
$this->method_title = __( 'PointCheckout', 'woocommerce' );
$this->title ='PointCheckout';
$this->description = __('Pay for your cart using PointCheckout payment method '.'<a href="https://www.pointcheckout.com/info/what-is-pointcheckout" target="_blank" style="font-size:10px;"> more details about PointCheckout?</a>', 'pointcheckout_pointcheckoutpay');
$this->pfConfig = PointCheckout_PointCheckoutPay_Config::getInstance();
$this->pfPayment = PointCheckout_PointCheckoutPay_Payment::getInstance();
Expand All @@ -30,7 +31,7 @@ public function __construct()

// Save options
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_wc_gateway_pointcheckout_process_response', array(&$this, 'process_response'));
add_action('woocommerce_wc_gateway_pointcheckout_process_response', array($this, 'process_response'));
}

function process_admin_options() {
Expand Down Expand Up @@ -276,7 +277,9 @@ function process_payment($order_id)
update_post_meta($order->id, '_payment_method', POINTCHECKOUT_PAY_PAYMENT_METHOD);
}
$form = $this->pfPayment->getPaymentRequestForm();
$result = array('result' => 'success', 'form' => $form);
$note = $this->pfPayment->getPointCheckoutOrderHistoryMessage($form['response']->result->checkoutId, 0, $form['response']->result->status, '');
$order->add_order_note( $note );
$result = array('result' => 'success', 'form' => $form['form']);
if (isset($_POST['woocommerce_pay']) && isset($_POST['_wpnonce']) && wp_verify_nonce($_POST['_wpnonce'], 'woocommerce-pay')) {
wp_send_json($result);
exit;
Expand All @@ -301,14 +304,12 @@ private function _handleResponse()
$order = wc_get_order($_REQUEST['reference']);
if ($success['success']) {
$order->payment_complete();
$order->add_order_note('PointCheckout payment confirmed');
WC()->session->set('refresh_totals', true);
$redirectUrl = $this->get_return_url($order);
}
else {
$redirectUrl = esc_url($woocommerce->cart->get_checkout_url());
$order->cancel_order();
$order->add_order_note('PointCheckout payment Failed');
}
echo '<script>window.top.location.href = "' . $redirectUrl . '"</script>';
exit;
Expand Down
28 changes: 12 additions & 16 deletions pointcheckout_pointcheckoutpay/lib/pointcheckout/classes/Config.php
Expand Up @@ -15,15 +15,13 @@ class PointCheckout_PointCheckoutPay_Config extends PointCheckout_PointCheckoutP
private $Api_Key;
private $command;
private $Mode;
private $gatewayCurrency;
private $successOrderStatusId;
private $orderPlacement;
private $status;
private $logFileDir;
private $allowSpecific;
private $specific_countries;
private $allowUserSpecific;
private $specific_uesr_roles;
private $new_order_status;


public function __construct()
Expand All @@ -39,14 +37,12 @@ public function __construct()
$this->command = $this->_getShoppingCartConfig('command');
$this->Api_Secret = $this->_getShoppingCartConfig('Api_Secret');
$this->Mode = $this->_getShoppingCartConfig('mode');
$this->gatewayCurrency = 'base';
$this->successOrderStatusId = '';
$this->orderPlacement = $this->_getShoppingCartConfig('order_placement');
$this->status = $this->enabled;
$this->allowSpecific = $this->_getShoppingCartConfig('allow_specific');
$this->specific_countries = $this->_getShoppingCartConfig('specific_countries');
$this->allowUserSpecific = $this->_getShoppingCartConfig('allow_user_specific');
$this->specific_uesr_roles = $this->_getShoppingCartConfig('specific_user_roles');
$this->specific_uesr_roles = $this->_getShoppingCartConfig('specific_user_roles');
$this->new_order_status = $this->_getShoppingCartConfig('new_order_status');
}

/**
Expand Down Expand Up @@ -83,22 +79,15 @@ public function getMode()
}


public function getGatewayCurrency()
{
return $this->gatewayCurrency;
}




public function getSuccessOrderStatusId()
{
return $this->successOrderStatusId;
}

public function getStatus()
{
return $this->status;
}


public function isActive()
{
Expand All @@ -124,6 +113,13 @@ public function getSpecificUserRoles(){
return $this->specific_uesr_roles;
}


public function getNewOrderStatus(){
return $this->new_order_status;
}



public function getOrderPlacement()
{
return $this->orderPlacement;
Expand Down
113 changes: 71 additions & 42 deletions pointcheckout_pointcheckoutpay/lib/pointcheckout/classes/Payment.php
Expand Up @@ -29,8 +29,9 @@ public static function getInstance()
public function getPaymentRequestParams()
{
$orderId = $this->pfOrder->getSessionOrderId();
$order = wc_get_order($orderId);
$order = new WC_order($orderId);
$this->pfOrder->loadOrder($orderId);
$order->update_status($this->pfConfig->getNewOrderStatus());

$gatewayParams = array(
'referenceId' => $orderId,
Expand Down Expand Up @@ -94,7 +95,10 @@ public function getPaymentRequestParams()
*/
public function getPaymentRequestForm()
{


if(!$this->pfConfig->isEnabled()){
return null;
}
$paymentRequestParams = $this->getPaymentRequestParams();
$response = $this->PointCheckoutApiCall($paymentRequestParams);
if (($response->success == 'true' && $response->result->checkoutKey != null )) {
Expand All @@ -109,7 +113,12 @@ public function getPaymentRequestForm()
$this->pfOrder->clearSessionCurrentOrder();
$form = '<form style="display:none" name="frm_pointcheckout_payment" id="frm_pointcheckout_payment" method="GET" action="' . $actionUrl . '">';
$form .= '<input type="submit">';
return $form;
$formArray = array(
'form' => $form,
'response' => $response

);
return $formArray;


}
Expand Down Expand Up @@ -137,7 +146,7 @@ public function PointCheckoutApiCall($paymentRequestParams){
$response = curl_exec($ch);

}catch(Exception $e){
$this->paymentLog('Failed while sending first request to pointchckout resone: '.$e->getMessage());
$this->paymentLog('Failed to connect pointchckout resone: '.$e->getMessage());
throw $e;
}
return json_decode($response);
Expand All @@ -164,7 +173,7 @@ public function PointCheckoutSecoundCall(){
$response = curl_exec($ch);

}catch(Exception $e){
$this->paymentLog('Failed while sending secound request to pointchckout resone: '.$e->getMessage());
$this->paymentLog('Failed to connect to pointchckout resone: '.$e->getMessage());
throw $e;
}
return $response;
Expand Down Expand Up @@ -198,61 +207,49 @@ public function getGatewayUrl(){
public function handlePointCheckoutResponse()
{
$response = $this->PointCheckoutSecoundCall();
$response_info = json_decode($response);
$order = new WC_Order($_REQUEST['reference']);


if (!empty($order)) {


if (!$response && $response_info->success != true){
$order->update_status('canceled');
return array(
'success' => false,
'referenceId' => WC()->session->get('pointCheckoutCurrentOrderId')
);
if($response){
$order->update_status('canceled');
$this->paymentLog('ERROR '.$response_info->error);
$note = __("[ERROR] order canceled :".$response_info->error);
// Add the note
$order->add_order_note( $note );
// Save the data
$order->save();
}
}elseif ($response_info->result->status != 'PAID' ){
$response_info = json_decode($response);
if (!$response || $response_info->success != true){
$order->update_status('canceled');
if($response_info->result->status == 'CANCELLED'){
$note = __("payment canceled by user order cancelled");
}else{
$note = __("trying to confirm an order with payment status (".$response_info->result->status.") order 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' => $response_info->referenceId
'referenceId' => isset($response_info->referenceId)?$response_info->referenceId:''
);
$this->paymentLog('ERROR -- Can not complete a non paid payment for order Id : '.$response_info->referenceId);
}
//$order->update_status( $this->pfConfig->getPaymentSuccessOrderStatus());
if($response_info->result->cod > 0){
$note = __("Payment COD amount :".$response_info->result->cod);

// Add the note
$order->add_order_note( $note );

// Save the data
$order->save();
}
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' => true,
'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)
Expand All @@ -266,7 +263,39 @@ public function paymentLog($messages, $forceDebug = false)
$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;

}


}

?>
?>
4 changes: 2 additions & 2 deletions pointcheckout_pointcheckoutpay/woocommerce-pointcheckout.php
@@ -1,8 +1,8 @@
<?php

/* Plugin Name: PointCheckout
* Description: PointCheckout allows you to accept reward points as payments -- PointCheckout will take care of paying you with your local currency -- and that would means more and more customers visiting your store <a href="https://www.pointcheckout.com/home/merchant" target="_blank"><span>visit website</span></a>
* Version: 0.1.1
* Description: PointCheckout allows your customer to pay for there cart using reward points from different programs as payments -- and that would means more and more customers visiting your store <a href="https://www.pointcheckout.com/home/merchant" target="_blank"><span>more details</span></a>
* Version: 1.0.0
* Author: PointCheckout
* Author URI: https://www.pointcheckout.com/
*/
Expand Down

0 comments on commit 0dc177d

Please sign in to comment.