Skip to content

Commit 035dc88

Browse files
author
root
committed
Release 2.0.0
1 parent dc25b5f commit 035dc88

23 files changed

+512
-23
lines changed

Api/Data/TransactionInfoInterface.php

+31
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,16 @@ interface TransactionInfoInterface extends ExtensibleDataInterface
101101
*/
102102
const TRANSACTION_ID = 'transaction_id';
103103

104+
/**
105+
* Success URL to redirect the customer after placing the order.
106+
*/
107+
const SUCCESS_URL = 'success_url';
108+
109+
/**
110+
* Failure URL to redirect the customer after placing the order.
111+
*/
112+
const FAILURE_URL = 'failure_url';
113+
104114
/**
105115
* Gets the authorization amount of the transaction info.
106116
*
@@ -205,4 +215,25 @@ public function getState();
205215
* @return int Transaction ID.
206216
*/
207217
public function getTransactionId();
218+
219+
/**
220+
* Gets the success URL to redirection of the transaction info.
221+
*
222+
* @return int Transaction ID.
223+
*/
224+
public function getSuccessUrl();
225+
226+
/**
227+
* Gets the failure URL to redirection of the transaction info.
228+
*
229+
* @return int Transaction ID.
230+
*/
231+
public function getFailureUrl();
232+
233+
/**
234+
* Check if the transaction is an external payment.
235+
*
236+
* @return bool
237+
*/
238+
public function isExternalPaymentUrl();
208239
}

Api/TransactionInfoManagementInterface.php

+10
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,14 @@ interface TransactionInfoManagementInterface
2929
* @return Data\TransactionInfoInterface
3030
*/
3131
public function update(Transaction $transaction, Order $order);
32+
33+
/**
34+
* Update the transaction info with the success and failure URL to redirect the customer after placing the order
35+
*
36+
* @param Transaction $transaction
37+
* @param string $successUrl
38+
* @param string $failureUrl
39+
* @return Data\TransactionInfoInterface
40+
*/
41+
public function setRedirectUrls(Transaction $transaction, $successUrl, $failureUrl);
3242
}

Model/Config/Source/IntegrationMethod.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class IntegrationMethod implements \Magento\Framework\Option\ArrayInterface
1818

1919
const IFRAME = 'iframe';
2020
const LIGHTBOX = 'lightbox';
21+
const PAYMENT_PAGE = 'paymentpage';
2122

2223
public function toOptionArray()
2324
{
@@ -29,7 +30,7 @@ public function toOptionArray()
2930
[
3031
'value' => self::LIGHTBOX,
3132
'label' => \__('Lightbox')
32-
]
33+
]
3334
];
3435
}
3536
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
/**
3+
* PostFinance Checkout Magento 2
4+
*
5+
* This Magento 2 extension enables to process payments with PostFinance Checkout (https://postfinance.ch/en/business/products/e-commerce/postfinance-checkout-all-in-one.html/).
6+
*
7+
* @package PostFinanceCheckout_Payment
8+
* @author wallee AG (http://www.wallee.com/)
9+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License (ASL 2.0)
10+
*/
11+
namespace PostFinanceCheckout\Payment\Model\Resolver;
12+
13+
use Magento\Checkout\Model\Session as CheckoutSession;
14+
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\GraphQl\Config\Element\Field;
18+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
19+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
20+
use Magento\Framework\GraphQl\Query\ResolverInterface;
21+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
22+
use Magento\Customer\Model\Session;
23+
use Magento\GraphQl\Model\Query\ContextInterface;
24+
use Magento\Sales\Api\OrderRepositoryInterface;
25+
use Psr\Log\LoggerInterface;
26+
use PostFinanceCheckout\Payment\Model\Service\Quote\TransactionService as TransactionQuoteService;
27+
use PostFinanceCheckout\Payment\Model\Service\Order\TransactionService as TransactionOrderService;
28+
29+
class CustomerOrderTransactionSettings implements ResolverInterface
30+
{
31+
/**
32+
*
33+
* @var Session
34+
*/
35+
private $customerSession;
36+
37+
/**
38+
*
39+
* @var CheckoutSession
40+
*/
41+
private $checkoutSession;
42+
43+
/**
44+
*
45+
* @var GetCustomer
46+
*/
47+
private $getCustomer;
48+
49+
/**
50+
*
51+
* @var OrderRepositoryInterface
52+
*/
53+
private $orderRepository;
54+
55+
/**
56+
*
57+
* @var TransactionQuoteService
58+
*/
59+
private $transactionQuoteService;
60+
61+
/**
62+
*
63+
* @var TransactionOrderService
64+
*/
65+
private $transactionOrderService;
66+
67+
/**
68+
*
69+
* @var LoggerInterface
70+
*/
71+
private $logger;
72+
73+
74+
public function __construct(
75+
Session $customerSession,
76+
CheckoutSession $checkoutSession,
77+
GetCustomer $getCustomer,
78+
OrderRepositoryInterface $orderRepository,
79+
TransactionQuoteService $transactionQuoteService,
80+
TransactionOrderService $transactionOrderService,
81+
LoggerInterface $logger
82+
) {
83+
$this->customerSession = $customerSession;
84+
$this->checkoutSession = $checkoutSession;
85+
$this->getCustomer = $getCustomer;
86+
$this->logger = $logger;
87+
$this->transactionQuoteService = $transactionQuoteService;
88+
$this->transactionOrderService = $transactionOrderService;
89+
$this->orderRepository = $orderRepository;
90+
}
91+
92+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
93+
{
94+
//only perform validations if the user is anonymous.
95+
if ($this->checkoutSession->getQuote()->getCustomerId()) {
96+
/** @var ContextInterface $context */
97+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
98+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
99+
}
100+
101+
$customer = $this->getCustomer->execute($context);
102+
if (!empty($this->customerSession) && $customer->getId() !== $this->customerSession->getCustomer()->getId()) {
103+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
104+
}
105+
}
106+
107+
try {
108+
$orderId = $args['order_id'];
109+
$integrationType = $args['integration_type'];
110+
return $this->getTransactionSettings($orderId, $integrationType);
111+
} catch (NoSuchEntityException $e) {
112+
$this->logger->critical($e);
113+
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
114+
}
115+
}
116+
117+
/**
118+
* Gets the transaction settings to use their custom payment integration
119+
*
120+
* @return array
121+
* @throws NoSuchEntityException
122+
* @throws LocalizedException
123+
*/
124+
private function getTransactionSettings(int $orderId, string $integrationType)
125+
{
126+
/** @var \Magento\Sales\Model\Order $order */
127+
$order = $this->orderRepository->get($orderId);
128+
$transaction = $this->transactionQuoteService->getTransaction(
129+
$order->getPostfinancecheckoutSpaceId(),
130+
$order->getPostfinancecheckoutTransactionId()
131+
);
132+
$url = $this->transactionOrderService->getTransactionPaymentUrl($order, $integrationType);
133+
134+
return [
135+
'order_id' => $order->getId(),
136+
'transaction_id' => $transaction->getId(),
137+
'transaction_state' => $transaction->getState(),
138+
'payment_url' => $url,
139+
'integration_type' => $integrationType
140+
];
141+
}
142+
}
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<?php
2+
/**
3+
* PostFinance Checkout Magento 2
4+
*
5+
* This Magento 2 extension enables to process payments with PostFinance Checkout (https://postfinance.ch/en/business/products/e-commerce/postfinance-checkout-all-in-one.html/).
6+
*
7+
* @package PostFinanceCheckout_Payment
8+
* @author wallee AG (http://www.wallee.com/)
9+
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License (ASL 2.0)
10+
*/
11+
namespace PostFinanceCheckout\Payment\Model\Resolver;
12+
13+
use Magento\Checkout\Model\Session as CheckoutSession;
14+
use Magento\CustomerGraphQl\Model\Customer\GetCustomer;
15+
use Magento\Framework\Exception\LocalizedException;
16+
use Magento\Framework\Exception\NoSuchEntityException;
17+
use Magento\Framework\GraphQl\Config\Element\Field;
18+
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
19+
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
20+
use Magento\Framework\GraphQl\Query\ResolverInterface;
21+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
22+
use Magento\Customer\Model\Session;
23+
use Magento\GraphQl\Model\Query\ContextInterface;
24+
use Psr\Log\LoggerInterface;
25+
use PostFinanceCheckout\Payment\Model\Service\Quote\TransactionService;
26+
use PostFinanceCheckout\Payment\Api\TransactionInfoManagementInterface;
27+
28+
class UpdateTransactionUrls implements ResolverInterface
29+
{
30+
/**
31+
*
32+
* @var Session
33+
*/
34+
private $customerSession;
35+
36+
/**
37+
*
38+
* @var CheckoutSession
39+
*/
40+
private $checkoutSession;
41+
42+
/**
43+
*
44+
* @var GetCustomer
45+
*/
46+
private $getCustomer;
47+
48+
/**
49+
*
50+
* @var TransactionService
51+
*/
52+
private $transactionQuoteService;
53+
54+
/**
55+
*
56+
* @var TransactionInfoManagementInterface
57+
*/
58+
private $transactionInfoManagement;
59+
60+
/**
61+
*
62+
* @var LoggerInterface
63+
*/
64+
private $logger;
65+
66+
67+
public function __construct(
68+
Session $customerSession,
69+
CheckoutSession $checkoutSession,
70+
GetCustomer $getCustomer,
71+
TransactionService $transactionQuoteService,
72+
TransactionInfoManagementInterface $transactionInfoManagement,
73+
LoggerInterface $logger
74+
) {
75+
$this->customerSession = $customerSession;
76+
$this->checkoutSession = $checkoutSession;
77+
$this->getCustomer = $getCustomer;
78+
$this->logger = $logger;
79+
$this->transactionQuoteService = $transactionQuoteService;
80+
$this->transactionInfoManagement = $transactionInfoManagement;
81+
}
82+
83+
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
84+
{
85+
//only perform validations if the user is anonymous.
86+
if ($this->checkoutSession->getQuote()->getCustomerId()) {
87+
/** @var ContextInterface $context */
88+
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
89+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
90+
}
91+
92+
$customer = $this->getCustomer->execute($context);
93+
if (!empty($this->customerSession) && $customer->getId() !== $this->customerSession->getCustomer()->getId()) {
94+
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
95+
}
96+
}
97+
98+
try {
99+
$successUrl = $args['input']['success_url'];
100+
$failureUrl = $args['input']['failure_url'];
101+
$cartId = $args['input']['cart_id'];
102+
return $this->setTransactionUrls($cartId, $successUrl, $failureUrl);
103+
} catch (NoSuchEntityException $e) {
104+
$this->logger->critical($e);
105+
throw new GraphQlNoSuchEntityException(__($e->getMessage()));
106+
}
107+
}
108+
109+
/**
110+
* Update transaction urls to redirect the customer after placing the order
111+
*
112+
* @param $cartId
113+
* @param $successUrl
114+
* @param $failureUrl
115+
* @return array
116+
* @throws LocalizedException
117+
*/
118+
private function setTransactionUrls($cartId, $successUrl, $failureUrl)
119+
{
120+
try {
121+
$quote = $this->checkoutSession->getQuote();
122+
/** @var \PostFinanceCheckout\Payment\Model\ResourceModel\TransactionInfo $transactionInfo */
123+
$transactionInfo = $this->transactionQuoteService->getTransactionByQuote($quote);
124+
125+
$this->transactionInfoManagement->setRedirectUrls($transactionInfo, $successUrl, $failureUrl);
126+
return ['result' => 'OK'];
127+
} catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
128+
return ['result' => 'KO. '.$e->getMessage()];
129+
}
130+
}
131+
}

0 commit comments

Comments
 (0)