Skip to content

Commit

Permalink
Fix redirect workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleh Vehera committed Apr 24, 2022
1 parent 832161e commit 3279ebd
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 60 deletions.
5 changes: 4 additions & 1 deletion commerce_viva.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ description: 'Viva Wallet payment gateway.'
package: Commerce
core_version_requirement: ^8 || ^9 || ^10
dependencies:
- commerce:commerce_payment
- commerce:commerce
- commerce:commerce_checkout
- commerce:commerce_order
- commerce:commerce_payment
- commerce:commerce_price
2 changes: 1 addition & 1 deletion commerce_viva.libraries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ donate-sdk-inline.js:
js:
src/assets/js/donate-sdk-inline.js: {}
dependencies:
- commerce_viva/donate-sdk.js
- commerce_viva/donate-sdk.js
13 changes: 11 additions & 2 deletions commerce_viva.routing.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
commerce_viva.order_success:
path: '/viva/success-payment'
path: '/viva/{payment_gateway}/success'
defaults:
_controller: '\Drupal\commerce_viva\Controller\PaymentCheckoutController::returnSuccessPage'
requirements:
# Needs to be accessed by an anonymous user.
_access: 'TRUE'
options:
parameters:
payment_gateway:
type: entity:commerce_payment_gateway

commerce_viva.order_error:
path: '/viva/error-payment'
path: '/viva/{payment_gateway}/error'
defaults:
_controller: '\Drupal\commerce_viva\Controller\PaymentCheckoutController::returnErrorPage'
requirements:
# Needs to be accessed by an anonymous user.
_access: 'TRUE'
options:
parameters:
payment_gateway:
type: entity:commerce_payment_gateway
32 changes: 9 additions & 23 deletions src/Controller/PaymentCheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\commerce_viva\Controller;

use Drupal\commerce_checkout\CheckoutOrderManagerInterface;
use Drupal\commerce_payment\Entity\PaymentGatewayInterface;
use Drupal\Component\Serialization\Json;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
Expand All @@ -19,13 +19,6 @@
*/
class PaymentCheckoutController implements ContainerInjectionInterface {

/**
* The checkout order manager.
*
* @var \Drupal\commerce_checkout\CheckoutOrderManagerInterface
*/
protected $checkoutOrderManager;

/**
* The messenger.
*
Expand All @@ -50,8 +43,6 @@ class PaymentCheckoutController implements ContainerInjectionInterface {
/**
* Constructs a new PaymentCheckoutController object.
*
* @param \Drupal\commerce_checkout\CheckoutOrderManagerInterface $checkout_order_manager
* The checkout order manager.
* @param \Drupal\Core\Messenger\MessengerInterface $messenger
* The messenger.
* @param \Psr\Log\LoggerInterface $logger
Expand All @@ -60,12 +51,10 @@ class PaymentCheckoutController implements ContainerInjectionInterface {
* The entity type manager.
*/
public function __construct(
CheckoutOrderManagerInterface $checkout_order_manager,
MessengerInterface $messenger,
LoggerInterface $logger,
EntityTypeManagerInterface $entity_type_manager
) {
$this->checkoutOrderManager = $checkout_order_manager;
$this->messenger = $messenger;
$this->logger = $logger;
$this->entityTypeManager = $entity_type_manager;
Expand All @@ -76,7 +65,6 @@ public function __construct(
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('commerce_checkout.checkout_order_manager'),
$container->get('messenger'),
$container->get('logger.channel.commerce_payment'),
$container->get('entity_type.manager')
Expand All @@ -88,14 +76,14 @@ public static function create(ContainerInterface $container) {
*
* Redirects to the next checkout page, completing checkout.
*
* @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway
* Payment getaway responsible to process request to the controller.
* @param \Symfony\Component\HttpFoundation\Request $request
* The request.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function returnSuccessPage(Request $request, RouteMatchInterface $route_match) {
public function returnSuccessPage(PaymentGatewayInterface $payment_gateway, Request $request) {
$transaction_id = $request->query->get('t');
$order = $this->retrieveTransaction($transaction_id);
$order = $this->retrieveTransaction($payment_gateway, $transaction_id);
// @todo calculate success step from the order.
$step = 'success';
return new RedirectResponse(Url::fromRoute('commerce_payment.checkout.return', [
Expand Down Expand Up @@ -123,6 +111,8 @@ public function returnErrorPage(Request $request, RouteMatchInterface $route_mat
/**
* Order entity getter based on the transaction ID.
*
* @param \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway
* Payment getaway responsible to process request to the controller.
* @param string $transaction_id
* Transaction ID.
*
Expand All @@ -132,13 +122,9 @@ public function returnErrorPage(Request $request, RouteMatchInterface $route_mat
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function retrieveTransaction(string $transaction_id) {
/** @var \Drupal\commerce_payment\PaymentGatewayStorage $payment_storage */
$payment_storage = $this->entityTypeManager->getStorage('commerce_payment_gateway');
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_viva */
$payment_viva = $payment_storage->load('vivawallet');
public function retrieveTransaction(PaymentGatewayInterface $payment_gateway, string $transaction_id) {
/** @var \Drupal\commerce_viva\Plugin\Commerce\PaymentGateway\OffsiteRedirect $payment_plugin */
$payment_plugin = $payment_viva->getPlugin();
$payment_plugin = $payment_gateway->getPlugin();
$curl = curl_init();
$url = $payment_plugin->resolveUrl('demo-api', 'api', "/checkout/v2/transactions/$transaction_id");

Expand Down
64 changes: 35 additions & 29 deletions src/Plugin/Commerce/PaymentGateway/VivaRedirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,34 +114,41 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
]
);

$form['info'] = [
'#type' => 'details',
'#title' => $this->t('Account configuration info'),
'#open' => TRUE,
'redirect_link' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->t(
'To successful work of plugin please use in redirect that links:<br /> - <b>%success_redirect</b> (for success) <br /> - <b>%error_redirect</b> (for failure)',
[
'%success_redirect' => Url::fromRoute(
'commerce_viva.order_success',
[],
[
'absolute' => TRUE,
]
)->toString(),
'%error_redirect' => Url::fromRoute(
'commerce_viva.order_error',
[],
[
'absolute' => TRUE,
]
)->toString(),
]
),
],
];
$payment_gateway = $form_state->getFormObject()->getEntity();
if (!$payment_gateway->isNew()) {
$form['info'] = [
'#type' => 'details',
'#title' => $this->t('Account configuration info'),
'#open' => TRUE,
'redirect_link' => [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $this->t(
'To successful work of plugin please use in redirect that links:<br /> - <b>%success_redirect</b> (for success) <br /> - <b>%error_redirect</b> (for failure)',
[
'%success_redirect' => Url::fromRoute(
'commerce_viva.order_success',
[
'payment_gateway' => $payment_gateway->id(),
],
[
'absolute' => TRUE,
]
)->toString(),
'%error_redirect' => Url::fromRoute(
'commerce_viva.order_error',
[
'payment_gateway' => $payment_gateway->id(),
],
[
'absolute' => TRUE,
]
)->toString(),
]
),
],
];
}

$form['donation'] = [
'#type' => 'container',
Expand All @@ -153,7 +160,6 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
],
];


$form['merchant_id'] = [
'#type' => 'textfield',
'#title' => $this->t('Merchant ID'),
Expand Down
5 changes: 1 addition & 4 deletions src/PluginForm/VivaRedirect/VivaOffsiteForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
use Drupal\commerce_payment\PluginForm\PaymentOffsiteForm as BasePaymentOffsiteForm;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
* Viva payment off-site form.
*/
class VivaOffsiteForm extends BasePaymentOffsiteForm {

use StringTranslationTrait;

/**
* Generate order code for the order entity.
*
Expand Down Expand Up @@ -152,5 +149,5 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$redirect_method
);
}

}

0 comments on commit 3279ebd

Please sign in to comment.