Skip to content

Commit

Permalink
Merge pull request #143 from genecommerce/4.0.6-develop
Browse files Browse the repository at this point in the history
v4.0.6 - Bug Fixes and New Features
  • Loading branch information
kartikmaniyar authored Jan 28, 2021
2 parents a36baf0 + d9a05be commit 35da85a
Show file tree
Hide file tree
Showing 60 changed files with 1,844 additions and 503 deletions.
8 changes: 1 addition & 7 deletions Block/Adminhtml/Form/Field/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ protected function _getElementHtml(AbstractElement $element): string
$title = __('Validate Credentials');
$envId = 'select-groups-braintree-section-groups-braintree-groups-braintree-'
. 'required-fields-environment-value';
$merchantId = 'text-groups-braintree-section-groups-braintree-groups-braintree-'
. 'required-fields-merchant-id-value';
$publicKeyId = 'password-groups-braintree-section-groups-braintree-groups-braintree-'
. 'required-fields-public-key-value';
$privateKeyId = 'password-groups-braintree-section-groups-braintree-groups-braintree-'
. 'required-fields-private-key-value';
$storeId = 0;

if ($this->getRequest()->getParam('website')) {
Expand All @@ -58,7 +52,7 @@ protected function _getElementHtml(AbstractElement $element): string
type="button"
title="{$title}"
class="button"
onclick="braintreeValidator.call(this, '{$endpoint}', '{$envId}', '{$merchantId}', '{$publicKeyId}', '{$privateKeyId}')">
onclick="braintreeValidator.call(this, '{$endpoint}', '{$envId}')">
<span>{$title}</span>
</button>
TEXT;
Expand Down
23 changes: 20 additions & 3 deletions Block/Paypal/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
class Button extends Template implements ShortcutInterface
{
const ALIAS_ELEMENT_INDEX = 'alias';

const BUTTON_ELEMENT_INDEX = 'button_id';

/**
Expand Down Expand Up @@ -181,11 +180,21 @@ public function isPayLaterActive(): bool
}

/**
* @param string $type
* @return bool
*/
public function isPayLaterMessageActive($type): bool
{
return $this->payPalPayLaterConfig->isMessageActive($type);
}

/**
* @param string $type
* @return bool
*/
public function isPayLaterMessageActive(): bool
public function isPayLaterButtonActive($type): bool
{
return $this->payPalPayLaterConfig->isMessageActive();
return $this->payPalPayLaterConfig->isButtonActive($type);
}

/**
Expand Down Expand Up @@ -274,4 +283,12 @@ public function getExtraClassname(): string
{
return $this->getIsCart() ? 'cart' : 'minicart';
}

/**
* @return bool
*/
public function isRequiredBillingAddress(): bool
{
return (bool) $this->config->isRequiredBillingAddress();
}
}
2 changes: 1 addition & 1 deletion Block/Paypal/ProductPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public function getAmount(): float
return $groupedProducts[0]->getPrice();
}

return $product->getPrice();
return $product->getFinalPrice();
}

return 100; // TODO There must be a better return value than this?
Expand Down
6 changes: 5 additions & 1 deletion Block/System/Config/Form/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
}

/**
* Remove UK specific fields from the form when on a non-UK merchant country
* Remove US & UK specific fields from the form if merchant country is not UK or US.
*
* @inheritDoc
*/
Expand All @@ -68,7 +68,11 @@ protected function _getChildrenElementsHtml(AbstractElement $element): string //
);
}

// Only available to GB and US
if ($locale !== 'gb' && $locale !== 'us') {
$element->removeField(
'payment_' . $locale . '_braintree_section_braintree_braintree_paypal_credit_active'
);
$element->removeField(
'payment_other_braintree_section_braintree_braintree_paypal_credit_active'
);
Expand Down
3 changes: 1 addition & 2 deletions Console/VaultMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ class VaultMigrate extends Command
'master-card' => 'MC',
'visa' => 'VI',
'maestro' => 'MI',
'diners-club' => 'DN',
'unionpay' => 'CUP'
'diners-club' => 'DN'
];

/**
Expand Down
16 changes: 13 additions & 3 deletions Controller/Adminhtml/Configuration/Validate.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Magento\Framework\Controller\ResultFactory;
use Magento\Braintree\Gateway\Config\Config;
use Magento\Framework\Controller\ResultInterface;
use Magento\Braintree\Model\Adminhtml\Source\Environment;

/**
* Class Validate
Expand Down Expand Up @@ -44,19 +45,28 @@ public function execute(): ResultInterface
$publicKey = $this->getRequest()->getParam('public_key');
$privateKey = $this->getRequest()->getParam('private_key');
$storeId = $this->getRequest()->getParam('storeId', 0);
$environment = $this->getRequest()->getParam('environment');

if (false !== strpos($publicKey, '*')) {
$publicKey = $this->config->getValue(Config::KEY_PUBLIC_KEY, $storeId);
if ($environment === Environment::ENVIRONMENT_SANDBOX) {
$publicKey = $this->config->getValue(Config::KEY_SANDBOX_PUBLIC_KEY, $storeId);
} else {
$publicKey = $this->config->getValue(Config::KEY_PUBLIC_KEY, $storeId);
}
}

if (false !== strpos($privateKey, '*')) {
$privateKey = $this->config->getValue(Config::KEY_PRIVATE_KEY, $storeId);
if ($environment === Environment::ENVIRONMENT_SANDBOX) {
$privateKey = $this->config->getValue(Config::KEY_SANDBOX_PRIVATE_KEY, $storeId);
} else {
$privateKey = $this->config->getValue(Config::KEY_PRIVATE_KEY, $storeId);
}
}

$response = $this->resultFactory->create(ResultFactory::TYPE_JSON);

try {
Configuration::environment($this->getRequest()->getParam('environment'));
Configuration::environment($environment);
Configuration::merchantId($this->getRequest()->getParam('merchant_id'));
Configuration::publicKey($publicKey);
Configuration::privateKey($privateKey);
Expand Down
49 changes: 28 additions & 21 deletions Cron/CreditPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@

namespace Magento\Braintree\Cron;

use Exception;
use Magento\Braintree\Api\CreditPriceRepositoryInterface;
use Magento\Braintree\Api\Data\CreditPriceDataInterface;
use Magento\Braintree\Api\Data\CreditPriceDataInterfaceFactory;
use Magento\Braintree\Gateway\Config\PayPalCredit\Config as PayPalCreditConfig;
use Magento\Braintree\Model\Paypal\CreditApi;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory as ProductCollectionFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Exception\AuthenticationException;
use Magento\Framework\Exception\LocalizedException;
use Psr\Log\LoggerInterface;
use RuntimeException;

/**
* Class CreditPrice
Expand All @@ -22,12 +16,12 @@
class CreditPrice
{
/**
* @var CreditPriceRepositoryInterface
* @var \Magento\Braintree\Api\CreditPriceRepositoryInterface
*/
private $creditPriceRepository;

/**
* @var ScopeConfigInterface
* @var \Magento\Framework\App\Config\ScopeConfigInterface
*/
private $scopeConfig;

Expand All @@ -37,7 +31,7 @@ class CreditPrice
private $productCollection;

/**
* @var CreditPriceDataInterfaceFactory
* @var \Magento\Braintree\Api\Data\CreditPriceDataInterfaceFactory
*/
private $creditPriceFactory;

Expand All @@ -47,7 +41,7 @@ class CreditPrice
private $logger;

/**
* @var CreditApi
* @var \Magento\Braintree\Model\Paypal\CreditApi
*/
private $creditApi;

Expand All @@ -58,19 +52,19 @@ class CreditPrice

/**
* CreditPrice constructor.
* @param CreditPriceRepositoryInterface $creditPriceRepository
* @param CreditPriceDataInterfaceFactory $creditPriceDataInterfaceFactory
* @param ScopeConfigInterface $scopeConfig
* @param CreditApi $creditApi
* @param \Magento\Braintree\Api\CreditPriceRepositoryInterface $creditPriceRepository
* @param \Magento\Braintree\Api\Data\CreditPriceDataInterfaceFactory $creditPriceDataInterfaceFactory
* @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
* @param \Magento\Braintree\Model\Paypal\CreditApi $creditApi
* @param ProductCollectionFactory $productCollection
* @param LoggerInterface $logger
* @param PayPalCreditConfig $config
*/
public function __construct(
CreditPriceRepositoryInterface $creditPriceRepository,
CreditPriceDataInterfaceFactory $creditPriceDataInterfaceFactory,
ScopeConfigInterface $scopeConfig,
CreditApi $creditApi,
\Magento\Braintree\Api\CreditPriceRepositoryInterface $creditPriceRepository,
\Magento\Braintree\Api\Data\CreditPriceDataInterfaceFactory $creditPriceDataInterfaceFactory,
\Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
\Magento\Braintree\Model\Paypal\CreditApi $creditApi,
ProductCollectionFactory $productCollection,
LoggerInterface $logger,
PayPalCreditConfig $config
Expand All @@ -86,19 +80,23 @@ public function __construct(

/**
* @return $this
* @throws Exception
* @throws \Exception
*/
public function execute(): self
public function execute()
{
if (!$this->config->isCalculatorEnabled()) {
return $this;
}

// Retrieve paginated collection of product and their price
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->productCollection->create();
$collection->addAttributeToSelect('price')
->setPageSize(100);

$connection = $collection->getResource()->getConnection();
$connection->beginTransaction();

$lastPage = $collection->getLastPageNumber();
for ($i = 1; $i <= $lastPage; $i++) {
$collection->setCurPage($i);
Expand All @@ -113,7 +111,7 @@ public function execute(): self
$priceOptions = $this->creditApi->getPriceOptions($product->getFinalPrice());
foreach ($priceOptions as $priceOption) {
// Populate model
/** @var CreditPriceDataInterface $model */
/** @var $model \Magento\Braintree\Api\Data\CreditPriceDataInterface */
$model = $this->creditPriceFactory->create();
$model->setProductId($product->getId());
$model->setTerm($priceOption['term']);
Expand All @@ -124,14 +122,23 @@ public function execute(): self

$this->creditPriceRepository->save($model);
}
} catch (AuthenticationException $e) {
$connection->rollBack();
throw new \Exception($e->getMessage());
} catch (LocalizedException $e) {
$this->logger->critical($e->getMessage());
} catch (\Exception $e) {
$connection->rollBack();
$this->logger->critical($e->getMessage());
return $this;
}
}

$collection->clear();
}

$connection->commit();

return $this;
}
}
18 changes: 16 additions & 2 deletions Gateway/Command/CaptureStrategyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Magento\Sales\Api\Data\OrderPaymentInterface;
use Magento\Sales\Api\TransactionRepositoryInterface;
use Magento\Sales\Api\Data\TransactionInterface;
use Magento\Braintree\Model\Ui\PayPal\ConfigProvider as PaypalConfigProvider;

/**
* Class CaptureStrategyCommand
Expand All @@ -36,6 +37,11 @@ class CaptureStrategyCommand implements CommandInterface
*/
const CAPTURE = 'settlement';

/**
* Braintree partial capture command
*/
const PARTIAL_CAPTURE = 'partial_capture';

/**
* Braintree vault capture command
*/
Expand Down Expand Up @@ -116,18 +122,20 @@ public function execute(array $commandSubject)

/** @var OrderPaymentInterface $paymentInfo */
$paymentInfo = $paymentDO->getPayment();
$amount = $commandSubject['amount'];
ContextHelper::assertOrderPayment($paymentInfo);

$command = $this->getCommand($paymentInfo);
$command = $this->getCommand($paymentInfo, $amount);
$this->commandPool->get($command)->execute($commandSubject);
}

/**
* Get execution command name
* @param OrderPaymentInterface $payment
* @param float $amount
* @return string
*/
private function getCommand(OrderPaymentInterface $payment): string
private function getCommand(OrderPaymentInterface $payment, $amount): string
{
// if auth transaction is not exists execute authorize&capture command
$existsCapture = $this->isExistsCaptureTransaction($payment);
Expand All @@ -136,6 +144,12 @@ private function getCommand(OrderPaymentInterface $payment): string
return self::SALE;
}

// do partial capture for authorization transaction only braintree PayPal
if ($amount < $payment->getAmountAuthorized() && !$this->isExpiredAuthorization($payment)
&& ($payment->getMethod() == PaypalConfigProvider::PAYPAL_CODE)) {
return self::PARTIAL_CAPTURE;
}

// do capture for authorization transaction
if (!$existsCapture && !$this->isExpiredAuthorization($payment)) {
return self::CAPTURE;
Expand Down
10 changes: 8 additions & 2 deletions Gateway/Config/CanVoidHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@ public function __construct(
public function handle(array $subject, $storeId = null)
{
$paymentDO = $this->subjectReader->readPayment($subject);

$canCaptureFlag = true;
$payment = $paymentDO->getPayment();
return $payment instanceof Payment && !(bool)$payment->getAmountPaid();
if ((bool)$payment->getAmountPaid()) {
$canCaptureFlag = false;
}
if ($payment->getAmountPaid() < $payment->getAmountAuthorized() && (bool)$payment->getAmountPaid()) {
$canCaptureFlag = true;
}
return $payment instanceof Payment && $canCaptureFlag;
}
}
Loading

0 comments on commit 35da85a

Please sign in to comment.