diff --git a/modules/payment/src/Controller/OffsitePaymentMethodUserPageController.php b/modules/payment/src/Controller/OffsitePaymentMethodUserPageController.php index 7a6e3c94..f2a67907 100644 --- a/modules/payment/src/Controller/OffsitePaymentMethodUserPageController.php +++ b/modules/payment/src/Controller/OffsitePaymentMethodUserPageController.php @@ -124,7 +124,8 @@ class OffsitePaymentMethodUserPageController implements ContainerInjectionInterf 'payment_gateway_mode' => $payment_gateway_plugin->getMode(), ]); // In case payment gateway supports payment method. - $payment_gateway_plugin->onReturnOffsitePaymentMethodOnUserPage($payment_method, $request); + $payment_details = $request->isMethod('GET') ? $request->query->all() : $request->request->all(); + $payment_gateway_plugin->createPaymentMethod($payment_method, $payment_details); return new RedirectResponse(Url::fromRoute('entity.commerce_payment_method.collection', ['user' => $user->id()])->toString()); } catch (PaymentGatewayException $e) { diff --git a/modules/payment/src/Controller/PaymentCheckoutController.php b/modules/payment/src/Controller/PaymentCheckoutController.php index 169c249f..a891f1bf 100644 --- a/modules/payment/src/Controller/PaymentCheckoutController.php +++ b/modules/payment/src/Controller/PaymentCheckoutController.php @@ -125,7 +125,8 @@ class PaymentCheckoutController implements ContainerInjectionInterface { 'payment_gateway_mode' => $payment_gateway_plugin->getMode(), ]); // In case payment gateway supports payment method. - $payment_gateway_plugin->createPaymentMethod($payment_method, ['order' => $order, 'request' => $request]); + $payment_details = $request->isMethod('GET') ? $request->query->all() : $request->request->all(); + $payment_gateway_plugin->createPaymentMethod($payment_method, $payment_details); // Add payment method to the order. $order->set('payment_method', $payment_method); diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsCreatingOffsitePaymentMethodsOnUserPageInterface.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsCreatingOffsitePaymentMethodsOnUserPageInterface.php index a2f32e8d..8d06b52a 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsCreatingOffsitePaymentMethodsOnUserPageInterface.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsCreatingOffsitePaymentMethodsOnUserPageInterface.php @@ -9,20 +9,7 @@ use Symfony\Component\HttpFoundation\Request; * Defines the interface for offline gateways which support creating payment * methods outside checkout on the user pages. */ -interface SupportsCreatingOffsitePaymentMethodsOnUserPageInterface { - - /** - * Creates a payment method with the given payment details. - * - * @param \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method - * The payment method. - * @param \Symfony\Component\HttpFoundation\Request $request - * The request object. - * - * @throws \Drupal\commerce_payment\Exception\PaymentGatewayException - * Thrown when the transaction fails for any reason. - */ - public function onReturnOffsitePaymentMethodOnUserPage(PaymentMethodInterface $payment_method, Request $request); +interface SupportsCreatingOffsitePaymentMethodsOnUserPageInterface extends SupportsStoredPaymentMethodsInterface { /** * Reacts to canceling an offsite payment method creation. diff --git a/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsStoredPaymentMethodsInterface.php b/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsStoredPaymentMethodsInterface.php index 15471e38..d5957dd9 100644 --- a/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsStoredPaymentMethodsInterface.php +++ b/modules/payment/src/Plugin/Commerce/PaymentGateway/SupportsStoredPaymentMethodsInterface.php @@ -15,9 +15,8 @@ interface SupportsStoredPaymentMethodsInterface { * @param \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method * The payment method. * @param array $payment_details - * The gateway-specific payment details in case of an onsite gateway. - * The order and the request (with keys 'order' and 'request') in case of - * an offsite gateway. + * The gateway-specific payment details provided by the payment method form + * for on-site gateways, or the incoming request for off-site gateways. * * @throws \Drupal\commerce_payment\Exception\PaymentGatewayException * Thrown when the transaction fails for any reason. diff --git a/modules/payment_example/src/Plugin/Commerce/PaymentGateway/StoredOffsiteRedirect.php b/modules/payment_example/src/Plugin/Commerce/PaymentGateway/StoredOffsiteRedirect.php index 006ecacc..965607dd 100644 --- a/modules/payment_example/src/Plugin/Commerce/PaymentGateway/StoredOffsiteRedirect.php +++ b/modules/payment_example/src/Plugin/Commerce/PaymentGateway/StoredOffsiteRedirect.php @@ -94,13 +94,12 @@ class StoredOffsiteRedirect extends OffsiteRedirect implements SupportsStoredPay * {@inheritdoc} */ public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) { - $request = $payment_details['request']; - $payment_method->card_type = CreditCard::detectType($request->query->get('cardprefix'))->getId(); - $payment_method->card_number = substr($request->query->get('cardnomask'), -4); - $payment_method->card_exp_month = substr($request->query->get('cardexpdate'), 0, 2); - $payment_method->card_exp_year = substr($request->query->get('cardexpdate'), -2); + $payment_method->card_type = CreditCard::detectType($payment_details['cardprefix'])->getId(); + $payment_method->card_number = substr($payment_details['cardnomask'], -4); + $payment_method->card_exp_month = substr($payment_details['cardexpdate'], 0, 2); + $payment_method->card_exp_year = substr($payment_details['cardexpdate'], -2); - $payment_method->setRemoteId($request->query->get('txn_id')); + $payment_method->setRemoteId($payment_details['txn_id']); $expires = CreditCard::calculateExpirationTimestamp($payment_method->card_exp_month->value, $payment_method->card_exp_year->value); $payment_method->setExpiresTime($expires); $payment_method->save(); @@ -155,7 +154,7 @@ class StoredOffsiteRedirect extends OffsiteRedirect implements SupportsStoredPay /** * {@inheritdoc} */ - public function onReturnOffsitePaymentMethodOnUserPage(PaymentMethodInterface $payment_method, Request $request) { + public function onReturnOffsitePaymentMethodOnUserPage(PaymentMethodInterface $payment_method, array $payment_details) { // This is the exact copy of // Drupal\commerce_payment_example\Plugin\Commerce\PaymentGateway\Onsite::createPaymentMethodOnUserPage(). $required_keys = [ @@ -164,7 +163,7 @@ class StoredOffsiteRedirect extends OffsiteRedirect implements SupportsStoredPay 'cardprefix', 'cardnomask', 'cardexpdate', ]; foreach ($required_keys as $required_key) { - if (empty($request->get($required_key))) { + if (empty($payment_details[$required_key])) { throw new \InvalidArgumentException(sprintf('$payment_details must contain the %s key.', $required_key)); } } @@ -185,15 +184,15 @@ class StoredOffsiteRedirect extends OffsiteRedirect implements SupportsStoredPay // You might need to do different API requests based on whether the // payment method is reusable: $payment_method->isReusable(). // Non-reusable payment methods usually have an expiration timestamp. - $payment_method->card_type = CreditCard::detectType($request->query->get('cardprefix'))->getId(); + $payment_method->card_type = CreditCard::detectType($payment_details['cardprefix'])->getId(); // Only the last 4 numbers are safe to store. - $payment_method->card_number = substr($request->query->get('cardnomask'), -4); - $payment_method->card_exp_month = substr($request->query->get('cardexpdate'), 0, 2); - $payment_method->card_exp_year = substr($request->query->get('cardexpdate'), -2); + $payment_method->card_number = substr($payment_details['cardnomask'], -4); + $payment_method->card_exp_month = substr($payment_details['cardexpdate'], 0, 2); + $payment_method->card_exp_year = substr($payment_details['cardexpdate'], -2); $expires = CreditCard::calculateExpirationTimestamp($payment_method->card_exp_month->value, $payment_method->card_exp_year->value); $payment_method->setExpiresTime($expires); // The remote ID returned by the request. - $payment_method->setRemoteId($request->query->get('remote_payment_method_id')); + $payment_method->setRemoteId($payment_details['remote_payment_method_id']); $payment_method->save(); $this->messenger->addMessage($this->t('A new payment method has been created.')); }