Post by Zayar45 » Fri Oct 04, 2019 2:17 pm

I am trying to update the coding structure of payment module called "MyanPay" that is only available for Opencart version 2.1.0.1.Now I am using the Opencart 3.0.2.0 and want to make changes in order to work with this Opencart version.Can anyone give some advice for me?

I've already made changes "MyanPay" payment module files in admin folder and it worked fine in the admin panel.
But I can't simply change the codes in catalog folder and I am stuck with it.

Here below are the changes I've made in "catalog/controller/extension/payment/myanpay.php".
The rest are in the attached files.

~/catalog/controller/extension/payment/myanpay.php

Code: Select all

<?php
require_once('catalog/controller/checkout/mpaycallerservice.php');
class ControllerExtensionPaymentMyanPay extends Controller {
	public function index() {
		/*+++++++++++++++[ Start Myanmar Currency Set ]+++++++++++++++*/
		$this->load->model('localisation/currency');
		 $results = $this->model_localisation_currency->getCurrencies();
		 $mpaycurstatus = true;
		foreach ($results as $result)
		{
		if ($result['status'])
		 	{
				if ($mpaycurstatus)
		 		{
		 			if ($result['code'] == 'MMK')
		 			{
		 				$mpaycurrency = 'MMK';
		 				$mpaycurstatus = false;
		 			} else {
						//$mpaycurrency = $this->currency->getCode(); // remove
						$mpaycurrency = $this->config->get('config_currency'); //replace
		 				$mpaycurstatus = true;

		 			}
		 		}
		 	}
		 }

		$this->currency->set($mpaycurrency); // remove
		/*+++++++++++++++[ Start Myanmar Currency Set ]+++++++++++++++*/
		
		//$this->data['button_confirm'] = $this->language->get('button_confirm');
		//$this->template = 'default/template/payment/myanpay.twig';
		//$this->render(); 

		$data['button_confirm'] = $this->language->get('button_confirm');
		//$data['action'] = "https://www.myanpay-virtualbox.com/Personal/ExpressCheckout/ExpressCheckoutLogin.aspx?cmd=express-checkout&token="; // test url (random testing)

		//$data['action'] = "https://www.myanpay.com.mm/Personal/ExpressCheckout/ExpressCheckoutLogin.aspx?cmd=express-checkout&token="; //Live url (random testing)

        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . 'extension/payment/myanpay')) {
            return $this->load->view($this->config->get('config_template') . 'extension/payment/myanpay', $data);
        } else {
            return $this->load->view('extension/payment/myanpay', $data);
        }

	}
	
	public function send()	
	{
		$json = array();
		$this->load->model('checkout/order');
		
		/*++++++++++[ Start Total for Shipping, Coupon, Tax and Voucher ]++++++++++*/
		$sort_order = array();
		$total_data = array();
		$total = 0;
		$taxes = $this->cart->getTaxes();
		$this->load->model('setting/extension');
		$results = $this->model_setting_extension->getExtensions('total');
		
		foreach ($results as $key => $value){
			$sort_order[$key] = $this->config->get($value['code'] . '_sort_order');
		}
		array_multisort($sort_order, SORT_ASC, $results);
		
		foreach ($results as $result){
			if ($this->config->get($result['code'] . '_status')){
				$this->load->model('total/' . $result['code']);
				$this->{'model_total_' . $result['code']}->getTotal($total_data, $total, $taxes);
			}
		}
		/*++++++++++[ End Total for Shipping, Coupon, Tax and Voucher ]++++++++++*/
		
		/*+++++++++++++++[ Start nvpLogIn ]+++++++++++++++*/
		$pageURL = 'http';
		if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on")
		{
			$pageURL .= "s";
		}
		$pageURL .= "://";
		
		if ($_SERVER["SERVER_PORT"] != "80")
		{
			$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"];
		} else {
			$pageURL .= $_SERVER["SERVER_NAME"];
		}
		
		$requestURI = explode("?", $_SERVER['REQUEST_URI']);
		$url=dirname($pageURL . $requestURI[0] );
		
		$returnURL =($url . "/" . "index.php?route=checkout/mpaycart");
		$cancelURL =($url . "/" . "index.php?route=checkout/checkout");
		
		$nvpLogIn = "&apiusername=" . urlencode($this->config->get('myanpay_username')) .
		"&apipassword=" . urlencode($this->config->get('myanpay_password')) .
		"&apisignature=" . urlencode($this->config->get('myanpay_signature')) .
		"&version=1.1" .
		"&paymentaction=Sale" .	
		"&returnUrl=" . $returnURL .
		"&cancelUrl=" . $cancelURL ;
		/*+++++++++++++++[ End nvpLogIn ]+++++++++++++++*/
		
		/*+++++++++++++++[ Start nvpProduct ]+++++++++++++++*/
		$w = 0;
		$productAmt =0;
		$nvpProduct = "";
		
		foreach ($this->cart->getProducts() as $product)
		{
			$nvpProduct .= "&paymentRequest_ItemNumber$w=" . $product['model'] .
			"&paymentRequest_ItemName$w=" . $product['name'] .
			"&paymentRequest_ItemAmt$w=" . $this->currency->format($product['price'],'','',false).
			"&paymentRequest_ItemQty$w=" . (int)$product['quantity'] ;
			$w++;
			$productAmt= $productAmt + $this->currency->format($product['price'],'','',false) * (int)$product['quantity'];
		}
		/*+++++++++++++++[ End nvpProduct ]+++++++++++++++*/
		
		/*+++++++++++++++[ Start All Additional  Total ]+++++++++++++++*/		
		$nvpShipping = $nvpCoupon = $nvpTax = $nvpVoucher = "" ;
		$shippingAmt = $couponAmt = $taxAmt = $voucherAmt = $totalAmt = 0;
		$s = $c = $t = $v = 1;
		
		foreach ($total_data as $key=>$total)
		{
			/*+++++++++++++++[ Start nvpShipping ]+++++++++++++++*/
			if($total['code'] == 'shipping')
			{
				$nvpShipping .= "&paymentRequest_ItemNumber$w=SM-00$s"  .
				"&paymentRequest_ItemName$w=" . $total['title'] . 
				"&paymentRequest_ItemAmt$w=" . $this->currency->format($total['value'],'','',false) .
				"&paymentRequest_ItemQty$w=1" ;
				$w++;
				$s++;
				$shippingAmt = $shippingAmt + $this->currency->format($total['value'],'','',false);
			}
			/*+++++++++++++++[ End nvpShipping ]+++++++++++++++*/
			
			/*+++++++++++++++[ Start nvpCoupon ]+++++++++++++++*/
			if($total['code'] == 'coupon')
			{
				$nvpCoupon .= "&paymentRequest_ItemNumber$w=C-00$c"  .
				"&paymentRequest_ItemName$w=" .  $total['title'] . 
				"&paymentRequest_ItemAmt$w=" . $this->currency->format($total['value'],'','',false) .
				"&paymentRequest_ItemQty$w=1" ;
				$w++;
				$c++;
				$couponAmt = $couponAmt + $this->currency->format($total['value'],'','',false);
			}
			/*+++++++++++++++[ End nvpCoupon ]+++++++++++++++*/
			
			/*+++++++++++++++[ Start nvpTax ]+++++++++++++++*/
			if($total['code'] == 'tax')
			{
				$nvpTax .= "&paymentRequest_ItemNumber$w=T-00$t"  .
				"&paymentRequest_ItemName$w=" .  $total['title'] . 
				"&paymentRequest_ItemAmt$w=" . $this->currency->format($total['value'],'','',false) .
				"&paymentRequest_ItemQty$w=1" ;
				$w++;
				$t++;
				$taxAmt = $taxAmt + $this->currency->format($total['value'],'','',false);
			}
			/*+++++++++++++++[ End nvpTax ]+++++++++++++++*/
			
			/*+++++++++++++++[ Start nvpVoucher ]+++++++++++++++*/
			if($total['code'] == 'voucher')
			{
				$nvpVoucher .= "&paymentRequest_ItemNumber$w=V-00$v"  .
				"&paymentRequest_ItemName$w=" .  $total['title'] . 
				"&paymentRequest_ItemAmt$w=" . $this->currency->format($total['value'],'','',false) .
				"&paymentRequest_ItemQty$w=1" ;
				$w++;
				$v++;
				$voucherAmt = $voucherAmt + $this->currency->format($total['value'],'','',false);
			}
			/*+++++++++++++++[ End nvpVoucher ]+++++++++++++++*/
		}
		/*+++++++++++++++[ End All Additional  Total ]+++++++++++++++*/
		
		/*+++++++++++++++[ Start All MPay Data Combine ]+++++++++++++++*/
		$mpayTotalAmt = ($productAmt + $shippingAmt+ $couponAmt + $taxAmt +  $voucherAmt);
		$this->session->data['Set_Amt']= (float)$mpayTotalAmt;
	
		$nvpTotalAmt ="&NoShipping=true" .
		"&PaymentRequest_ItemTotalAmt=" . $mpayTotalAmt .
		"&paymentRequest_Amt=" . $mpayTotalAmt ;
		
		$nvpEC ="&EcHeaderImage=" . urlencode($this->config->get('myanpay_logo')) .
		"&EcBrandName=" . urlencode($this->config->get('myanpay_brandname')) . 
		"&EcCustomerServiceNumber=" . urlencode($this->config->get('myanpay_sellerphone'));
	
		$nvpstr =  $nvpLogIn .$nvpProduct .$nvpShipping .$nvpCoupon . $nvpTax . $nvpVoucher . $nvpTotalAmt . $nvpEC ;
		$nvpstr = htmlspecialchars_decode($nvpstr);
				
		/*+++++++++++++++[ End All MPay Data Combine ]+++++++++++++++*/
				
		/*+++++++++++++++[ Start Myanmar Currency Check ]+++++++++++++++*/
		$this->load->model('localisation/currency');
		$results = $this->model_localisation_currency->getCurrencies();	
		
		$strCurTitle = "";		
		//$strCurCode = $this->currency->getCode(); // remove
		$strCurCode = $this->config->get('config_currency'); // replace
		foreach ($results as $result) 
		{
			if ($result['code'] == $strCurCode)
			{				
				$strCurTitle = $result['title'];
			}
		}
		
		$curcheck = true;
		if($strCurCode != 'MMK')
		{
			$nvpErr = "Now your currency is $strCurTitle ( $strCurCode ).\nMyanPay Payment accepts only Myanmar Kyat. \nPlease add Myanmar Currency (MMK) to currency\nsetting in the admin panel.";			
			$json['error'] = $nvpErr;
			$curcheck = false;
		}
		/*+++++++++++++++[ End Myanmar Currency Check ]+++++++++++++++*/
		
		if ($curcheck) 
		{
			$resArray=hash_call("SetExpressCheckout",$nvpstr,$this->config->get('myanpay_mode'));			
			//var_dump($resArray);
			//exit();
			if (isset($resArray["mpayerr"]) && ($resArray["mpayerr"] == "ErrorOccur"))
			{
				$json['mpayerrurl'] = $resArray["mpayerrurl"] ;
			} else  {
				/*++++++++++[ Start ack Process ]++++++++++*/
				$ack = '';
				if (isset($resArray["Ack"])) $ack = strtoupper($resArray["Ack"]);
				$strTime = '[ ' . date("h:i:s A") . ' ] - ';
				if($ack=="SUCCESS")
				{
					$token = urldecode($resArray["Token"]);
					
					if($this->config->get('myanpay_mode') == "1")
					{
						$myanpayURL = "https://www.myanpay.com.mm/Personal/ExpressCheckout/ExpressCheckoutLogin.aspx?cmd=express-checkout&token=" .$token;
					} else  {
						$myanpayURL = "https://www.myanpay-virtualbox.com/Personal/ExpressCheckout/ExpressCheckoutLogin.aspx?cmd=express-checkout&token=" .$token;
					}					
					$json['success'] = $myanpayURL;
					
					$this->model_checkout_order->confirm($this->session->data['order_id'], $this->config->get('config_order_status_id'), $strTime . 'Start MyanPay ExpressCheckout', true);					
				} else if($ack=="FAIL") {
					$nvpErr = "The MyanPay API has returned an error! \n";
					foreach($resArray as $key => $value)
					{
						$nvpErr .= " $key : $value \n";
					}
					$json['error'] = $nvpErr;
				} else {
					$nvpErr = "Unable to Establish MyanPay Payment Server! \n";
					$nvpErr.= "Your request cannot be completed because the service could not be found or did not respond. \n";	
					$nvpErr.= "Please try again for your payment process. \n";	
					$json['error'] = $nvpErr;
				}
				/*++++++++++[ Start ack Process ]++++++++++*/
			}
		}
		
		$this->response->setOutput(json_encode($json));
	}
}


Newbie

Posts

Joined
Fri Jul 12, 2019 6:34 pm

Post by kestas » Fri Oct 04, 2019 2:53 pm

if you are familiar with coding, but do not know how OC works and want update for example payment module, the best way to take one module from old version and the same another one from new version and compare it. You'll find out what you need to change.

Custom OpenCart modules and solutions. You can write PM with additional questions... Extensions you can find here


Active Member

Posts

Joined
Tue Oct 12, 2010 2:23 am
Who is online

Users browsing this forum: No registered users and 2 guests