Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2932704: Allow new PayFlow payment methods to be altered before save #8

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/Event/PayPalEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
75 changes: 75 additions & 0 deletions src/Event/PostCreatePayFlowPaymentMethodEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Drupal\commerce_paypal\Event;

use Drupal\commerce_payment\Entity\PaymentMethodInterface;
use Symfony\Component\EventDispatcher\Event;

/**
* Defines the 'Post-create PayFlow payment method' event.
*
* @see \Drupal\commerce_paypal\Event\PaypalEvents
*/
class PostCreatePayFlowPaymentMethodEvent extends Event {

/**
* The payment details.
*
* @var array
*/
protected $paymentDetails;

/**
* The payment method.
*
* @var \Drupal\commerce_payment\Entity\PaymentMethodInterface
*/
protected $paymentMethod;

/**
* Constructs a new PostCreatePayFlowPaymentMethodEvent object.
*
* @param \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method
* The payment method.
* @param array $payment_details
* The payment details.
*/
public function __construct(PaymentMethodInterface $payment_method, array $payment_details) {
$this->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;
}

}
25 changes: 21 additions & 4 deletions src/Plugin/Commerce/PaymentGateway/Payflow.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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')
);
}

Expand Down Expand Up @@ -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");
Expand Down