Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
commerce_paypal
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
commerce_paypal
Merge requests
!33
Resolve
#3420132
"Use payment gateway"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve
#3420132
"Use payment gateway"
issue/commerce_paypal-3420132:3420132-use-payment-gateway
into
8.x-1.x
Overview
0
Commits
2
Pipelines
1
Changes
4
Merged
Jonathan Sacksick
requested to merge
issue/commerce_paypal-3420132:3420132-use-payment-gateway
into
8.x-1.x
5 months ago
Overview
0
Commits
2
Pipelines
1
Changes
4
Expand
Closes
#3420132
0
0
Merge request reports
Compare
8.x-1.x
8.x-1.x (base)
and
latest version
latest version
34a39704
2 commits,
5 months ago
4 files
+
84
−
2
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
src/EventSubscriber/PaymentOptionsSubscriber.php
0 → 100644
+
76
−
0
Options
<?php
namespace
Drupal\commerce_paypal\EventSubscriber
;
use
Drupal\commerce_payment
\Entity\PaymentMethodInterface
;
use
Drupal\commerce_payment
\Event\FilterPaymentOptionsEvent
;
use
Drupal\commerce_payment
\Event\PaymentEvents
;
use
Drupal\commerce_payment
\PaymentOption
;
use
Drupal\commerce_paypal
\Plugin\Commerce\PaymentGateway\CheckoutInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
/**
* Defines an event subscriber altering PayPal checkout option labels.
*/
class
PaymentOptionsSubscriber
implements
EventSubscriberInterface
{
/**
* Constructs a new PaymentOptionsSubscriber object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public
function
__construct
(
protected
EntityTypeManagerInterface
$entityTypeManager
)
{}
/**
* {@inheritdoc}
*/
public
static
function
getSubscribedEvents
():
array
{
return
[
PaymentEvents
::
FILTER_PAYMENT_OPTIONS
=>
[
'onFilterPaymentOptions'
,
-
100
],
];
}
/**
* Alters the PayPal Checkout option labels.
*
* PayPal Checkout allows customizing the payment gateway display name but
* because of the way it is currently implemented, the display label isn't
* used. This code was written to address that.
*
* @param \Drupal\commerce_payment\Event\FilterPaymentOptionsEvent $event
* The filter payment options event.
*/
public
function
onFilterPaymentOptions
(
FilterPaymentOptionsEvent
$event
):
void
{
$options
=
$event
->
getPaymentOptions
();
/** @var \Drupal\commerce_payment\PaymentGatewayStorageInterface $payment_gateway_storage */
$payment_gateway_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_gateway'
);
/** @var \Drupal\commerce_payment\PaymentMethodStorageInterface $payment_method_storage */
$payment_method_storage
=
$this
->
entityTypeManager
->
getStorage
(
'commerce_payment_method'
);
foreach
(
$options
as
$key
=>
$option
)
{
/** @var \Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway */
$payment_gateway
=
$payment_gateway_storage
->
load
(
$option
->
getPaymentGatewayId
());
$payment_gateway_plugin
=
$payment_gateway
?->
getPlugin
();
if
(
!
$payment_gateway_plugin
instanceof
CheckoutInterface
)
{
continue
;
}
// In case the option references an existing payment method, ensure
// skip retitling the payment option if a remote ID is set as we'll
// be using the card type and the last digits of the card as the option
// label.
if
(
!
empty
(
$option
->
getPaymentMethodId
()))
{
$payment_method
=
$payment_method_storage
->
load
(
$option
->
getPaymentMethodId
());
if
(
$payment_method
instanceof
PaymentMethodInterface
&&
!
empty
(
$payment_method
->
getRemoteId
()))
{
continue
;
}
}
$retitled_option
=
new
PaymentOption
([
'label'
=>
$payment_gateway_plugin
->
getDisplayLabel
(),
]
+
$option
->
toArray
());
$options
[
$key
]
=
$retitled_option
;
}
$event
->
setPaymentOptions
(
$options
);
}
}
Loading