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

Fix: added #DS and taxvat to Wallet and Pix #10

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
8 changes: 8 additions & 0 deletions Controller/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PicPay\Checkout\Helper\Data as HelperData;
use PicPay\Checkout\Helper\Order as HelperOrder;
use PicPay\Checkout\Helper\Tds as HelperTds;
use PicPay\Checkout\Model\CallbackFactory;
use PicPay\Checkout\Model\ResourceModel\Callback as CallbackResourceModel;
use Magento\Framework\App\Action\Action;
Expand Down Expand Up @@ -33,6 +34,11 @@ abstract class Callback extends Action implements \Magento\Framework\App\CsrfAwa
*/
protected $helperOrder;

/**
* @var HelperTds
*/
protected $helperTds;

/**
* @var CallbackFactory
*/
Expand Down Expand Up @@ -70,6 +76,7 @@ public function __construct(
ResultFactory $resultFactory,
HelperData $helperData,
HelperOrder $helperOrder,
HelperTds $helperTds,
CallbackFactory $callbackFactory,
CallbackResourceModel $callbackResourceModel,
ManagerInterface $eventManager,
Expand All @@ -78,6 +85,7 @@ public function __construct(
$this->resultFactory = $resultFactory;
$this->helperData = $helperData;
$this->helperOrder = $helperOrder;
$this->helperTds = $helperTds;
$this->callbackFactory = $callbackFactory;
$this->callbackResourceModel = $callbackResourceModel;
$this->eventManager = $eventManager;
Expand Down
19 changes: 10 additions & 9 deletions Controller/Callback/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\ResultFactory;
use PicPay\Checkout\Controller\Callback;
use PicPay\Checkout\Gateway\Http\Client\Api;
use PicPay\Checkout\Helper\Order;
use Laminas\Http\Response;
use PicPay\Checkout\Helper\Order as HelperOrder;
use Magento\Sales\Model\Order as SalesOrder;

class Payments extends Callback
{
Expand Down Expand Up @@ -47,13 +43,18 @@ public function execute()
$orderIncrementId = '';

try {
$content = $this->getContent($this->getRequest());
$this->logParams($content);
$webhookData = $this->getContent($this->getRequest());
$this->logParams($webhookData);
$method = 'picpay-payments';
$content = isset($webhookData['type']) ? $webhookData['data'] : $webhookData;

$content = isset($content['type']) ? $content['data'] : $content;

if (isset($content['status'])) {
if (isset($webhookData['type']) && $webhookData['type'] == 'THREE_DS_CHALLENGE') {
$quote = $this->helperTds->loadQuoteByChargeId($content['chargeId']);
if ($quote->getId()) {
contardi marked this conversation as resolved.
Show resolved Hide resolved
$this->helperTds->updateQuote($quote, $content);
$statusCode = Response::STATUS_CODE_200;
}
} else if (isset($content['status'])) {
contardi marked this conversation as resolved.
Show resolved Hide resolved
$chargeId = $content['merchantChargeId'];
if (isset($content['status'])) {
$picpayStatus = $content['status'];
Expand Down
85 changes: 85 additions & 0 deletions Controller/Tds/Challenge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace PicPay\Checkout\Controller\Tds;

use Magento\Checkout\Model\Session;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\HttpGetActionInterface;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Serialize\Serializer\Json;
use Magento\Backend\App\Action\Context;
use Magento\Quote\Model\QuoteRepository;

class Challenge extends Action implements HttpGetActionInterface, CsrfAwareActionInterface
{
/** @var Json */
protected $json;

/** @var Session */
protected $checkoutSession;

/**
* @var JsonFactory
*/
protected JsonFactory $resultJsonFactory;

/**
* @var QuoteRepository
*/
protected $quoteRepository;

public function __construct(
Context $context,
Session $checkoutSession,
Json $json,
JsonFactory $resultJsonFactory,
QuoteRepository $quoteRepository
)
{
$this->checkoutSession = $checkoutSession;
$this->json = $json;
$this->resultJsonFactory = $resultJsonFactory;
$this->quoteRepository = $quoteRepository;

parent::__construct($context);
}

public function execute()
{
$result = $this->resultJsonFactory->create();

$quoteId = $this->checkoutSession->getQuoteId();

if ($quoteId) {
$quote = $this->quoteRepository->get($quoteId);

if ($quote->getPicpayChargeId()) {
contardi marked this conversation as resolved.
Show resolved Hide resolved
$tdsChallengeStatus = $quote->getPicpayChallengeStatus();
return $result->setData([
'challenge_status' => $tdsChallengeStatus,
'charge_id' => $quote->getPicpayChargeId()
]);
}
}

return $result->setData(['error' => true, 'message' => __('No orders found for this user.')]);
}

public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
{
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setHttpResponseCode(403);
return new InvalidRequestException(
$result
);
}

public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
}
102 changes: 102 additions & 0 deletions Controller/Tds/Enrollment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace PicPay\Checkout\Controller\Tds;

use Magento\Backend\App\Action\Context;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\HttpPostActionInterface;
use Magento\Framework\App\CsrfAwareActionInterface;
use Magento\Framework\App\Request\InvalidRequestException;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Controller\Result\JsonFactory;
use Magento\Framework\Controller\ResultFactory;
use Magento\Framework\Serialize\Serializer\Json;
use PicPay\Checkout\Gateway\Http\Client\Api;
use PicPay\Checkout\Model\CheckoutTds;

class Enrollment extends Action implements HttpPostActionInterface, CsrfAwareActionInterface
{
/**
* @var JsonFactory
*/
protected $resultJsonFactory;

/**
* @var Json
*/
protected $json;

/**
* @var Api
*/
protected $api;

/**
* @var CheckoutTds
*/
protected $tds;

/**
* @param Context $context
* @param CheckoutTds $tds
* @param JsonFactory $resultJsonFactory
* @param Json $json
*/
public function __construct(
Context $context,
CheckoutTds $tds,
JsonFactory $resultJsonFactory,
Json $json
) {
$this->resultJsonFactory = $resultJsonFactory;
$this->json = $json;
$this->tds = $tds;

parent::__construct($context);
}

public function execute()
{
$result = $this->resultJsonFactory->create();

try {
$content = $this->getRequest()->getContent();
$bodyParams = ($content) ? $this->json->unserialize($content) : [];
$response = $this->tds->runTdsRequest($bodyParams);

if ($response['response']['chargeId']) {
$result->setJsonData($this->json->serialize($response['response']['transactions'][0]));
}

$responseCode = 200;
} catch (\Exception $e) {
$responseCode = 500;
$this->messageManager->addErrorMessage($e->getMessage());
}

$result->setHttpResponseCode($responseCode);
return $result;
}

/**
* @param RequestInterface $request
* @return InvalidRequestException|null
*/
public function createCsrfValidationException(RequestInterface $request): ?InvalidRequestException
{
$result = $this->resultFactory->create(ResultFactory::TYPE_RAW);
$result->setHttpResponseCode(403);
return new InvalidRequestException(
$result
);
}

/**
* @param RequestInterface $request
* @return bool|null
*/
public function validateForCsrf(RequestInterface $request): ?bool
{
return true;
}
}
18 changes: 17 additions & 1 deletion Gateway/Http/Client/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use PicPay\Checkout\Gateway\Http\Client\Api\Refund;
use PicPay\Checkout\Gateway\Http\Client\Api\Capture;
use PicPay\Checkout\Gateway\Http\Client\Api\Token;
use PicPay\Checkout\Gateway\Http\Client\Api\Tds;
use PicPay\Checkout\Gateway\Http\ClientInterface;
use PicPay\Checkout\Helper\Data;

Expand Down Expand Up @@ -74,6 +75,11 @@ class Api
*/
private $token;

/**
* @var Tds
*/
private $tds;

/**
* @var string
*/
Expand All @@ -88,7 +94,8 @@ public function __construct(
Refund $refund,
Capture $capture,
Card $card,
Query $query
Query $query,
Tds $tds
) {
$this->helper = $helper;
$this->token = $token;
Expand All @@ -99,6 +106,7 @@ public function __construct(
$this->capture = $capture;
$this->card = $card;
$this->query = $query;
$this->tds = $tds;
}

/**
Expand Down Expand Up @@ -186,6 +194,14 @@ public function card(): ClientInterface
return $this->getClient($this->card);
}

/**
* @throws \Exception
*/
public function tds(): ClientInterface
{
return $this->getClient($this->tds);
}

/**
* @throws \Exception
*/
Expand Down
48 changes: 48 additions & 0 deletions Gateway/Http/Client/Api/Tds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

/**
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this extension to newer
* version in the future.
*
* @category PicPay
* @package PicPay_Checkout
*
*/

namespace PicPay\Checkout\Gateway\Http\Client\Api;

use PicPay\Checkout\Gateway\Http\Client;
use Laminas\Http\Request;

class Tds extends Client
{
public function setup(array $data): array
{
$path = $this->getEndpointPath('payments/tds_setup');
$method = Request::METHOD_POST;
return $this->makeRequest($path, $method, 'payments', $data);
}

public function enrollment(array $data): array
{
$path = $this->getEndpointPath('payments/tds_enrollment');
$method = Request::METHOD_POST;
return $this->makeRequest($path, $method, 'payments', $data);
}

public function challengeStatus($chargeId): array
{
$path = $this->getEndpointPath('payments/tds_challenge_status');
$method = Request::METHOD_GET;
return $this->makeRequest($path, $method);
}

public function authorization($data): array
{
$path = $this->getEndpointPath('payments/tds_authorization');
$method = Request::METHOD_POST;
return $this->makeRequest($path, $method, 'payments', $data);
}
}
7 changes: 6 additions & 1 deletion Gateway/Http/Client/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public function placeRequest(TransferInterface $transferObject)
break;

default:
$transaction = $this->api->create()->execute($requestBody, $config['store_id']);
if ($config['use_tds']) {
contardi marked this conversation as resolved.
Show resolved Hide resolved
$transaction = $this->api->tds()->authorization($requestBody);
$transaction['response'] = $transaction['response']['charge'] ?? $transaction['response'];
} else {
$transaction = $this->api->create()->execute($requestBody, $config['store_id']);
}
}

$this->api->logResponse($transaction, self::LOG_NAME);
Expand Down
2 changes: 1 addition & 1 deletion Gateway/Request/CaptureRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function build(array $buildSubject)
];

$clientConfig = [
'order_id' => $payment->getAdditionalInformation('merchantChargeId'),
'order_id' => $payment->getAdditionalInformation('merchantChargeId') ?? $order->getPicpayMerchantId(),
'status' => $payment->getAdditionalInformation('status'),
'store_id' => $order->getStoreId()
];
Expand Down
Loading