Skip to content

Commit

Permalink
Merge pull request #33 from elisei/Magento@2.4.6
Browse files Browse the repository at this point in the history
PagBank 😍 Magento
  • Loading branch information
elisei authored Sep 19, 2023
2 parents 2c952c3 + 600c8b4 commit e9b490b
Showing 1 changed file with 75 additions and 18 deletions.
93 changes: 75 additions & 18 deletions Gateway/Response/FetchPaymentHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use InvalidArgumentException;
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface;
use Magento\Payment\Gateway\Response\HandlerInterface;
use Magento\Payment\Model\InfoInterface;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;

/**
Expand Down Expand Up @@ -60,6 +61,11 @@ class FetchPaymentHandler implements HandlerInterface
*/
public const RESPONSE_STATUS_DECLINED = 'DECLINED';

/**
* Response Pay Status Canceled - Value.
*/
public const RESPONSE_STATUS_CANCELED = 'CANCELED';

/**
* Response Pay Authorized - Block name.
*/
Expand All @@ -70,13 +76,19 @@ class FetchPaymentHandler implements HandlerInterface
*/
protected $invoiceSender;

/**
* @var String
*/
protected $finalStatus;

/**
* @param InvoiceSender $invoiceSender
*/
public function __construct(
InvoiceSender $invoiceSender
) {
$this->invoiceSender = $invoiceSender;
$this->finalStatus = null;
}

/**
Expand Down Expand Up @@ -104,40 +116,85 @@ public function handle(array $handlingSubject, array $response)

$amount = $order->getBaseGrandTotal();

$order = $payment->getOrder();

if (isset($response[self::RESPONSE_CHARGES])) {
$charges = $response[self::RESPONSE_CHARGES][0];
$pagbankPayId = $charges[self::RESPONSE_PAGBANK_ID];
$charges = $response[self::RESPONSE_CHARGES];
$pagbankPayId = $charges[0][self::RESPONSE_PAGBANK_ID];
$paymentParentId = $pagbankPayId;

if (isset($response[self::RESPONSE_QR_CODES])) {
$qrCodes = $response[self::RESPONSE_QR_CODES][0];
$paymentParentId = $qrCodes[self::RESPONSE_PAGBANK_ID];
$paymentParentId = $response[self::RESPONSE_QR_CODES][0][self::RESPONSE_PAGBANK_ID];
}

if ($charges[self::RESPONSE_STATUS] === self::RESPONSE_AUTHORIZED) {
$payment->setIsTransactionApproved(false);
$payment->setIsTransactionDenied(false);
$payment->setIsInProcess(false);
$order->setStatus('payment_review');
$comment = __('Awaiting payment review.');
$order->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());
}
$this->findForPaymentStatus($response, $charges);

if ($charges[self::RESPONSE_STATUS] === self::RESPONSE_STATUS_PAID) {
$charges = $response[self::RESPONSE_CHARGES][0];
$pagbankPayId = $charges[self::RESPONSE_PAGBANK_ID];
if ($this->finalStatus === 'PAID') {
$this->setPaymentPay($payment, $paymentParentId, $pagbankPayId, $amount);
}

if ($charges[self::RESPONSE_STATUS] === self::RESPONSE_STATUS_DECLINED) {
if ($this->finalStatus === 'AUTH') {
$this->setPaymentAuth($payment);
}

if ($this->finalStatus === 'CANCEL') {
$this->setPaymentDeny($payment, $paymentParentId, $pagbankPayId, $amount);
}
}
}
}

/**
* Find for Payment Status.
*
* @param array $response
* @param array $charges
*/
public function findForPaymentStatus($response, $charges)
{
$isPix = isset($response[self::RESPONSE_QR_CODES]) ? true : false;
$isTempCancel = false;

foreach ($charges as $charge) {
switch ($charge[self::RESPONSE_STATUS]) {
case self::RESPONSE_AUTHORIZED:
$this->finalStatus = 'AUTH';
break;

case self::RESPONSE_STATUS_PAID:
$this->finalStatus = 'PAID';
break;

case self::RESPONSE_STATUS_CANCELED:
case self::RESPONSE_STATUS_DECLINED:
if ($isPix) {
$isTempCancel = $charge['summary']['paid'] === 0 ? 1 : 0;
}

if (!$isTempCancel && !$this->finalStatus) {
$this->finalStatus = 'CANCEL';
}
break;
}
}
}

/**
* Set Payment Auth.
*
* @param InfoInterface $payment
*
* @return void
*/
public function setPaymentAuth($payment)
{
$order = $payment->getOrder();
$payment->setIsTransactionApproved(false);
$payment->setIsTransactionDenied(false);
$payment->setIsInProcess(false);
$order->setStatus('payment_review');
$comment = __('Awaiting payment review.');
$order->addStatusHistoryComment($comment, $payment->getOrder()->getStatus());
}

/**
* Set Payment Pay.
*
Expand Down

0 comments on commit e9b490b

Please sign in to comment.