You are here

class ExpressCheckoutRequestSubscriber in Commerce PayPal 8

Alters the Express checkout request before its sent to PayPal.

Hierarchy

Expanded class hierarchy of ExpressCheckoutRequestSubscriber

1 string reference to 'ExpressCheckoutRequestSubscriber'
commerce_paypal_test.services.yml in tests/modules/commerce_paypal_test/commerce_paypal_test.services.yml
tests/modules/commerce_paypal_test/commerce_paypal_test.services.yml
1 service uses ExpressCheckoutRequestSubscriber
commerce_paypal_test.express_checkout_request_subscriber in tests/modules/commerce_paypal_test/commerce_paypal_test.services.yml
Drupal\commerce_paypal_test\EventSubscriber\ExpressCheckoutRequestSubscriber

File

tests/modules/commerce_paypal_test/src/EventSubscriber/ExpressCheckoutRequestSubscriber.php, line 11

Namespace

Drupal\commerce_paypal_test\EventSubscriber
View source
class ExpressCheckoutRequestSubscriber implements EventSubscriberInterface {

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events = [
      'commerce_paypal.express_checkout_request' => [
        'setNvpData',
        -100,
      ],
    ];
    return $events;
  }

  /**
   * Alter the NVP data array before its sent to PayPal.
   *
   * @param \Drupal\commerce_paypal\Event\ExpressCheckoutRequestEvent $event
   *   The Express checkout request event.
   */
  public function setNvpData(ExpressCheckoutRequestEvent $event) {
    $nvp_data = $event
      ->getNvpData();

    // Send the billing address information.
    if ($nvp_data['METHOD'] === 'SetExpressCheckout') {

      /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
      $order = $event
        ->getOrder();

      // Check if the billing profile is empty.
      if (!$order
        ->getBillingProfile()) {
        return;
      }
      $address = $order
        ->getBillingProfile()->address
        ->first();
      $name = $address
        ->getGivenName() . ' ' . $address
        ->getFamilyName();
      $billing_info = [
        'PAYMENTREQUEST_0_SHIPTONAME' => substr($name, 0, 32),
        'PAYMENTREQUEST_0_SHIPTOSTREET' => substr($address
          ->getAddressLine1(), 0, 100),
        'PAYMENTREQUEST_0_SHIPTOSTREET2' => substr($address
          ->getAddressLine2(), 0, 100),
        'PAYMENTREQUEST_0_SHIPTOCITY' => substr($address
          ->getLocality(), 0, 40),
        'PAYMENTREQUEST_0_SHIPTOSTATE' => substr($address
          ->getAdministrativeArea(), 0, 40),
        'PAYMENTREQUEST_0_SHIPTOCOUNTRYCODE' => $address
          ->getCountryCode(),
        'PAYMENTREQUEST_0_SHIPTOZIP' => substr($address
          ->getPostalCode(), 0, 20),
      ];

      // Filter out empty values.
      $nvp_data = array_merge($nvp_data, array_filter($billing_info));
      $event
        ->setNvpData($nvp_data);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExpressCheckoutRequestSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
ExpressCheckoutRequestSubscriber::setNvpData public function Alter the NVP data array before its sent to PayPal.