Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a bunch of logging to get more info around pending orders issue #107

Draft
wants to merge 9 commits into
base: develop
Choose a base branch
from
10 changes: 10 additions & 0 deletions Controller/Checkout/EstimateShipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,19 @@ public function execute()
'typeId' => $rate->getCode(),
'cost' => round($rate->getPrice() * 100),
];

$this->logger->log([
'SHIPPING METHOD' => $rate->getCode(),
'SHIPPING AMOUNT' => $rate->getPrice()
]);
}
}
$response = ['result' => $methods];

$this->logger->log([
'ESTIMATE SHIPPING METHODS' => $methods
]);

} catch (\Throwable $e) {
$this->logger->log(['ERROR' => $e->getMessage(),'PARAMS'=> $this->getRequest()->getParams()]);
$this->messageManager->addErrorMessage(
Expand Down
6 changes: 6 additions & 0 deletions Controller/Checkout/EstimateTax.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,12 @@ public function execute()
}

$result = round($shippingAddress->getTaxAmount() * 100);

$this->logger->log([
'SHIPPING ADDRESS TAX AMOUNT' => $shippingAddress->getTaxAmount(),
'ESTIMATE TAX RETURNED' => $result
]);

$response = ['result' => $result];
} catch (\Throwable $e) {
$this->logger->log(['EXCEPTION IN TAX ESTIMATE ACTION' => $e->getMessage()]);
Expand Down
7 changes: 7 additions & 0 deletions Controller/Checkout/ValidateTotals.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ public function execute()
$result = ['valid' => false];

if (isset($params['bread_transaction_id'])) {
$this->logger->info([
'MESSAGE' => 'tx_id is set',
'TX_ID' => $params['bread_transaction_id']
]);
if ($this->helper->validateTransactionAmount($params['bread_transaction_id'])) {
$this->logger->info(['MESSAGE' => 'tx amount is valid']);
$result['valid'] = true;
} else {
$errorMsg = __(
'Your order total does not match the amount authorized by Bread.
Please complete checkout again before placing the order.'
);
$this->logger->info(['MESSAGE' => 'order total doesnt match amount authorized']);
}
} else {
$this->logger->info(['ERROR' => 'tx_id not set']);
$errorMsg = __('Please complete the Bread checkout form before placing the order.');
}

Expand Down
35 changes: 33 additions & 2 deletions Helper/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

class Checkout extends Quote
{

/**
* @var \Psr\Log\LoggerInterface
*/
public $logger;

const BREAD_AMOUNT = "bread_transaction_amount";

public function __construct(
Expand All @@ -23,8 +29,10 @@ public function __construct(
\Magento\Sales\Model\AdminOrder\Create $orderCreateModel,
\Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency,
\Bread\BreadCheckout\Model\Payment\Api\Client $paymentApiClient,
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository
\Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
\Bread\BreadCheckout\Helper\Log $logger
) {
$this->logger = $logger;
parent::__construct(
$helperContext,
$context,
Expand Down Expand Up @@ -76,10 +84,33 @@ public function validateTransactionAmount($transactionId)
$quoteTotal = (int)($this->priceCurrency->round($this->getSessionQuote()->getGrandTotal() * 100));

if ($breadAmount === 0) {
$this->logger->info('bread amount is 0');
$info = $this->paymentApiClient->getInfo($transactionId);
$this->setBreadTransactionAmount($info['adjustedTotal']);
}

return (bool) ($breadAmount == $quoteTotal || (abs((int)$breadAmount - (int)$quoteTotal) <= 2));
$areAmountsEqual = (bool) (abs((int)$breadAmount - $quoteTotal) <= 2);

if (!$areAmountsEqual) {
$quote = $this->getSessionQuote();

$itemPrices = array_map(function($item) {
return $item->getPrice() * 100;
}, $quote->getItems());

$this->logger->log([
'LOCATION' => __CLASS__,
'SESSION QUOTE GRAND TOTAL' => ($quote->getGrandTotal() * 100),
'SESSION QUOTE AFTER ROUND METHOD' => ($quoteTotal),
'SESSION QUOTE SUB TOTAL' => ($quote->getSubtotal() * 100),
'SESSION QUOTE SUB TOTAL W/ DISCOUNT' => ($quote->getSubtotalWithDiscount() * 100),
'SESSION QUOTE SHIPPING ADDRESS TAX AMOUNT' => ($quote->getShippingAddress()->getBaseTaxAmount() * 100),
'SESSION QUOTE SHIPPING COST' => ($quote->getShippingAddress()->getShippingAmount() * 100),
'SESSION QUOTE ITEM PRICES' => $itemPrices
]);

}

return $areAmountsEqual;
}
}
13 changes: 13 additions & 0 deletions Helper/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,17 @@ public function log($data)
$this->breadLogger->debug($data);
}
}

/**
* @param $data
*/
public function info($data)
{
if ($this->logEnabled()) {
if (!is_string($data)) {
$data = print_r($data, true);
}
$this->breadLogger->info($data);
}
}
}
27 changes: 27 additions & 0 deletions Model/Payment/Api/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,27 @@ class Client extends \Magento\Framework\Model\AbstractModel

public $logger;

/**
* @var \Magento\Checkout\Model\Session
*/
private $checkoutSession;

public function __construct(
\Magento\Framework\Model\Context $context,
\Magento\Framework\Registry $registry,
\Bread\BreadCheckout\Helper\Data $helper,
\Magento\Framework\Json\Helper\Data $jsonHelper,
\Magento\Store\Model\StoreResolver $storeResolver,
\Bread\BreadCheckout\Helper\Log $log,
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Framework\App\CacheInterface $cache
) {
$this->context = $context;
$this->helper = $helper;
$this->jsonHelper = $jsonHelper;
$this->storeResolver = $storeResolver;
$this->logger = $log;
$this->checkoutSession = $checkoutSession;
$this->cache = $cache;
parent::__construct($context, $registry);
}
Expand Down Expand Up @@ -130,6 +137,26 @@ public function authorize($breadTransactionId, $amount, $merchantOrderId = null)
$amount = trim($amount);

if (((int) $breadAmount != (int) $amount) && (abs((int)$breadAmount - (int)$amount) >= 2)) {
$quote = $this->checkoutSession->getQuote();

$items = $quote->getItems();
$itemPrices = "No Item Prices";
if (is_array($items)) {
$itemPrices = array_map(function ($item) {
return $item->getPrice() * 100;
}, $items);
}

$this->logger->log([
'LOCATION' => __CLASS__,
'SESSION QUOTE GRAND TOTAL' => ($quote->getGrandTotal() * 100),
'SESSION QUOTE SUB TOTAL' => ($quote->getSubtotal() * 100),
'SESSION QUOTE SUB TOTAL W/ DISCOUNT' => ($quote->getSubtotalWithDiscount() * 100),
'SESSION QUOTE SHIPPING ADDRESS TAX AMOUNT' => ($quote->getShippingAddress()->getBaseTaxAmount() * 100),
'SESSION QUOTE SHIPPING COST' => ($quote->getShippingAddress()->getShippingAmount() * 100),
'SESSION QUOTE ITEM PRICES' => $itemPrices
]);

$this->logger->log(
[
'ERROR' =>'BREAD AMOUNT AND QUOTE AMOUNT MIS-MATCH',
Expand Down
48 changes: 42 additions & 6 deletions Model/Payment/Method/Bread.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,13 @@ public function fetchTransactionInfo(\Magento\Payment\Model\InfoInterface $payme
public function validate()
{
$paymentInfo = $this->getInfoInstance();
$this->breadLogger->info('got payment info');
if ($paymentInfo instanceof \Magento\Sales\Model\Order\Payment) {
$billingCountry = $paymentInfo->getOrder()->getBillingAddress()->getCountryId();
$this->breadLogger->info('payment info instance of payment');
$billingCountry = $paymentInfo->getOrder()->getBillingAddress()->getCountryId();
} else {
$billingCountry = $paymentInfo->getQuote()->getBillingAddress()->getCountryId();
$this->breadLogger->info('payment info NOT instance of payment');
$billingCountry = $paymentInfo->getQuote()->getBillingAddress()->getCountryId();
}

if (!$this->canUseForCountry($billingCountry)) {
Expand All @@ -194,6 +197,7 @@ public function validate()
)
);
}
$this->breadLogger->info('can use billing country');

$token = $this->getToken();
if (empty($token)) {
Expand All @@ -205,6 +209,7 @@ public function validate()
)
);
}
$this->breadLogger->info('validate succeeded');

return $this;
}
Expand Down Expand Up @@ -248,12 +253,25 @@ public function void(\Magento\Payment\Model\InfoInterface $payment)
public function authorize(\Magento\Payment\Model\InfoInterface $payment, $amount)
{
if (!$this->canAuthorize()) {
$this->breadLogger->info('authorize action is not available');
throw new \Magento\Framework\Exception\LocalizedException(__('Authorize action is not available.'));
}

$this->breadLogger->info([
'MESSAGE' => 'about to set amount in authorize',
'amount' => $amount
]);
$payment->setAmount($amount);
$this->breadLogger->info('about to set isTxClosed in authorize');
$payment->setIsTransactionClosed(false);
$payment->setTransactionId($this->getToken());
$tx_id = $this->getToken();
$this->breadLogger->info([
'MESSAGE' => 'about to set tx_id in authorize',
'tx_id' => $tx_id
]);
$payment->setTransactionId($tx_id);

$this->breadLogger->info('all payment info set in authorize');

$this->_place($payment, $amount, self::ACTION_AUTHORIZE);
return $this;
Expand Down Expand Up @@ -385,13 +403,31 @@ public function order(\Magento\Payment\Model\InfoInterface $payment, $amount)
protected function _place(\Magento\Payment\Model\InfoInterface $payment, $amount, $requestType)
{
$this->apiClient->setOrder($payment->getOrder());
$this->breadLogger->info('api client order was set');

switch ($requestType) {
case self::ACTION_AUTHORIZE:
$this->breadLogger->info('about to call api client authorize');
$tx_id = $this->getValidatedTxId($payment);
$this->breadLogger->info('got tx_id in place authorize');
$amount = ($this->priceCurrency->round($amount) * 100);
$this->breadLogger->info([
'MESSAGE' => 'got amount in place authorize',
'amount' => $amount
]);
$orderId = $payment->getOrder()->getIncrementId();
$this->breadLogger->info([
'MESSAGE' => 'got orderId in place authorize',
'orderId' => $orderId
]);

$result = $this->apiClient->authorize(
$this->getValidatedTxId($payment),
($this->priceCurrency->round($amount) * 100),
$payment->getOrder()->getIncrementId()
$tx_id,
$amount,
$orderId
);
$this->breadLogger->info('called api client authorize');

$payment->setTransactionId($result['breadTransactionId']);
$this->addTransactionInfo(
$payment,
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Offers the Bread financing and checkout tools for your Magento store",
"license": "MIT",
"minimum-stability": "stable",
"version": "1.1.12",
"version": "1.1.12.1",
"require": {
"php": "~7.0.13||~7.1.0||~7.1.3||~7.2.0||~7.3.0"
},
Expand Down
26 changes: 14 additions & 12 deletions view/base/web/js/sentry/sentry-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,24 @@ define(
return function (config) {

var TRACKED_TAG_KEYS = [
'plugin_version',
'merchant_api_key',
'plugin_version',
'merchant_api_key',
'tx_id',
];

var getConsoleFunc = function (level) {
//don't print to console for info and debug level
switch (level) {
case 'fatal':
return console.error;
case 'error':
return console.error;
case 'warning':
return console.warn;
case 'info':
return console.info;
case 'debug':
return console.log;
case 'fatal':
return console.error;
case 'error':
return console.error;
case 'warning':
return console.warn;
case 'info':
return function (issue) {};
case 'debug':
return function (issue) {};
}
};

Expand Down
Loading