Skip to content

Commit

Permalink
StoredCard payment fix
Browse files Browse the repository at this point in the history
  • Loading branch information
krzGablo committed Mar 15, 2024
1 parent 99101c9 commit e5d46e6
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 13 deletions.
31 changes: 30 additions & 1 deletion Model/Resolver/CardPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,53 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Tpay\Magento2\Model\ApiFacade\CardTransaction\CardApiFacade;
use Tpay\Magento2\Service\TpayService;

class CardPayment implements ResolverInterface
{
private CardApiFacade $cardApiFacade;
private TpayService $tpayService;

public function __construct(CardApiFacade $cardApiFacade)
public function __construct(CardApiFacade $cardApiFacade, TpayService $tpayService)
{
$this->cardApiFacade = $cardApiFacade;
$this->tpayService = $tpayService;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$args = $args['input'];
$orderId = $args['incrementId'];
$this->updateAdditionalData($args);

if ($args['transactionId']) {
$payment = $this->tpayService->getPayment($orderId);
$paymentData = $payment->getData();

return ['redirectUrl' => $this->cardApiFacade->payTransaction($orderId, $paymentData['additional_information'], $args['transactionId'])];
}

if ($orderId) {
return ['redirectUrl' => $this->cardApiFacade->makeCardTransaction($orderId)];
}

return ['redirectUrl' => 'error'];
}

private function updateAdditionalData(array $args): void
{
$transactionId = $args['transactionId'];

$payment = $this->tpayService->getPayment($args['incrementId']);
$paymentData = $payment->getData();
$paymentData['additional_information']['card_data'] = $args['cardData'];
$paymentData['additional_information']['card_save'] = $args['storeCard'];

if ($transactionId) {
$paymentData['additional_information']['transaction_id'] = $transactionId;
}

$payment->setData($paymentData);
$this->tpayService->saveOrderPayment($payment);
}
}
8 changes: 6 additions & 2 deletions Model/Resolver/CreateTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Phrase;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Tpay\Magento2\Api\TpayConfigInterface;
use Tpay\Magento2\Api\TpayInterface;
use Tpay\Magento2\Model\ApiFacade\Transaction\TransactionApiFacade;
Expand All @@ -22,14 +23,16 @@ class CreateTransaction implements ResolverInterface
private Session $checkoutSession;
private OrderRepositoryInterface $orderRepository;
private TpayService $tpayService;
private StoreManagerInterface $storeManager;

public function __construct(
TpayInterface $tpay,
TransactionApiFacade $transactionApiFacade,
TpayConfigInterface $tpayConfig,
Session $checkoutSession,
OrderRepositoryInterface $orderRepository,
TpayService $tpayService
TpayService $tpayService,
StoreManagerInterface $storeManager
)
{
$this->transactionApiFacade = $transactionApiFacade;
Expand All @@ -38,6 +41,7 @@ public function __construct(
$this->checkoutSession = $checkoutSession;
$this->orderRepository = $orderRepository;
$this->tpayService = $tpayService;
$this->storeManager = $storeManager;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
Expand All @@ -57,7 +61,7 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
$payment = $order->getPayment();
$paymentData = $payment->getData();

if (!empty($paymentData['additional_information']['card_data']) || $paymentData['additional_information']['card_id']) {
if ('PLN' !== $this->storeManager->getStore()->getCurrentCurrencyCode()) {
return ['transaction' => null, 'redirectUrl' => null];
} else {
$transaction = $this->prepareTransaction($order->getIncrementId(), $paymentData);
Expand Down
57 changes: 54 additions & 3 deletions Model/Resolver/StoredCardPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,75 @@
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Tpay\Magento2\Model\ApiFacade\CardTransaction\CardApiFacade;
use Tpay\Magento2\Service\TpayService;

class StoredCardPayment implements ResolverInterface
{
private CardApiFacade $cardApiFacade;
private StoredTokens $storedTokens;
private TpayService $tpayService;

public function __construct(CardApiFacade $cardApiFacade)
public function __construct(CardApiFacade $cardApiFacade, StoredTokens $storedTokens, TpayService $tpayService)
{
$this->cardApiFacade = $cardApiFacade;
$this->storedTokens = $storedTokens;
$this->tpayService = $tpayService;
}

public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
$args = $args['input'];
$orderId = $args['incrementId'];
if ($orderId) {
return ['redirectUrl' => $this->cardApiFacade->makeCardTransaction($orderId)];

$token = $this->findRequiredToken($args['tokenId'], $this->storedTokens->resolve($field, $context, $info, $value, $args));
$token = $this->handleTokenData($token);

if ($orderId && !empty($token)) {
$this->updateAdditionalData($args, $token);

if ($args['transactionId']) {
$payment = $this->tpayService->getPayment($orderId);
$paymentData = $payment->getData();

return ['redirectUrl' => $this->cardApiFacade->payTransaction($orderId, $paymentData['additional_information'], $args['transactionId'], $token)];
}

return ['redirectUrl' => $this->cardApiFacade->makeCardTransaction($orderId, $token)];
}

return ['redirectUrl' => 'error'];
}

private function handleTokenData(array $token): ?array
{
if (!empty($token)) {
$token = $token[0];
$token['cli_auth'] = $token['token'];
return $token;
}
return null;
}

private function findRequiredToken(string $tokenId, array $tokens): array
{
return array_filter($tokens, function ($token) use ($tokenId) {
return $tokenId == $token['tokenId'];
});
}

private function updateAdditionalData(array $args, array $token): void
{
$transactionId = $args['transactionId'];

$payment = $this->tpayService->getPayment($args['incrementId']);
$paymentData = $payment->getData();
$paymentData['additional_information']['card_id'] = $token['tokenId'];

if ($transactionId) {
$paymentData['additional_information']['transaction_id'] = $transactionId;
}

$payment->setData($paymentData);
$this->tpayService->saveOrderPayment($payment);
}
}
4 changes: 0 additions & 4 deletions etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
</argument>
</arguments>
</type>

<!-- <type name="PayPro\Przelewy24\Gateway\Request\TransactionUrlRequestBuilder">-->
<!-- <plugin name="PayPro_Przelewy24GraphQl::modify_graphql_return_url" type="PayPro\Przelewy24GraphQl\Plugin\ModifyGraphQlReturnUrlPlugin"/>-->
<!-- </type>-->
</config>
6 changes: 3 additions & 3 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Query {

type Mutation {
tpayCardPayment(input: CardPaymentInput): TpayPayOutput @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\CardPayment")
tpayStoredCardPayment(input: StoredCardPaymentInput): TpayPayOutput @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\TokenPayment")
tpayStoredCardPayment(input: StoredCardPaymentInput): TpayPayOutput @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\StoredCardPayment")
tpayBlikPayment(input: BlikPaymentInput): TpayPayOutput @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\BlikPayment")
}

Expand Down Expand Up @@ -64,8 +64,8 @@ input BlikPaymentInput {
}

type CreateTpayTransactionOutput {
transaction: String!
redirectUrl: String!
transaction: String
redirectUrl: String
}

type TpayPayOutput {
Expand Down

0 comments on commit e5d46e6

Please sign in to comment.