Skip to content

Commit

Permalink
Merge pull request #23 from Xaxxis/2.4.3-Development
Browse files Browse the repository at this point in the history
2.4.3 development
  • Loading branch information
Zaki Ibrahim authored Nov 25, 2020
2 parents 2f0d2e8 + dc52f77 commit 2ea6a61
Show file tree
Hide file tree
Showing 22 changed files with 335 additions and 186 deletions.
36 changes: 36 additions & 0 deletions Block/Adminhtml/Config/NotificationEndpoint.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Midtrans\Snap\Block\Adminhtml\Config;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Midtrans\Snap\Helper\Data;

class NotificationEndpoint extends Field
{
protected $_midtransHelper;

/**
* constructor.
* @param Context $context
* @param array $data
* @param Data $midtransHelper
*/
public function __construct(Data $midtransHelper, Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_midtransHelper = $midtransHelper;
}

/**
* Retrieve the midtrans Notification endpoint
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
return $this->_midtransHelper->getNotificationEndpoint();
}
}
36 changes: 36 additions & 0 deletions Block/Adminhtml/Config/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Midtrans\Snap\Block\Adminhtml\Config;

use Magento\Backend\Block\Template\Context;
use Magento\Config\Block\System\Config\Form\Field;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Midtrans\Snap\Helper\Data;

class Version extends Field
{
protected $_midtransHelper;

/**
* Version constructor.
* @param Context $context
* @param array $data
* @param Data $midtransHelper
*/
public function __construct(Data $midtransHelper, Context $context, array $data = [])
{
parent::__construct($context, $data);
$this->_midtransHelper = $midtransHelper;
}

/**
* Retrieve the setup version of the extension
*
* @param AbstractElement $element
* @return string
*/
protected function _getElementHtml(AbstractElement $element)
{
return $this->_midtransHelper->getModuleVersion();
}
}
6 changes: 2 additions & 4 deletions Controller/Payment/AbstractAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ public function getPayload($config)
if ($config['one_click']) {
$payloads['user_id'] = crypt($order_billing_address->getEmail(), $this->data->getServerKey($paymentCode));
}
if (!empty($customExpiry)) {
$customExpiry = explode(" ", $customExpiry);
if (isset($config['custom_expiry'])) {
$customExpiry = explode(" ", $config['custom_expiry']);
$expiry_unit = $customExpiry[1];
$expiry_duration = (int)$customExpiry[0];

Expand Down Expand Up @@ -1036,10 +1036,8 @@ public function isContainDownloadableProduct()
//look for downloadable products
if ($item->getProductType() === 'downloadable') {
return true;
break;
} else {
return false;
break;
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions Controller/Payment/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function execute()
if ($transaction == 'capture') {
if ($fraud == 'challenge') {
$order_note = $note_prefix . 'Payment status challenged. Please take action on your Merchant Administration Portal - ' . $payment_type;
$this->setOrderStateAndStatus($orderId, Order::STATE_PAYMENT_REVIEW, $order_note, $trxId);
$this->setOrderStateAndStatus($orderId, Order::STATE_PAYMENT_REVIEW, $order_note);
} elseif ($fraud == 'accept') {
$order_note = $note_prefix . 'Payment Completed - ' . $payment_type;
if ($order->canInvoice() && !$order->hasInvoices()) {
Expand All @@ -66,7 +66,7 @@ public function execute()
}
} elseif ($transaction == 'pending') {
$order_note = $note_prefix . 'Awating Payment - ' . $payment_type;
$this->setOrderStateAndStatus($orderId, Order::STATE_PENDING_PAYMENT, $order_note, $trxId);
$this->setOrderStateAndStatus($orderId, Order::STATE_PENDING_PAYMENT, $order_note);
} elseif ($transaction == 'cancel') {
if ($order->canCancel()) {
$order_note = $note_prefix . 'Canceled Payment - ' . $payment_type;
Expand All @@ -81,7 +81,7 @@ public function execute()
$order_note = $note_prefix . 'Payment Deny - ' . $payment_type;
$order->addStatusToHistory(Order::STATE_PAYMENT_REVIEW, $order_note, false);
} elseif ($transaction == 'refund' || $transaction == 'partial_refund') {
$isFullRefund = ($transaction == 'refund') ? true : false;
$isFullRefund = $transaction == 'refund';

/**
* Get last array object from refunds array and get the value from last refund object
Expand Down
28 changes: 17 additions & 11 deletions Controller/Payment/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,32 @@

use Exception;
use Magento\Framework\Controller\ResultFactory;
use Midtrans\Snap\Gateway\SnapApi;
use Midtrans\Snap\Gateway\Config\Config;
use Midtrans\Snap\Gateway\SnapApi;

class Redirect extends AbstractAction
{
public function execute()
{
$paymentCode = $this->getCode();
$requestConfig = $this->getData()->getRequestConfig($paymentCode);
$enableRedirect = $this->getData()->isRedirect();
try {
$paymentCode = $this->getCode();
$requestConfig = $this->getData()->getRequestConfig($paymentCode);
$enableRedirect = $this->getData()->isRedirect();

$payloads = $this->getPayload($requestConfig);
$is3ds = $requestConfig['is3ds'];
$payloads = $this->getPayload($requestConfig);
$is3ds = $requestConfig['is3ds'];
$isProduction = $this->getData()->isProduction();

Config::$is3ds = $is3ds;
Config::$isProduction = $this->getData()->isProduction();
Config::$serverKey = $this->getData()->getServerKey($paymentCode);
Config::$isSanitized = false;
Config::$isProduction = $isProduction;
Config::$serverKey = $this->getData()->getServerKey($paymentCode);
Config::$isSanitized = true;
Config::$is3ds = $is3ds;

/*Override notification, if override notification from admin setting is active (default is active) */
if ($this->getData()->isOverrideNotification() && $this->getData()->getNotificationEndpoint() != null) {
Config::$overrideNotifUrl = $this->getData()->getNotificationEndpoint();
}

try {
$_info = 'Info - Payloads: ' . print_r($payloads, true);
$this->_midtransLogger->midtransRequest($_info);

Expand Down
14 changes: 13 additions & 1 deletion Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ class Config
* @static
*/
public static $is3ds = false;
/**
* Set Append URL notification
*
* @static
*/
public static $appendNotifUrl;
/**
* Set Override URL notification
*
* @static
*/
public static $overrideNotifUrl;
/**
* Enable request params sanitizer (validate and modify charge request params).
* See Midtrans_Sanitizer for more details
Expand All @@ -44,7 +56,7 @@ class Config
*
* @static
*/
public static $curlOptions = array();
public static $curlOptions = [];

const SANDBOX_BASE_URL = 'https://api.sandbox.midtrans.com/v2';
const PRODUCTION_BASE_URL = 'https://api.midtrans.com/v2';
Expand Down
29 changes: 17 additions & 12 deletions Gateway/CoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
namespace Midtrans\Snap\Gateway;

use Midtrans\Snap\Gateway\Config\Config;
use Midtrans\Snap\Gateway\Utility\Sanitizer;
use Midtrans\Snap\Gateway\Http\Client\ApiRequestor;
use Midtrans\Snap\Gateway\Utility\Sanitizer;

/**
* Provide charge and capture functions for Core API
*/
Expand All @@ -19,11 +20,11 @@ class CoreApi
*/
public static function charge($params)
{
$payloads = array(
$payloads = [
'payment_type' => 'credit_card'
);
];

if (array_key_exists('item_details', $params)) {
if (isset($params['item_details'])) {
$gross_amount = 0;
foreach ($params['item_details'] as $item) {
$gross_amount += $item['quantity'] * $item['price'];
Expand All @@ -37,13 +38,19 @@ public static function charge($params)
Sanitizer::jsonRequest($payloads);
}

$result = ApiRequestor::post(
if (Config::$appendNotifUrl) {
Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'X-Append-Notification: ' . Config::$appendNotifUrl;
}

if (Config::$overrideNotifUrl) {
Config::$curlOptions[CURLOPT_HTTPHEADER][] = 'X-Override-Notification: ' . Config::$overrideNotifUrl;
}

return ApiRequestor::post(
Config::getBaseUrl() . '/charge',
Config::$serverKey,
$payloads
);

return $result;
}

/**
Expand All @@ -55,16 +62,14 @@ public static function charge($params)
*/
public static function capture($param)
{
$payloads = array(
$payloads = [
'transaction_id' => $param,
);
];

$result = ApiRequestor::post(
return ApiRequestor::post(
Config::getBaseUrl() . '/capture',
Config::$serverKey,
$payloads
);

return $result;
}
}
24 changes: 12 additions & 12 deletions Gateway/Http/Client/ApiRequestor.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Midtrans\Snap\Gateway\Http\Client;

use Midtrans\Snap\Gateway\Config\Config;

/**
* Send request to Midtrans API
* Better don't use this class directly, use CoreApi, Transaction
Expand Down Expand Up @@ -53,26 +54,26 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)
{
$ch = curl_init();

$curl_options = array(
$curl_options = [
CURLOPT_URL => $url,
CURLOPT_HTTPHEADER => array(
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Accept: application/json',
'user-agent : Magento 2 Module',
'x-plugin-name : midtrans-magento2',
'x-plugin-name : midtrans-magento-2.4.3',
'Authorization: Basic ' . base64_encode($server_key . ':')
),
],
CURLOPT_RETURNTRANSFER => 1
);
];

// merging with Config::$curlOptions
if (count(Config::$curlOptions)) {
// We need to combine headers manually, because it's array and it will no be merged
if (Config::$curlOptions[CURLOPT_HTTPHEADER]) {
$mergedHeders = array_merge($curl_options[CURLOPT_HTTPHEADER], Config::$curlOptions[CURLOPT_HTTPHEADER]);
$headerOptions = array( CURLOPT_HTTPHEADER => $mergedHeders );
$headerOptions = [ CURLOPT_HTTPHEADER => $mergedHeders ];
} else {
$mergedHeders = array();
$mergedHeders = [];
}

$curl_options = array_replace_recursive($curl_options, Config::$curlOptions, $headerOptions);
Expand All @@ -99,16 +100,15 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)
// curl_close($ch);
}


if ($result === false) {
throw new \Exception('CURL Error: ' . curl_error($ch), curl_errno($ch));
} else {
try {
$result_array = json_decode($result);
} catch (\Exception $e) {
throw new \Exception("API Request Error unable to json_decode API response: ".$result . ' | Request url: '.$url);
throw new \Exception("API Request Error unable to json_decode API response: " . $result . ' | Request url: ' . $url);
}
if (!in_array($result_array->status_code, array(200, 201, 202, 407))) {
if (!in_array($result_array->status_code, [200, 201, 202, 407])) {
$message = 'Midtrans Error (' . $result_array->status_code . '): '
. $result_array->status_message;
if (isset($result_array->validation_messages)) {
Expand All @@ -126,13 +126,13 @@ public static function remoteCall($url, $server_key, $data_hash, $post = true)

private static function processStubed($curl, $url, $server_key, $data_hash, $post)
{
VT_Tests::$lastHttpRequest = array(
VT_Tests::$lastHttpRequest = [
"url" => $url,
"server_key" => $server_key,
"data_hash" => $data_hash,
"post" => $post,
"curl" => $curl
);
];

return VT_Tests::$stubHttpResponse;
}
Expand Down
Loading

0 comments on commit 2ea6a61

Please sign in to comment.