Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions Block/Adminhtml/Order/View/BreadApiVersionInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* Admin order management block info
*
* @since 2.3.0
* @author Maritim, Kip
*/

namespace Bread\BreadCheckout\Block\Adminhtml\Order\View;

class BreadApiVersionInfo extends \Magento\Sales\Block\Adminhtml\Order\View {

/**
*
* @param \Magento\Backend\Block\Widget\Context $context
* @param \Magento\Framework\Registry $registry
* @param \Magento\Sales\Model\Config $salesConfig
* @param \Magento\Sales\Helper\Reorder $reorderHelper
* @param array $data
*/
public function __construct(
\Magento\Backend\Block\Widget\Context $context,
\Magento\Framework\Registry $registry,
\Magento\Sales\Model\Config $salesConfig,
\Magento\Sales\Helper\Reorder $reorderHelper,
array $data = []
) {
$this->_reorderHelper = $reorderHelper;
$this->_coreRegistry = $registry;
$this->_salesConfig = $salesConfig;
parent::__construct($context, $registry, $salesConfig, $reorderHelper, $data);
}

}
11 changes: 10 additions & 1 deletion Helper/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,11 @@ public function useDefaultButtonSizeCategory($store = \Magento\Store\Model\Scope
*/
public function getBreadCategories($store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE)
{
return explode(",", $this->scopeConfig->getValue(self::XML_CONFIG_SELECT_CATEGORIES, $store));
$selectedCategories =$this->scopeConfig->getValue(self::XML_CONFIG_SELECT_CATEGORIES, $store);
if(!is_null($selectedCategories)) {
return explode(",", $this->scopeConfig->getValue(self::XML_CONFIG_SELECT_CATEGORIES, $store));
}
return array();
}

/**
Expand Down Expand Up @@ -105,6 +109,11 @@ public function isEnabledForCategory($category)
if (!$this->isActive() || !$this->isEnabledOnCAT() || empty($category)) {
return false;
}
$breadCategories = $this->getBreadCategories();
if(count($breadCategories) < 1) {
return true;
}

return in_array($category->getId(), $this->getBreadCategories());
}
}
67 changes: 52 additions & 15 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper {
const XML_CONFIG_API_SECRET_KEY = 'payment/breadcheckout/api_secret_key';
const XML_CONFIG_API_SANDBOX_PUB_KEY = 'payment/breadcheckout/api_sandbox_public_key';
const XML_CONFIG_API_SANDBOX_SECRET_KEY = 'payment/breadcheckout/api_sandbox_secret_key';
const XML_CONFIG_CLASSIC_API_PUB_KEY = 'payment/breadcheckout/classic_api_public_key';
const XML_CONFIG_CLASSIC_API_SECRET_KEY = 'payment/breadcheckout/classic_api_secret_key';
const XML_CONFIG_CLASSIC_API_SANDBOX_PUB_KEY = 'payment/breadcheckout/classic_api_sandbox_public_key';
const XML_CONFIG_CLASSIC_API_SANDBOX_SECRET_KEY = 'payment/breadcheckout/classic_api_sandbox_secret_key';
const XML_CONFIG_JS_LIB_LOCATION = 'payment/breadcheckout/js_location';
const XML_CONFIG_BUTTON_ON_PRODUCTS = 'payment/breadcheckout/button_on_products';
const XML_CONFIG_BUTTON_DESIGN = 'payment/breadcheckout/button_design';
Expand Down Expand Up @@ -238,12 +242,25 @@ public function getProductTypeMessage($store = \Magento\Store\Model\ScopeInterfa
* @param null $store
* @return mixed
*/
public function getApiPublicKey($storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store)) {
return $this->scopeConfig->getValue(self::XML_CONFIG_API_PUB_KEY, $store, $storeCode);
public function getApiPublicKey($breadApiVersion = null, $storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
$apiVersion = $this->getApiVersion();
if(!is_null($breadApiVersion)) {
$apiVersion = $breadApiVersion;
}
if($apiVersion === 'bread_2') {
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store)) {
return $this->scopeConfig->getValue(self::XML_CONFIG_API_PUB_KEY, $store, $storeCode);
} else {
return $this->scopeConfig->getValue(self::XML_CONFIG_API_SANDBOX_PUB_KEY, $store, $storeCode);
}
} else {
return $this->scopeConfig->getValue(self::XML_CONFIG_API_SANDBOX_PUB_KEY, $store, $storeCode);
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store)) {
return $this->scopeConfig->getValue(self::XML_CONFIG_CLASSIC_API_PUB_KEY, $store, $storeCode);
} else {
return $this->scopeConfig->getValue(self::XML_CONFIG_CLASSIC_API_SANDBOX_PUB_KEY, $store, $storeCode);
}
}

}

/**
Expand All @@ -252,15 +269,31 @@ public function getApiPublicKey($storeCode = null, $store = \Magento\Store\Model
* @param null $store
* @return string
*/
public function getApiSecretKey($storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store)) {
return (string) $this->encryptor->decrypt(
$this->scopeConfig->getValue(self::XML_CONFIG_API_SECRET_KEY, $store, $storeCode)
);
public function getApiSecretKey($breadApiVersion = null, $storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
$apiVersion = $this->getApiVersion();
if(!is_null($breadApiVersion)) {
$apiVersion = $breadApiVersion;
}
if($apiVersion === 'bread_2') {
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store)) {
return (string) $this->encryptor->decrypt(
$this->scopeConfig->getValue(self::XML_CONFIG_API_SECRET_KEY, $store, $storeCode)
);
} else {
return (string) $this->encryptor->decrypt(
$this->scopeConfig->getValue(self::XML_CONFIG_API_SANDBOX_SECRET_KEY, $store, $storeCode)
);
}
} else {
return (string) $this->encryptor->decrypt(
$this->scopeConfig->getValue(self::XML_CONFIG_API_SANDBOX_SECRET_KEY, $store, $storeCode)
);
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store)) {
return (string) $this->encryptor->decrypt(
$this->scopeConfig->getValue(self::XML_CONFIG_CLASSIC_API_SECRET_KEY, $store, $storeCode)
);
} else {
return (string) $this->encryptor->decrypt(
$this->scopeConfig->getValue(self::XML_CONFIG_CLASSIC_API_SANDBOX_SECRET_KEY, $store, $storeCode)
);
}
}
}

Expand Down Expand Up @@ -307,8 +340,11 @@ public function getJsLibLocation($store = \Magento\Store\Model\ScopeInterface::S
* @param null $store
* @return mixed
*/
public function getTransactionApiUrl($storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
public function getTransactionApiUrl($breadApiVersion = null, $storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $apiVersion = null) {
$apiVersion = $this->getApiVersion($storeCode, $store);
if(!is_null($breadApiVersion)) {
$apiVersion = $breadApiVersion;
}
if($apiVersion === 'bread_2') {
$tenant = strtoupper($this->getConfigClient($storeCode, $store));
if ($this->scopeConfig->getValue(self::XML_CONFIG_API_MODE, $store, $storeCode)) {
Expand Down Expand Up @@ -1004,8 +1040,9 @@ public function getPlatformApiUri($tenant, $env) {
* @return string
*/
public function getApiVersion($storeCode = null, $store = \Magento\Store\Model\ScopeInterface::SCOPE_STORE) {
if($this->scopeConfig->getValue(self::XML_CONFIG_API_VERSION, $store, $storeCode)) {
return (string) $this->scopeConfig->getValue(self::XML_CONFIG_API_VERSION, $store, $storeCode);
$apiVersion = $this->scopeConfig->getValue(self::XML_CONFIG_API_VERSION, $store, $storeCode);
if($apiVersion) {
return (string) $apiVersion;
} else {
return 'bread_2';
}
Expand Down
80 changes: 62 additions & 18 deletions Model/Payment/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,23 @@ public function setOrder(\Magento\Sales\Model\Order $order)
public function cancel($breadTransactionId, $amount = 0, $lineItems = [])
{
$this->logger->info('Call API Cancel method. Bread trxId: '. $breadTransactionId);

$apiVersion = $this->helper->getApiVersion();
$payment = $this->order->getPayment();
$paymentApiVersion = $payment->getData('bread_api_version');
$this->logger->info('Payment API version: '. $paymentApiVersion);
if(!is_null($paymentApiVersion) && in_array($paymentApiVersion, ['classic','bread_2'])) {
$apiVersion = strtolower($paymentApiVersion);
}

/* Check if already canceled in bread */
$transaction = $this->getInfo($breadTransactionId);
$transaction = $this->getInfo($breadTransactionId, $apiVersion);
if (strtoupper($transaction['status']) === self::STATUS_CANCELED || strtoupper($transaction['status']) === 'CANCELLED') {
$this->logger->info('Transaction is already canceled in Bread. Bread trxId: '. $breadTransactionId);
return $transaction;
}

$apiVersion = $this->helper->getApiVersion();


if ($apiVersion === 'bread_2') {
$this->logger->info('Bread platform transaction. Bread trxId: '. $breadTransactionId);
$currency = $transaction['totalAmount']['currency'];
Expand Down Expand Up @@ -178,6 +186,12 @@ public function authorize($breadTransactionId, $amount, $merchantOrderId = null)
$validateAmount = $this->getInfo($breadTransactionId);
$this->logger->info('Trx: ' . json_encode($validateAmount));
$apiVersion = $this->helper->getApiVersion();

$payment = $this->order->getPayment();
$paymentApiVersion = $payment->getData('bread_api_version');
if(!is_null($paymentApiVersion) && in_array($paymentApiVersion, ['classic','bread_2'])) {
$apiVersion = strtolower($paymentApiVersion);
}

// set transaction id so it can be fetched for split payment cancel
$this->setBreadTransactionId($breadTransactionId);
Expand Down Expand Up @@ -229,10 +243,11 @@ public function authorize($breadTransactionId, $amount, $merchantOrderId = null)
'orderId' => $merchantOrderId
]);
$data = '{"externalID":"' . $merchantOrderId . '","metadata":{"externalMerchantData":"externalInfo"}}';

$updateMerchantOrderIdResult = $this->call(
$this->getTransactionInfoUrl($breadTransactionId),
$data,
\Zend_Http_Client::PUT,
\Zend_Http_Client::PATCH,
false
);
$this->logger->info('Response: ' . json_encode($updateMerchantOrderIdResult) . 'Bread trxId: '. $breadTransactionId);
Expand Down Expand Up @@ -311,6 +326,13 @@ public function settle($breadTransactionId, $amount = null, $currency = null)
{
$this->logger->info('Call API settle method. Bread trxId: '. $breadTransactionId);
$apiVersion = $this->helper->getApiVersion();

$payment = $this->order->getPayment();
$paymentApiVersion = $payment->getData('bread_api_version');
if(!is_null($paymentApiVersion) && in_array($paymentApiVersion, ['classic','bread_2'])) {
$apiVersion = strtolower($paymentApiVersion);
}

if ($apiVersion === 'bread_2') {
$validateAmount = $this->getInfo($breadTransactionId);
$currency = trim($validateAmount['totalAmount']['currency']);
Expand Down Expand Up @@ -358,7 +380,14 @@ public function settle($breadTransactionId, $amount = null, $currency = null)
public function refund($breadTransactionId, $amount = 0, $lineItems = [], $currency = null)
{
$this->logger->info('Call API refund method. Bread trxId: '. $breadTransactionId);
$apiVersion = $this->helper->getApiVersion();
$apiVersion = $this->helper->getApiVersion();

$payment = $this->order->getPayment();
$paymentApiVersion = $payment->getData('bread_api_version');
if(!is_null($paymentApiVersion) && in_array($paymentApiVersion, ['classic','bread_2'])) {
$apiVersion = strtolower($paymentApiVersion);
}

if ($apiVersion === 'bread_2') {
$validateAmount = $this->getInfo($breadTransactionId);
$currency = trim($validateAmount['totalAmount']['currency']);
Expand Down Expand Up @@ -397,11 +426,11 @@ public function refund($breadTransactionId, $amount = 0, $lineItems = [], $curre
* @return mixed
* @throws \Exception
*/
public function getInfo($breadTransactionId)
public function getInfo($breadTransactionId, $apiVersion = null)
{
$this->logger->info('Call API getInfo method. Bread trxId: '. $breadTransactionId);
return $this->call(
$this->getTransactionInfoUrl($breadTransactionId),
$this->getTransactionInfoUrl($breadTransactionId, $apiVersion),
[],
\Zend_Http_Client::GET
);
Expand Down Expand Up @@ -433,7 +462,7 @@ public function submitCartData($data)
*/
public function getAsLowAs($data)
{
$baseUrl = $this->helper->getTransactionApiUrl($this->getStoreId());
$baseUrl = $this->helper->getTransactionApiUrl('classic', $this->getStoreId());
$asLowAsUrl = join('/', [ trim($baseUrl, '/'), 'aslowas' ]);
return $this->call(
$asLowAsUrl,
Expand All @@ -455,11 +484,21 @@ public function getAsLowAs($data)
*/
protected function call($url, $data, $method = \Zend_Http_Client::POST, $jsonEncode = true)
{
$this->logger->info('We are at call');
$storeId = $this->getStoreId();
$username = $this->helper->getApiPublicKey($storeId);
$password = $this->helper->getApiSecretKey($storeId);
$apiVersion = $this->helper->getApiVersion();

if(!is_null($this->order)) {
$payment = $this->order->getPayment();
$paymentApiVersion = $payment->getData('bread_api_version');
if (!is_null($paymentApiVersion) && in_array($paymentApiVersion, ['classic', 'bread_2'])) {
$apiVersion = strtolower($paymentApiVersion);
}
}
$this->logger->info("API Version :: " . $apiVersion );
$username = $this->helper->getApiPublicKey($apiVersion, $storeId);
$password = $this->helper->getApiSecretKey($apiVersion, $storeId);

if($apiVersion === 'bread_2') {
try {
$authToken = $this->helper->getAuthToken();
Expand Down Expand Up @@ -639,7 +678,7 @@ protected function callBread($url, $authToken, $data, $method = \Zend_Http_Clien
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}

if ($method == \Zend_Http_Client::PUT) {
if ($method == \Zend_Http_Client::PUT || $method == \Zend_Http_Client::PATCH) {
$authorization = "Authorization: Bearer " . $authToken;
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
Expand Down Expand Up @@ -764,7 +803,7 @@ public function sendEmail($cartId, $email, $name)
public function setShippingDetails($transactionId, $trackingNumber, $carrierName)
{
$apiVersion = $this->helper->getApiVersion();
$baseUrl = $this->helper->getTransactionApiUrl($this->getStoreId());
$baseUrl = $this->helper->getTransactionApiUrl(null, $this->getStoreId());

if ($apiVersion === 'bread_2') {

Expand Down Expand Up @@ -797,10 +836,13 @@ public function setShippingDetails($transactionId, $trackingNumber, $carrierName
* @param $transactionId
* @return string
*/
protected function getTransactionInfoUrl($transactionId)
{
protected function getTransactionInfoUrl($transactionId, $breadApiVersion = null)
{
$apiVersion = $this->helper->getApiVersion();
$baseUrl = $this->helper->getTransactionApiUrl($this->getStoreId());
if(!is_null($breadApiVersion)) {
$apiVersion = $breadApiVersion;
}
$baseUrl = $this->helper->getTransactionApiUrl($apiVersion, $this->getStoreId());
if ($apiVersion === 'bread_2') {
return join('/', [trim($baseUrl, '/'), 'transaction', trim($transactionId, '/')]);
} else {
Expand All @@ -816,7 +858,7 @@ protected function getTransactionInfoUrl($transactionId)
*/
protected function getUpdateTransactionUrl($transactionId)
{
$baseUrl = $this->helper->getTransactionApiUrl($this->getStoreId());
$baseUrl = $this->helper->getTransactionApiUrl('classic', $this->getStoreId());
return join(
'/',
[ trim($baseUrl, '/'), 'transactions/actions', trim($transactionId, '/') ]
Expand All @@ -830,7 +872,8 @@ protected function getUpdateTransactionUrl($transactionId)
* @return string
*/
protected function getAuthTokenUrl() {
$baseUrl = $this->helper->getTransactionApiUrl($this->getStoreId());
$baseUrl = $this->helper->getTransactionApiUrl('bread_2', $this->getStoreId());
//return join('/', [trim($baseUrl, '/'), 'auth/service/authorize']);
return join('/', [trim($baseUrl, '/'), 'auth/sa/authenticate']);
}

Expand All @@ -851,6 +894,7 @@ protected function generateAuthToken($url, $apiKey, $apiSecret) {
$curl = curl_init($url);
try {
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_USERPWD, $apiKey. ':' . $apiSecret);
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
Expand Down Expand Up @@ -948,7 +992,7 @@ protected function getStoreId()
* @param type $action
*/
protected function getUpdateTransactionUrlPlatform($transactionId, $action) {
$baseUrl = $this->helper->getTransactionApiUrl($this->getStoreId());
$baseUrl = $this->helper->getTransactionApiUrl('bread_2', $this->getStoreId());
$url = join('/', [trim($baseUrl, '/'), 'transaction', $transactionId, $action]);
return $url;
}
Expand Down
4 changes: 2 additions & 2 deletions Model/System/Config/Source/ApiVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ApiVersion implements \Magento\Framework\Option\ArrayInterface {
public function toOptionArray() {
return [
['value' => 'classic', 'label' => __('Bread Classic')],
['value' => 'bread_2', 'label' => __('Bread 2.0')]
['value' => 'bread_2', 'label' => __('Bread Platform')]
];
}

Expand All @@ -32,7 +32,7 @@ public function toOptionArray() {
public function toArray() {
return [
'classic' => __('Bread Classic'),
'bread_2' => __('Bread 2.0')
'bread_2' => __('Bread Platform')
];
}

Expand Down
Loading