diff --git a/src/Event/PayPalEvents.php b/src/Event/PayPalEvents.php index 6afab03..5256a89 100644 --- a/src/Event/PayPalEvents.php +++ b/src/Event/PayPalEvents.php @@ -7,6 +7,17 @@ */ final class PayPalEvents { + /** + * Name of the event fired after creating a new PayFlow payment method. + * + * This event is fired before the payment method is saved. + * + * @Event + * + * @see \Drupal\commerce_paypal\Event\PostCreatePayFlowPaymentMethodEvent.php + */ + const POST_CREATE_PAYMENT_METHOD_PAYFLOW = 'commerce_paypal.post_create_payment_method_payflow'; + /** * Name of the event fired when performing the Express Checkout requests. * diff --git a/src/Event/PostCreatePayFlowPaymentMethodEvent.php b/src/Event/PostCreatePayFlowPaymentMethodEvent.php new file mode 100644 index 0000000..8e60b82 --- /dev/null +++ b/src/Event/PostCreatePayFlowPaymentMethodEvent.php @@ -0,0 +1,75 @@ +paymentDetails = $payment_details; + $this->paymentMethod = $payment_method; + } + + /** + * Gets the payment details + * + * @return array + * The payment details + */ + public function getPaymentDetails() { + return $this->paymentDetails; + } + + /** + * Gets the payment method. + * + * @return \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method + * The payment method. + */ + public function getPaymentMethod() { + return $this->paymentMethod; + } + + /** + * Sets the payment method. + * + * @param \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method + * The payment method. + * + * @return $this + */ + public function setPaymentMethod(PaymentMethodInterface $payment_method) { + $this->paymentMethod = $payment_method; + return $this; + } + +} diff --git a/src/Plugin/Commerce/PaymentGateway/Payflow.php b/src/Plugin/Commerce/PaymentGateway/Payflow.php index e823e0d..e2ec74d 100644 --- a/src/Plugin/Commerce/PaymentGateway/Payflow.php +++ b/src/Plugin/Commerce/PaymentGateway/Payflow.php @@ -11,6 +11,8 @@ use Drupal\commerce_payment\PaymentMethodTypeManager; use Drupal\commerce_payment\PaymentTypeManager; use Drupal\commerce_payment\Plugin\Commerce\PaymentGateway\OnsitePaymentGatewayBase; +use Drupal\commerce_paypal\Event\PostCreatePayFlowPaymentMethodEvent; +use Drupal\commerce_paypal\Event\PayPalEvents; use Drupal\commerce_price\Price; use Drupal\commerce_price\RounderInterface; use Drupal\Component\Datetime\TimeInterface; @@ -20,6 +22,7 @@ use GuzzleHttp\Exception\RequestException; use InvalidArgumentException; use Symfony\Component\DependencyInjection\ContainerInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; /** * Provides the PayPal Payflow payment gateway. @@ -60,14 +63,22 @@ class Payflow extends OnsitePaymentGatewayBase implements PayflowInterface { */ protected $rounder; + /** + * The event dispatcher. + * + * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface + */ + protected $eventDispatcher; + /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PaymentTypeManager $payment_type_manager, PaymentMethodTypeManager $payment_method_type_manager, TimeInterface $time, ClientInterface $client, RounderInterface $rounder) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, PaymentTypeManager $payment_type_manager, PaymentMethodTypeManager $payment_method_type_manager, TimeInterface $time, ClientInterface $client, RounderInterface $rounder, EventDispatcherInterface $event_dispatcher) { parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $payment_type_manager, $payment_method_type_manager, $time); $this->httpClient = $client; $this->rounder = $rounder; + $this->eventDispatcher = $event_dispatcher; } /** @@ -83,7 +94,8 @@ public static function create(ContainerInterface $container, array $configuratio $container->get('plugin.manager.commerce_payment_method_type'), $container->get('datetime.time'), $container->get('http_client'), - $container->get('commerce_price.rounder') + $container->get('commerce_price.rounder'), + $container->get('event_dispatcher') ); } @@ -499,8 +511,13 @@ public function createPaymentMethod(PaymentMethodInterface $payment_method, arra // Store the remote ID returned by the request. $payment_method ->setRemoteId($data['pnref']) - ->setExpiresTime($expires) - ->save(); + ->setExpiresTime($expires); + + // Allow the new payment method to be altered before being saved. + $event = new PostCreatePayFlowPaymentMethodEvent($payment_method, $payment_details); + $this->eventDispatcher->dispatch(PayPalEvents::POST_CREATE_PAYMENT_METHOD_PAYFLOW, $event); + + $event->getPaymentMethod()->save(); } catch (RequestException $e) { throw new HardDeclineException("Unable to store the credit card");