Skip to content

Commit

Permalink
TOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Karol Wojciechowski committed Apr 16, 2024
1 parent 83ad060 commit 641523e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
48 changes: 30 additions & 18 deletions Model/Resolver/CreateTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ class CreateTransaction implements ResolverInterface
private StoreManagerInterface $storeManager;

public function __construct(
TpayInterface $tpay,
TransactionApiFacade $transactionApiFacade,
TpayConfigInterface $tpayConfig,
Session $checkoutSession,
TpayInterface $tpay,
TransactionApiFacade $transactionApiFacade,
TpayConfigInterface $tpayConfig,
Session $checkoutSession,
OrderRepositoryInterface $orderRepository,
TpayService $tpayService,
StoreManagerInterface $storeManager
) {
TpayService $tpayService,
StoreManagerInterface $storeManager
)
{
$this->transactionApiFacade = $transactionApiFacade;
$this->tpay = $tpay;
$this->tpayConfig = $tpayConfig;
Expand All @@ -46,21 +47,32 @@ public function __construct(

public function resolve(Field $field, $context, ResolveInfo $info, ?array $value = null, ?array $args = null)
{
if (!isset($value['order_number'])) {
return;
}
$args = $args['input'] ?? [];
if (isset($args['real_order_id']) && !empty($args['real_order_id'])) {
$orderId = $args['real_order_id'];
} else {
if (!isset($value['order_number'])) {
return;
}

$orderId = $this->checkoutSession->getLastRealOrderId();
if (!$orderId) {
return;
$orderId = $this->checkoutSession->getLastRealOrderId();
if (!$orderId) {
return;
}
}
$transaction = null;
try {
$order = $this->orderRepository->get($orderId);
/** @var \Magento\Sales\Model\Order\Payment $payment */
$payment = $order->getPayment();
$paymentData = $payment->getData();
if ($payment->getMethod() !== TpayInterface::CODE) {
return;
}

$paymentData = $payment->getData();
if ($paymentData['additional_information']['accept_tos'] !== true) {
throw new \Exception('Tpay terms of service not accepted');
}
if ('PLN' !== $this->storeManager->getStore()->getCurrentCurrencyCode()) {
return ['transaction' => null, 'redirectUrl' => null];
}
Expand All @@ -70,13 +82,13 @@ public function resolve(Field $field, $context, ResolveInfo $info, ?array $value
if (isset($transaction['transactionId'])) {
$paymentData['additional_information']['transaction_id'] = $transaction['transactionId'];
}
$this->tpayService->addCommentToHistory($orderId, 'Transaction title '.$transaction['title']);
$this->tpayService->addCommentToHistory($orderId, 'Transaction title ' . $transaction['title']);

if (true === $this->tpayConfig->redirectToChannel()) {
$transactionUrl = str_replace('gtitle', 'title', $transactionUrl);
}

$this->tpayService->addCommentToHistory($orderId, 'Transaction link '.$transactionUrl);
$this->tpayService->addCommentToHistory($orderId, 'Transaction link ' . $transactionUrl);
$paymentData['additional_information']['transaction_url'] = $transactionUrl;
$payment->setData($paymentData);
$this->tpayService->saveOrderPayment($payment);
Expand All @@ -91,8 +103,8 @@ private function prepareTransaction($orderId, array $additionalPaymentInformatio
{
$data = $this->tpay->getTpayFormData($orderId);

$data['group'] = (int) ($additionalPaymentInformation['group'] ?? null);
$data['channel'] = (int) ($additionalPaymentInformation['channel'] ?? null);
$data['group'] = (int)($additionalPaymentInformation['group'] ?? null);
$data['channel'] = (int)($additionalPaymentInformation['channel'] ?? null);

if ($this->tpayConfig->redirectToChannel()) {
$data['direct'] = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
use Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderInterface;
use Tpay\Magento2\Api\TpayInterface;

class TermsOfServiceDataProvider implements AdditionalDataProviderInterface
class TpayDataProvider implements AdditionalDataProviderInterface
{
/** @throws GraphQlInputException */
public function getData(array $data): array
{
$code = $data['code'];
$method = $data[$code]['method'] ?? null;
if (!$method) {
throw new GraphQlInputException(__('No payment method provided!'));
if($code === TpayInterface::CODE) {
$data['tpay'][TpayInterface::TERMS_ACCEPT] ??= false;
}

$data[$code][TpayInterface::TERMS_ACCEPT] ??= false;

return $data[$code];
return $data['tpay'];
}
}

2 changes: 1 addition & 1 deletion etc/graphql/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<type name="Magento\QuoteGraphQl\Model\Cart\Payment\AdditionalDataProviderPool">
<arguments>
<argument name="dataProviders" xsi:type="array">
<item name="tpay_tos" xsi:type="object">Tpay\Magento2GraphQl\Model\TermsOfServiceDataProvider</item>
<item name="Tpay_Magento2" xsi:type="object">Tpay\Magento2GraphQl\Model\TpayDataProvider</item>
</argument>
</arguments>
</type>
Expand Down
12 changes: 12 additions & 0 deletions etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type Query {
}

type Mutation {
tpayCreateTransaction(input: CreateTpayTransactionInput): CreateTpayTransactionOutput @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\CreateTransaction")
tpayCardPayment(input: CardPaymentInput): TpayPayOutput @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\CardPayment")
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 @@ -70,16 +71,27 @@ type CreateTpayTransactionOutput {
redirectUrl: String
}

input CreateTpayTransactionInput {
real_order_id: String!
}

type TpayPayOutput {
transaction: String
result: String
redirectUrl: String!
}

input PaymentMethodInput {
tpay: TpayInput
}

input TpayInput {
channel: Int @doc(description: "Set channel ID, group ID or just ignore it to allow user to choose bank on Tpay paywall")
group: Int
accept_tos: Boolean!
}


type Order {
tpay: CreateTpayTransactionOutput @doc(description: "Tpay transaction output") @resolver(class: "Tpay\\Magento2GraphQl\\Model\\Resolver\\CreateTransaction")
}

0 comments on commit 641523e

Please sign in to comment.