diff --git a/Hyperpay/Extension/Block/Adminhtml/Order/View/Custom.php b/Hyperpay/Extension/Block/Adminhtml/Order/View/Custom.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Block/Display.php b/Hyperpay/Extension/Block/Display.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Block/Status.php b/Hyperpay/Extension/Block/Status.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Controller/Index/Request.php b/Hyperpay/Extension/Controller/Index/Request.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Controller/Index/Sadad.php b/Hyperpay/Extension/Controller/Index/Sadad.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Controller/Index/ServerToServer.php b/Hyperpay/Extension/Controller/Index/ServerToServer.php new file mode 100755 index 0000000..165f91d --- /dev/null +++ b/Hyperpay/Extension/Controller/Index/ServerToServer.php @@ -0,0 +1,279 @@ +_coreRegistry = $coreRegistry; + parent::__construct($context); + $this->_checkoutSession = $checkoutSession; + $this->_helper = $helper; + $this->_pageFactory = $pageFactory; + $this->_adapter = $adapter; + $this->_storeManager = $storeManager; + $this->_resolver = $resolver; + $this->_remote = $remote; + $this->_stockManagement = $stockManagement; + $this->_quoteFactory = $quoteFactory; + + } + + public function execute() + { + try { + if (!($this->_checkoutSession->getLastRealOrderId())) { + $this->_helper->doError(__('Order is not found')); + } + + $order = $this->_checkoutSession->getLastRealOrder(); + } catch (\Exception $e) { + $this->messageManager->addError($e->getMessage()); + return $this->_pageFactory->create(); + } + $quote = $this->_quoteFactory->create()->load($order->getQuoteId()); + $quote->setIsActive(true); + $quote->save(); + $this->_checkoutSession->replaceQuote($quote); + if (($order->getState() !== 'new') && ($order->getState() !== 'pending_payment')) { + $this->messageManager->addError(__("This order has already been processed,Please place a new order")); + $resultRedirect = $this->resultRedirectFactory->create(); + $resultRedirect->setPath('checkout/onepage/failure'); + return $resultRedirect; + } + try { + $base = $this->_storeManager->getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); + $statusUrl = $base . "hyperpay/index/servertoserverstatus/?method=" . $order->getPayment()->getData('method'); + $urlReq = $this->serverToServer($order, $statusUrl); + + } catch (\Exception $e) { + $this->messageManager->addError($e->getMessage()); + $resultRedirect = $this->resultRedirectFactory->create(); + $resultRedirect->setPath('checkout/onepage/failure'); + return $resultRedirect; + } + + $this->_coreRegistry->register('formurl', $urlReq); + $this->_coreRegistry->register('status', $statusUrl); + + return $this->_pageFactory->create(); + } + + /** + * Build data and make a request to hyperpay payment gateway + * and return url of form + * + * @param $order + * @return string + */ + public function serverToServer($order, $status) + { + $payment = $order->getPayment(); + $method = $payment->getData('method'); + + $paymentBrand = $this->getPaymentBrand($method); + + $email = $order->getBillingAddress()->getEmail(); + //order# + $orderId = $order->getIncrementId(); + $amount = $order->getBaseGrandTotal(); + $total = $this->_helper->convertPrice($payment, $amount); + + if ($this->_adapter->getEnv()) { + $grandTotal = (int)$total; + } else { + $grandTotal = number_format($total, 2, '.', ''); + } + + $currency = $this->_adapter->getSupportedCurrencyCode($method); + $paymentType = $this->_adapter->getPaymentType($method); + $this->_adapter->setPaymentTypeAndCurrency($order, $paymentType, $currency); + $entityId = $this->_adapter->getEntity($method); + $baseUrl = $this->_adapter->getServerToServerUrl(); + $data = "entityId=" . $entityId . + "¬ificationUrl=" . $status . + "&shopperResultUrl=" . $status . + "&amount=" . $grandTotal . + "&paymentBrand=$paymentBrand" . + "¤cy=" . $currency . + "&paymentType=" . $paymentType . + "&customer.email=" . $email . + "&merchantTransactionId=" . $orderId; + + if ($method == 'HyperPay_Zoodpay') { + $data .= "&customParameters[service_code]=ZPI"; // fixed + } + $data .= $this->_adapter->getModeHyperpay(); + + + $accesstoken = $this->_adapter->getAccessToken(); + $auth = array('Authorization' => 'Bearer ' . $accesstoken); + $this->_helper->setHeaders($auth); + + $data .= $this->_helper->getBillingAndShippingAddress($order); + $data .= $this->buildCartItems($method); + + $decodedData = $this->_helper->getCurlServerToServer($baseUrl, $data); + + if (!isset($decodedData['id'])) { + $this->_helper->doError(__('Request id is not found')); + return; + } + + if (!isset($decodedData['result']['code']) || $decodedData['result']['code'] != '000.200.000') { + $desc = \Safe\json_decode($decodedData['resultDetails']['ExtendedDescription'], true); + $errors = ''; + if (isset($desc['details'])) { + foreach ($desc['details'] as $detail) { + $errors .= $detail['error'] . ' - '; + } + } + $this->_helper->doError(__($errors)); + return; + } + + $redirectForm = $this->buildRedirectForm($decodedData); + if (!$redirectForm) { + $this->_helper->doError(__($decodedData['result']['description'])); + return; + } + + echo $redirectForm; + } + + private function buildCartItems($method) + { + $objectManager = ObjectManager::getInstance(); + $cart = $objectManager->get('\Magento\Checkout\Model\Cart'); + // retrieve quote items array + $items = $cart->getQuote()->getAllItems(); + $cartData = ''; + $categories = []; + + foreach ($items as $key => $item) { + $cartData .= "&cart.items[" . $key . "].name=" . $item->getName() . + "&cart.items[" . $key . "].price=" . number_format($item->getPrice(), 2, '.', '') . + "&cart.items[" . $key . "].quantity=" . $item->getQty(); + + if ($method == 'HyperPay_Tabby') { + $cartData .= "&cart.items[" . $key . "].sku=" . $item->getSku(); + } + + $categories[] = [["test"]]; + } + + if ($method == 'HyperPay_Zoodpay') { + $cartData .= "&customParameters['categories']=" . (json_encode($categories)); + } + return $cartData; + } + + private function buildRedirectForm($data) + { + if (!isset($data['redirect'])) { + return false; + } + + $form = '
'; + $form .= ''; + return $form; + } + + private function getPaymentBrand($method) + { + switch ($method) { + case 'HyperPay_Zoodpay': + return 'ZOODPAY'; + case 'HyperPay_Tabby': + return 'TABBY'; + } + + return false; + } +} diff --git a/Hyperpay/Extension/Controller/Index/ServerToServerStatus.php b/Hyperpay/Extension/Controller/Index/ServerToServerStatus.php new file mode 100644 index 0000000..3713fa0 --- /dev/null +++ b/Hyperpay/Extension/Controller/Index/ServerToServerStatus.php @@ -0,0 +1,156 @@ +_pageFactory = $pageFactory; + $this->_coreRegistry = $coreRegistry; + $this->_orderFactory = $orderFactory; + $this->_request = $request; + $this->_helper = $helper; + $this->_adapter = $adapter; + $this->_scopeConfig = $scopeConfig; + + } + + public function execute() + { +// die("i am here in ServerToServer.php"); + try { + $data = $this->getStatusRequest(); + + $order = $this->_orderFactory->create()->loadByIncrementId($data['merchantTransactionId']); + if (!$order) { + $this->_helper->doError(__('Order id does not found')); + } + } catch (\Exception $exception) { + $this->messageManager->addError($exception->getMessage()); + $resultRedirect = $this->resultRedirectFactory->create(); + $resultRedirect->setPath('checkout/onepage/failure'); + return $resultRedirect; + } + + try { + if ($order->getState() == 'processing') { + $this->_redirect('checkout/onepage/success'); + } + $this->_adapter->setInfo($order, $data['id']); + $status = $this->_adapter->orderStatus($data, $order); + $this->_coreRegistry->register('status', $status); + if ($status !== 'success') { + $this->messageManager->addError($status); + $this->_redirect('checkout/onepage/failure'); + } else { + $this->_redirect('checkout/onepage/success'); + + } + } catch (\Exception $e) { + $order->addStatusHistoryComment('Exception message: ' . $e->getMessage(), false); + $this->messageManager->addError($e->getMessage()); + $resultRedirect = $this->resultRedirectFactory->create(); + $resultRedirect->setPath('checkout/onepage/failure'); + return $resultRedirect; + } + } + + /** + * Retrieve payment gateway response and set id to payment table + * @return array + */ + + private function getStatusRequest() + { + if (empty($this->_request->getParam('id'))) { + $this->_helper->doError(__('Checkout id does not found')); + } + $id = $this->_request->getParam('id'); + + $method = $this->_request->getParam('method'); + $entityId = $this->_adapter->getEntity($method); + + $baseUrl = $this->_adapter->getUrl(); + $url = $baseUrl . 'payments/' . $id; + $url .= "?entityId=$entityId"; + $accessToken = $this->_adapter->getAccessToken(); + + $auth = array('Authorization' => 'Bearer ' . $accessToken); + $this->_helper->setHeaders($auth); + $decodedData = $this->_helper->getCurlRespData($url); + + if (!isset($decodedData)) { + $this->_helper->doError(__('No response data found')); + } + if (!isset($decodedData['id'])) { + $this->_helper->doError(__('Failed to get response from the payment gateway')); + } + return $decodedData; + } + +} diff --git a/Hyperpay/Extension/Controller/Index/Sstatus.php b/Hyperpay/Extension/Controller/Index/Sstatus.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Controller/Index/Status.php b/Hyperpay/Extension/Controller/Index/Status.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Cron/CancelOrderPending.php b/Hyperpay/Extension/Cron/CancelOrderPending.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Helper/Data.php b/Hyperpay/Extension/Helper/Data.php old mode 100644 new mode 100755 index b6e041e..92671c0 --- a/Hyperpay/Extension/Helper/Data.php +++ b/Hyperpay/Extension/Helper/Data.php @@ -1,4 +1,5 @@ _checkoutSession = $checkoutSession; $this->_responseFactory = $responseFactory; $this->_jsonHelper = $jsonHelper; $this->_curlClient = $curl; - $this->_storeManager=$storeManager; + $this->_storeManager = $storeManager; $this->_messageManager = $messageManager; - $this->_adapter =$adapter; + $this->_adapter = $adapter; $this->_assetRepo = $assetRepo; parent::__construct($context); } + /** * Set Headers for curl request * @@ -90,140 +93,144 @@ public function setHeaders($headers) $this->_curlClient->setHeaders($headers); } + /** - * Set Headers for curl request + * Set Headers for curl request * * @param $data - */ + */ public function setSadadHeaders($data) { $headers = array( - 'Content-Type'=>'application/json', - 'Content-Length'=> strlen($data)); + 'Content-Type' => 'application/json', + 'Content-Length' => strlen($data)); $this->_curlClient->setHeaders($headers); } + /** * throw a new error exception * * @param $payment - */ + */ public function doError($string) { throw new \Exception($string); } + /** - * Retrieve payment brand depending on payment method + * Retrieve payment brand depending on payment method * * @return string - */ + */ public function getBrand() - { - try{ - if(!($this->_checkoutSession->getLastRealOrderId())) { + { + try { + if (!($this->_checkoutSession->getLastRealOrderId())) { $this->doError('Order id does not found'); - } + } $order = $this->_checkoutSession->getLastRealOrder(); - $payment= $order->getPayment(); + $payment = $order->getPayment(); $code = $payment->getData('method'); - $paymentMethod=''; + $paymentMethod = ''; switch ($code) { case 'HyperPay_Visa': - $paymentMethod ='VISA'; - break; + $paymentMethod = 'VISA'; + break; case 'HyperPay_Mada': - $paymentMethod ='MADA'; + $paymentMethod = 'MADA'; break; case 'HyperPay_SadadNcb': - $paymentMethod= 'SADAD'; - break; + $paymentMethod = 'SADAD'; + break; case 'HyperPay_PayPal': - $paymentMethod= 'PAYPAL'; - break; + $paymentMethod = 'PAYPAL'; + break; case 'HyperPay_Master': - $paymentMethod= 'MASTER'; - break; + $paymentMethod = 'MASTER'; + break; case 'HyperPay_Amex': - $paymentMethod= 'AMEX'; - break; + $paymentMethod = 'AMEX'; + break; case 'HyperPay_ApplePay': $paymentMethod = 'APPLEPAY'; - break; + break; + case 'HyperPay_Zoodpay': + $paymentMethod = 'ZOODPAY'; + break; case 'HyperPay_stc': - $paymentMethod= 'STC_PAY'; - - break; + $paymentMethod = 'STC_PAY'; + + break; } return $paymentMethod; - } catch(\Exception $e) - { + } catch (\Exception $e) { $this->_messageManager->addError($e->getMessage()); $this->_logger->critical($e->getMessage()); $this->_responseFactory->create()->setRedirect($this->_storeManager-> - getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB))->sendResponse(); - } - + getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB))->sendResponse(); + } + } + /** - * Retrieve payment brand depending on payment method + * Retrieve payment brand depending on payment method * * @param $street * @param $type * @return string */ - public function getStreetAddresses($street,$type) + public function getStreetAddresses($street, $type) { - $streetAdd=""; + $streetAdd = ""; foreach ($street as $key => $value) { - if($key == '2') + if ($key == '2') break; - $end = $key+1; - $streetAdd.="&".$type."."."street".$end."=".$street[$key]; + $end = $key + 1; + $streetAdd .= "&" . $type . "." . "street" . $end . "=" . $street[$key]; } return $streetAdd; } + /** * Retrieve Increment order id to status view * * @return string - */ + */ public function getOrderId() { - try{ - if(!($this->_checkoutSession->getLastRealOrderId())) { + try { + if (!($this->_checkoutSession->getLastRealOrderId())) { $this->doError('Order id does not found'); - } + } - return $this->_checkoutSession->getLastRealOrderId(); - } catch(\Exception $e) - { + return $this->_checkoutSession->getLastRealOrderId(); + } catch (\Exception $e) { $this->_messageManager->addError($e->getMessage()); $this->_logger->critical($e->getMessage()); $this->_responseFactory->create()->setRedirect($this->_storeManager-> getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB))->sendResponse(); } } + /** - * Convert amount of payment from base currency to selected currency + * Convert amount of payment from base currency to selected currency * * @param $payment * @param $amountValue * @return double - */ - public function convertPrice($payment,$amountValue) - { + */ + public function convertPrice($payment, $amountValue) + { $currentCurrency = $this->_adapter->getSupportedCurrencyCode($payment->getData('method')); $baseCurrency = $this->_storeManager->getStore()->getBaseCurrency()->getCode(); if ($currentCurrency != $baseCurrency) { - try - { + try { $amountValue = $this->_storeManager->getStore()->getBaseCurrency()-> convert($amountValue, $currentCurrency); - } - catch (\Exception $e) - { + } catch (\Exception $e) { $this->catchExceptionRedirectAndCancelOrder($payment->getOrder(), $e); return; } @@ -231,12 +238,13 @@ public function convertPrice($payment,$amountValue) return $amountValue; } + /** - * Set curl options depending on server mode - */ + * Set curl options depending on server mode + */ public function setCurlOptions() { - if($this->_adapter->getEnv()) { + if ($this->_adapter->getEnv()) { $this->_curlClient->setOption(CURLOPT_SSL_VERIFYPEER, false); $this->_curlClient->setOption(CURLOPT_SSL_VERIFYHOST, false); } else { @@ -244,155 +252,175 @@ public function setCurlOptions() } } + /** - * method to check if test passed is English + * method to check if test passed is English * - * @param (string) $text to be checked. - * @return (bool) true|false. - */ + * @param (string) $text to be checked. + * @return (bool) true|false. + */ public function isThisEnglishText($text) { - return preg_match("/^[\w\s\.\-\,]*$/", $text); + return preg_match("/^[\w\s\.\-\,]*$/", $text); } + /** * Retrieve billing And shipping Address * * @param $order * @return string - */ + */ public function getBillingAndShippingAddress($order) { - $data=""; + $data = ""; $payment = $order->getPayment(); $method = $payment->getData('method'); - $shippingAddress = $order->getShippingAddress(); - if(isset($shippingAddress) && !empty($shippingAddress)){ - $firstNameShipping = $order->getShippingAddress()->getFirstname(); - $surNameShipping = $order->getShippingAddress()->getLastname(); - $countryShipping = $order->getShippingAddress()->getCountryId(); - $telShipping= $order->getShippingAddress()->getTelephone(); - $postCodeShipping = $order->getShippingAddress()->getPostcode(); - $streetShipping = $order->getShippingAddress()->getStreet(); - $cityShipping = $order->getShippingAddress()->getCity(); - $streetShippingCompare = implode(',', $streetShipping); - - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($cityShipping)==false)) { - $data.="&shipping.city=".$cityShipping; - } + $shippingAddress = $order->getShippingAddress(); + if (isset($shippingAddress) && !empty($shippingAddress)) { + $firstNameShipping = $order->getShippingAddress()->getFirstname(); + $surNameShipping = $order->getShippingAddress()->getLastname(); + $countryShipping = $order->getShippingAddress()->getCountryId(); + $telShipping = $order->getShippingAddress()->getTelephone(); + $postCodeShipping = $order->getShippingAddress()->getPostcode(); + $streetShipping = $order->getShippingAddress()->getStreet(); + $cityShipping = $order->getShippingAddress()->getCity(); + $streetShippingCompare = implode(',', $streetShipping); + + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($cityShipping) == false)) { + $data .= "&shipping.city=" . $cityShipping; + } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($countryShipping)==false)) { - $data.="&shipping.country=".$countryShipping; - } + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($countryShipping) == false)) { + $data .= "&shipping.country=" . $countryShipping; + } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($postCodeShipping)==false)) { - $data.="&shipping.postcode=".$postCodeShipping; - } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($firstNameShipping)==false)) { - $data.="&shipping.customer.givenName=".$firstNameShipping; - } + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($postCodeShipping) == false)) { + $data .= "&shipping.postcode=" . $postCodeShipping; + } + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($firstNameShipping) == false)) { + $data .= "&shipping.customer.givenName=" . $firstNameShipping; + } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($surNameShipping)==false)) { - $data.="&shipping.customer.surname=".$surNameShipping; - } + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($surNameShipping) == false)) { + $data .= "&shipping.customer.surname=" . $surNameShipping; + } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($telShipping)==false)) { - $data.="&shipping.customer.phone=".$telShipping; - } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($streetShippingCompare)==false)) { - $data.=$this->getStreetAddresses($streetShipping, "shipping"); + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($telShipping) == false)) { + $data .= "&shipping.customer.phone=" . $telShipping; + } + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($streetShippingCompare) == false)) { + $data .= $this->getStreetAddresses($streetShipping, "shipping"); + } } - } $firsName = $order->getBillingAddress()->getFirstname(); $surName = $order->getBillingAddress()->getLastname(); $country = $order->getBillingAddress()->getCountryId(); - $tel= $order->getBillingAddress()->getTelephone(); + $tel = $order->getBillingAddress()->getTelephone(); $postCode = $order->getBillingAddress()->getPostcode(); $street = $order->getBillingAddress()->getStreet(); $city = $order->getBillingAddress()->getCity(); $streetCompare = implode(',', $street); - - - + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($city) == false)) { + $data .= "&billing.city=" . $city; + } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($city)==false)) { - $data.="&billing.city=".$city; + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($country) == false)) { + $data .= "&billing.country=" . $country; } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($country)==false)) { - $data.="&billing.country=".$country; + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($firsName) == false)) { + $data .= "&customer.givenName=" . $firsName; } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($firsName)==false)) { - $data.="&customer.givenName=".$firsName; + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($tel) == false)) { + $data .= "&customer.phone=" . $tel; } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($tel)==false)) { - $data.="&customer.phone=".$tel; + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($tel) == false)) { + $data .= "&customer.mobile=" . $tel; } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($postCode)==false)) { - $data.="&billing.postcode=".$postCode; + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($postCode) == false)) { + $data .= "&billing.postcode=" . $postCode; } - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($surName)==false)) { - $data.="&customer.surname=".$surName; + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($surName) == false)) { + $data .= "&customer.surname=" . $surName; } - - if(!($this->_adapter->getConnector($method)=='migs' && $this->isThisEnglishText($streetCompare)==false)) { - $data.=$this->getStreetAddresses($street, "billing"); + if (!($this->_adapter->getConnector($method) == 'migs' && $this->isThisEnglishText($streetCompare) == false)) { + $data .= $this->getStreetAddresses($street, "billing"); } - - return $data; } + /** - * Set order status to cancel, add message, and redirect to home page + * Set order status to cancel, add message, and redirect to home page * * @param $order * @param $e - */ - public function catchExceptionRedirectAndCancelOrder($order,$e) + */ + public function catchExceptionRedirectAndCancelOrder($order, $e) { $order->setState(OrderStatus::STATE_CANCELED); - $order->addStatusHistoryComment('Exception message: '.$e->getMessage(), OrderStatus::STATE_CANCELED); + $order->addStatusHistoryComment('Exception message: ' . $e->getMessage(), OrderStatus::STATE_CANCELED); $order->save(); $this->_messageManager->addError($e->getMessage()); $this->_logger->critical($e->getMessage()); $this->_responseFactory->create()->setRedirect($this->_storeManager-> - getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB))->sendResponse(); + getStore()->getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB))->sendResponse(); } + /** - * Post a request and retrieve decoded data + * Post a request and retrieve decoded data * * @param $url * @param $data * @return string - */ - public function getCurlReqData($url,$data) + */ + public function getCurlReqData($url, $data) { $this->setCurlOptions(); $this->_curlClient->setOption(CURLOPT_RETURNTRANSFER, true); parse_str($data, $params); - $params = $this->replaceArrayKeys($params); - $this->_curlClient->post($url, $params); + $params = $this->replaceArrayKeys($data); + $this->_curlClient->post($url, $data); + $response = $this->_curlClient->getBody(); + $decodedData = $this->_jsonHelper->jsonDecode($response); + + return $decodedData; + } + + /** + * Post a request and retrieve decoded data + * + * @param $url + * @param $data + * @return string + */ + public function getCurlServerToServer($url, $data) + { + $this->setCurlOptions(); + $this->_curlClient->setOption(CURLOPT_RETURNTRANSFER, true); + $this->_curlClient->post($url, $data); $response = $this->_curlClient->getBody(); $decodedData = $this->_jsonHelper->jsonDecode($response); return $decodedData; } + /** - * Post a request and retrieve decoded data + * Post a request and retrieve decoded data * * @param $url * @return string - */ + */ public function getCurlRespData($url) { $this->setCurlOptions(); @@ -402,49 +430,55 @@ public function getCurlRespData($url) $decodedData = $this->_jsonHelper->jsonDecode($response); return $decodedData; } + /** * Replace _ char with . char * * @param $array * @return array */ - private function replaceArrayKeys( $array ) { + private function replaceArrayKeys($array) + { $replacedKeys = str_replace('_', '.', array_keys($array)); return array_combine($replacedKeys, $array); } + public function getPaymentMarkImageUrl($code) { $paymentImage = ''; switch ($code) { case 'HyperPay_Visa': - $paymentImage =$this->_assetRepo->getUrl("Hyperpay_Extension::images/visa.svg"); + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/visa.svg"); break; case 'HyperPay_Mada': - $paymentImage =$this->_assetRepo->getUrl("Hyperpay_Extension::images/mada.svg");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/mada.svg"); break; + case 'HyperPay_SadadPayware': case 'HyperPay_SadadNcb': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/sadad.png");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/sadad.png"); break; case 'HyperPay_PayPal': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/paypal.svg");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/paypal.svg"); break; case 'HyperPay_Master': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/master.svg");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/master.svg"); break; case 'HyperPay_Amex': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/amex.svg");; - break; - case 'HyperPay_SadadPayware': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/sadad.png");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/amex.svg"); break; case 'HyperPay_ApplePay': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/apple.svg");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/apple.svg"); break; case 'HyperPay_stc': - $paymentImage= $this->_assetRepo->getUrl("Hyperpay_Extension::images/stc.png");; + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/stc.png"); + break; + case 'HyperPay_Zoodpay': + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/zoodpay.png"); + break; + case 'HyperPay_Tabby': + $paymentImage = $this->_assetRepo->getUrl("Hyperpay_Extension::images/tabby.png"); break; - } return $paymentImage; } diff --git a/Hyperpay/Extension/Model/Adapter.php b/Hyperpay/Extension/Model/Adapter.php old mode 100644 new mode 100755 index 70e7640..fe3a052 --- a/Hyperpay/Extension/Model/Adapter.php +++ b/Hyperpay/Extension/Model/Adapter.php @@ -1,4 +1,5 @@ _scopeConfig = $scopeConfig; - $this->_storeManager=$storeManager; - $this->blackBins=$blackBins; + $this->_storeManager = $storeManager; + $this->blackBins = $blackBins; $this->_request = $request; $this->_objectManager = $objectManager; $this->_orderManagement = $orderManagement; $this->_invoiceCollectionFactory = $invoiceCollectionFactory; $this->_invoiceService = $invoiceService; $this->_transactionFactory = $transactionFactory; - $this->_stockRegistry = $stockRegistry; + $this->_stockRegistry = $stockRegistry; $this->_productRepository = $productRepository; parent::__construct($context, $registry, $resource, $resourceCollection, $data); } @@ -182,6 +187,7 @@ public function getStockOption() { return $this->_scopeConfig->getValue('cataloginventory/options/can_subtract', $this->_storeScope); } + /** * Retrieve the server mode from configuration * @@ -191,6 +197,7 @@ public function getMode() { return $this->getConfigData(self::MODE); } + /** * Retrieve Webhook key from configuration * @@ -200,6 +207,7 @@ public function getWebhookKey() { return $this->getConfigData(self::WEBHOOK_KEY); } + /** * Retrieve Access token from configuration * @@ -209,6 +217,7 @@ public function getAccessToken() { return $this->getConfigData(self::ACCESS_TOKEN); } + /** * Retrieve risk channel id from configuration * @@ -218,6 +227,7 @@ public function getRiskChannelId() { return $this->getConfigData(self::RISK_CHANNEL_ID); } + /** * Retrieve the style of payment form from configuration * Options : @@ -229,6 +239,7 @@ public function getStyle() { return $this->getConfigData(self::STYLE); } + /** * Retrieve the CSS tags and attributes of payment form from configuration * @@ -239,6 +250,7 @@ public function getCss() return $this->getConfigData(self::CSS); } + /** * Retrieve the Url depending on environment 'server mode' from configuration * @@ -252,12 +264,25 @@ public function getUrl() if ($this->getMode() == "live") { return $this->getConfigData(self::LIVE_URL); - } - else - { + } else { return $this->getConfigData(self::TEST_URL); } } + + + /** + * Retrieve the server to server Url depending on environment 'server mode' from configuration + * + * Options : + * Integrator Test , Connector Test, Live + * + * @return string + */ + public function getServerToServerUrl() + { + return $this->getMode() == 'live' ? $this->liveServerToServerUrl : $this->testServerToServerUrl; + } + /** * Retrieve the Connector from configuration * @@ -270,6 +295,7 @@ public function getConnector($method) { return $this->getConfigDataForSpecificMethod($method, self::CONNECTOR); } + /** * Retrieve the entity id from configuration * @@ -281,6 +307,7 @@ public function getEntity($method) return $this->getConfigDataForSpecificMethod($method, self::ENTITY_ID); } + /** * Retrieve the payment type depending on method code from configuration * @@ -291,6 +318,7 @@ public function getPaymentType($method) { return $this->getConfigDataForSpecificMethod($method, self::PAYMENT_ACTION); } + /** * Retrieve the currency code depending on method code from configuration * @@ -301,6 +329,7 @@ public function getSupportedCurrencyCode($method) { return $this->getConfigDataForSpecificMethod($method, self::CURRENCY_CODE); } + /** * Retrieve the status from configuration * @@ -311,6 +340,7 @@ public function getStatus() return $this->getConfigData(self::ORDER_STATUS); } + /** * Retrieve the Api User Name for sadad depending on method code from configuration * @@ -321,6 +351,7 @@ public function getApiUserName($method) { return $this->getConfigDataForSpecificMethod($method, self::API_USER_NAME); } + /** * Retrieve the api secret for sadad depending on method code from configuration * @@ -331,6 +362,7 @@ public function getApiSecret($method) { return $this->getConfigDataForSpecificMethod($method, self::API_SECRET); } + /** * Retrieve the merchant id for sadad depending on method code from configuration * @@ -341,6 +373,7 @@ public function getMerchantId($method) { return $this->getConfigDataForSpecificMethod($method, self::MERCHANT_ID); } + /** * Add mode to data of curl request depending on server mode * @@ -352,6 +385,7 @@ public function getModeHyperpay() return "&testMode=EXTERNAL"; } } + /** * Retrieve false on live mode and false otherwise * @@ -359,12 +393,13 @@ public function getModeHyperpay() */ public function getEnv() { - if($this->getMode()=="live") { + if ($this->getMode() == "live") { return false; } return true; } + /** * Retrieve sadad request url depending on server mode * @@ -372,13 +407,14 @@ public function getEnv() */ public function getSadadReqUrl() { - if($this->getEnv()) { + if ($this->getEnv()) { return $this->_sadadRequestTestUrl; } - return $this->_sadadRequestLivetUrl; + return $this->_sadadRequestLivetUrl; } + /** * Retrieve sadad redirect url depending on server mode * @@ -386,12 +422,13 @@ public function getSadadReqUrl() */ public function getSadadRedirectUrl() { - if($this->getEnv()) { + if ($this->getEnv()) { return $this->_sadadTestRedirectUrl; } - return $this->_sadadLiveRedirectUrl; + return $this->_sadadLiveRedirectUrl; } + /** * Retrieve sadad Status url depending on server mode * @@ -401,14 +438,15 @@ public function getSadadRedirectUrl() */ public function getSadadStatusUrl() { - if($this->getEnv()) { + if ($this->getEnv()) { return $this->_sadadStatusTestUrl; } - return $this->_sadadStatusLiveUrl; + return $this->_sadadStatusLiveUrl; } + /** * Retrieve url that redirect from checkout page * @@ -418,8 +456,9 @@ public function getSadadUrl() { $base = $this->_storeManager->getStore()-> getBaseUrl(\Magento\Framework\UrlInterface::URL_TYPE_WEB); - return $base."hyperpay/index/sstatus"; + return $base . "hyperpay/index/sstatus"; } + /** * Set status and state to database after transaction complete * and return sucess or fail to view @@ -428,9 +467,8 @@ public function getSadadUrl() * @param $order * @return string */ - public function orderStatus($decodedData,$order) + public function orderStatus($decodedData, $order) { - if (preg_match('/^(000\.400\.0|000\.400\.100)/', $decodedData['result']['code']) || preg_match('/^(000\.000\.|000\.100\.1|000\.[36])/', $decodedData['result']['code'])) { $order->addStatusHistoryComment($decodedData['result']['description'], false); @@ -439,22 +477,23 @@ public function orderStatus($decodedData,$order) } else { $order->addStatusHistoryComment($decodedData['result']['description'], OrderStatus::STATE_CANCELED); $order->setState(OrderStatus::STATE_CANCELED); - $orderCommentSender = $this->_objectManager - ->create('Magento\Sales\Model\Order\Email\Sender\OrderCommentSender'); - $orderCommentSender->send($order, true, ''); +// $orderCommentSender = $this->_objectManager +// ->create('Magento\Sales\Model\Order\Email\Sender\OrderCommentSender'); +// $orderCommentSender->send($order, true, ''); $this->_orderManagement->cancel($order->getEntityId()); $order->save(); $method = $order->getPayment()->getData('method'); - if($method=='SadadNcb') { + + if ($method == 'SadadNcb') { $this->_status = $decodedData['resultDetails']['ErrorMessage']; } else { $this->_status = $decodedData['result']['description']; } - if ((isset($decodedData['card']['bin'])) && ($method != 'HyperPay_Mada') ) { - $blackBins =$this->blackBins->bins(); + if ((isset($decodedData['card']['bin'])) && ($method != 'HyperPay_Mada')) { + $blackBins = $this->blackBins->bins(); $searchBin = $decodedData['card']['bin']; - if (in_array($searchBin,$blackBins)) { - $this->_status =__('Sorry! Please select mada payment option in order to be able to complete your purchase successfully.'); + if (in_array($searchBin, $blackBins)) { + $this->_status = __('Sorry! Please select mada payment option in order to be able to complete your purchase successfully.'); } } @@ -462,26 +501,25 @@ public function orderStatus($decodedData,$order) return $this->_status; } + /** * Set status and state to database after transaction complete - * and return sucess or fail to view (Sadad payment method) + * and return success or fail to view (Sadad payment method) * - * @param $$decodedData + * @param $decodedData * @param $order * @return string */ - public function orderStatusSadad($decodedData,$order) + public function orderStatusSadad($decodedData, $order) { - if ($decodedData=="0") { + if ($decodedData == "0") { $order->addStatusHistoryComment('Request successfully processed', $this->getStatus()); $order->setState($this->getStatus()); $this->_orderManagement->notify($order->getEntityId()); $order->save(); $this->createInvoice($order); $this->_status = 'success'; - } - else - { + } else { $errorMessage = $this->_request->getParam('ErrorDescription'); $order->addStatusHistoryComment($errorMessage, OrderStatus::STATE_CANCELED); @@ -495,6 +533,7 @@ public function orderStatusSadad($decodedData,$order) return $this->_status; } + /** * Set checkoutId to additionalInformation column in sales_order_payment table * @@ -508,6 +547,7 @@ public function setInfo($order, $checkOutId) $order->save(); } + /** * Get checkoutId from additionalInformation column in sales_order_payment table * @@ -518,6 +558,7 @@ public function getCheckoutId($payment) { return $payment->getAdditionalInformation('checkoutId'); } + /** * Set payment type and currency to additionalInformation column in sales_order_payment table * @@ -525,7 +566,7 @@ public function getCheckoutId($payment) * @param $paymentType * @param $currency */ - public function setPaymentTypeAndCurrency($order, $paymentType,$currency) + public function setPaymentTypeAndCurrency($order, $paymentType, $currency) { $payment = $order->getPayment(); $payment->setAdditionalInformation('payment_type', $paymentType); @@ -533,6 +574,7 @@ public function setPaymentTypeAndCurrency($order, $paymentType,$currency) $order->save(); } + /** * Retrieve configuration from admin panel for hyperpay group * @@ -541,9 +583,10 @@ public function setPaymentTypeAndCurrency($order, $paymentType,$currency) */ public function getConfigData($field) { - return $this->_scopeConfig->getValue('payment/hyperpay/'.$field, $this->_storeScope); + return $this->_scopeConfig->getValue('payment/hyperpay/' . $field, $this->_storeScope); } + /** * Retrieve configuration from admin panel for specific payment method group * @@ -551,11 +594,12 @@ public function getConfigData($field) * @param $field * @return string */ - public function getConfigDataForSpecificMethod($method,$field) + public function getConfigDataForSpecificMethod($method, $field) { - return $this->_scopeConfig->getValue('payment/'.$method.'/'.$field, $this->_storeScope); + return $this->_scopeConfig->getValue('payment/' . $method . '/' . $field, $this->_storeScope); } + /** * Bulid data for capture curl request * @@ -564,15 +608,16 @@ public function getConfigDataForSpecificMethod($method,$field) * @param $grandTotal * @return string */ - public function buildCaptureOrRefundRequest($payment,$currency,$grandTotal,$op) + public function buildCaptureOrRefundRequest($payment, $currency, $grandTotal, $op) { - $data = "entityId=".$this->getEntity($payment->getData('method')). - "¤cy=".$currency. - "&amount=".$grandTotal. - "&paymentType=".$op; + $data = "entityId=" . $this->getEntity($payment->getData('method')) . + "¤cy=" . $currency . + "&amount=" . $grandTotal . + "&paymentType=" . $op; $data .= $this->getModeHyperpay(); return $data; } + /** * Create invoice automatically * **status will be set to processing @@ -582,8 +627,8 @@ public function buildCaptureOrRefundRequest($payment,$currency,$grandTotal,$op) public function createInvoice($order) { - if(!$order->getId()) { - $order->addStatusHistoryComment('The order id is not found',$this->getStatus()); + if (!$order->getId()) { + $order->addStatusHistoryComment('The order id is not found', $this->getStatus()); $order->setState(OrderStatus::STATE_PROCESSING)->setStatus($this->getStatus()); $order->save(); return $this; @@ -596,7 +641,7 @@ public function createInvoice($order) $invoices->getSelect()->limit(1); if ((int)$invoices->count() !== 0) { - $order->addStatusHistoryComment('The order has been invoiced already ',$this->getStatus()); + $order->addStatusHistoryComment('The order has been invoiced already ', $this->getStatus()); $order->setState(OrderStatus::STATE_PROCESSING)->setStatus($this->getStatus()); $this->_orderManagement->notify($order->getEntityId()); $order->setEmailSent(true); @@ -604,8 +649,8 @@ public function createInvoice($order) return null; } - if(!$order->canInvoice()) { - $order->addStatusHistoryComment('Could not create an invoice,Creating invoices is inactive',$this->getStatus()); + if (!$order->canInvoice()) { + $order->addStatusHistoryComment('Could not create an invoice,Creating invoices is inactive', $this->getStatus()); $order->setState(OrderStatus::STATE_PROCESSING)->setStatus($this->getStatus()); $this->_orderManagement->notify($order->getEntityId()); $order->setEmailSent(true); @@ -613,9 +658,8 @@ public function createInvoice($order) return null; } foreach ($order->getAllItems() as $item) { - if($item->getProduct()->getIsVirtual()) - { - $order->addStatusHistoryComment('Could not create an invoice,The items has virtual product',$this->getStatus()); + if ($item->getProduct()->getIsVirtual()) { + $order->addStatusHistoryComment('Could not create an invoice,The items has virtual product', $this->getStatus()); $order->setState(OrderStatus::STATE_PROCESSING)->setStatus($this->getStatus()); $this->_orderManagement->notify($order->getEntityId()); $order->setEmailSent(true); @@ -627,8 +671,7 @@ public function createInvoice($order) $code = $order->getPayment()->getData('method'); if ($this->getPaymentType($code) == "DB") { $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_ONLINE); - } - else{ + } else { $invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::NOT_CAPTURE); } $invoice->register(); @@ -641,7 +684,7 @@ public function createInvoice($order) $order->setState(OrderStatus::STATE_PROCESSING)->setStatus($this->getStatus()); $order->save(); } catch (\Exception $e) { - $order->addStatusHistoryComment('Exception message: '.$e->getMessage(), + $order->addStatusHistoryComment('Exception message: ' . $e->getMessage(), false); $this->_orderManagement->notify($order->getEntityId()); $order->setState(OrderStatus::STATE_PROCESSING)->setStatus($this->getStatus()); @@ -655,7 +698,7 @@ public function createInvoice($order) $order->setEmailSent(true); $order->save(); } catch (\Exception $e) { - $order->addStatusHistoryComment('Exception message: '.$e->getMessage(),false); + $order->addStatusHistoryComment('Exception message: ' . $e->getMessage(), false); $order->save(); return null; } diff --git a/Hyperpay/Extension/Model/MainConfigProvider.php b/Hyperpay/Extension/Model/MainConfigProvider.php old mode 100644 new mode 100755 index 83263c3..189edfd --- a/Hyperpay/Extension/Model/MainConfigProvider.php +++ b/Hyperpay/Extension/Model/MainConfigProvider.php @@ -24,7 +24,9 @@ class MainConfigProvider implements ConfigProviderInterface 'HyperPay_SadadPayware', 'HyperPay_Visa', 'HyperPay_ApplePay', - 'HyperPay_stc' + 'HyperPay_stc', + 'HyperPay_Zoodpay', + 'HyperPay_Tabby', ]; /** * @var \Magento\Payment\Model\Method\AbstractMethod[] diff --git a/Hyperpay/Extension/Model/Method/Amex.php b/Hyperpay/Extension/Model/Method/Amex.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/Applepay.php b/Hyperpay/Extension/Model/Method/Applepay.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/Mada.php b/Hyperpay/Extension/Model/Method/Mada.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/Master.php b/Hyperpay/Extension/Model/Method/Master.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/MethodAbstract.php b/Hyperpay/Extension/Model/Method/MethodAbstract.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/PayPal.php b/Hyperpay/Extension/Model/Method/PayPal.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/SadadNcb.php b/Hyperpay/Extension/Model/Method/SadadNcb.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/SadadPayware.php b/Hyperpay/Extension/Model/Method/SadadPayware.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/Stc.php b/Hyperpay/Extension/Model/Method/Stc.php old mode 100644 new mode 100755 diff --git a/Hyperpay/Extension/Model/Method/Tabby.php b/Hyperpay/Extension/Model/Method/Tabby.php new file mode 100644 index 0000000..6e31403 --- /dev/null +++ b/Hyperpay/Extension/Model/Method/Tabby.php @@ -0,0 +1,19 @@ + -