Commit ad5d3dec authored by Ryan Szrama's avatar Ryan Szrama Committed by Ryan Szrama
Browse files

Issue #3256176 by rszrama: Add Venmo support to PayPal Checkout

parent 08d5ab65
Loading
Loading
Loading
Loading
+45 −0
Original line number Diff line number Diff line
@@ -273,3 +273,48 @@ function commerce_paypal_form_commerce_payment_method_add_form_alter(&$form, For
  // The current PayPal checkout implementation doesn't support tokenization.
  unset($form["payment_method"]["#options"]["new--paypal_checkout--paypal"]);
}

/**
 * Returns the label to use for a PayPal funding source.
 *
 * @param string $funding_source
 *   The machine-name of the funding source returned by PayPal.
 *
 * @return string
 *   The label to show a customer for the funding source.
 */
function commerce_paypal_funding_source_label($funding_source) {
  $funding_sources = [
    'card' => t('Credit or debit card'),
    'credit' => t('PayPal Credit'),
    'paylater' => t('Pay Later'),
    'paypal' => t('PayPal'),
    'venmo' => t('Venmo'),
  ];

  return $funding_sources[$funding_source] ?? '';
}

/**
 * Implements hook_preprocess_HOOK().
 *
 * To facilitate the display of the funding source in order templates, this
 * function looks for an order with a PayPal Checkout funding source set and
 * adds its label to the array of available template variables.
 *
 * @see https://developer.paypal.com/docs/checkout/standard/customize/display-funding-source/
 */
function commerce_paypal_preprocess_commerce_order(&$variables) {
  if (!empty($variables['elements']['#commerce_order'])) {
    /** @var Drupal\commerce_order\Entity\OrderInterface $order */
    $order = $variables['elements']['#commerce_order'];

    // Check for a PayPal Checkout funding source.
    $data = $order->getData('commerce_paypal_checkout', []);

    // If we found a funding source, add its label to the template variables.
    if (!empty($data['funding_source'])) {
      $variables['order']['funding_source'] = commerce_paypal_funding_source_label($data['funding_source']);
    }
  }
}
+6 −0
Original line number Diff line number Diff line
@@ -53,6 +53,12 @@
            }
          });
        },
        onClick: function (data) {
          // Set the fundingSource in the cookie for retrieval post-checkout.
          if (data.hasOwnProperty('fundingSource')) {
            document.cookie = 'lastFundingSource = ' + data.fundingSource + ';path=/';
          }
        },
        style: settings['style']
      }).render('#' + settings['elementId']);
    },
+7 −3
Original line number Diff line number Diff line
@@ -195,12 +195,13 @@ class Checkout extends OffsitePaymentGatewayBase implements CheckoutInterface {
      '#description' => $this->t('The disabled funding sources for the transaction. Any funding sources passed do not display with Smart Payment Buttons. By default, funding source eligibility is smartly decided based on a variety of factors.'),
      '#type' => 'checkboxes',
      '#options' => [
        'card' => $this->t('Credit or Debit Cards'),
        'card' => $this->t('Credit or debit cards'),
        'giropay' => $this->t('Giropay'),
        'mybank' => $this->t('MyBank'),
        'credit' => $this->t('PayPal Credit'),
        'sepa' => $this->t('SEPA-Lastschrift'),
        'sofort' => $this->t('Sofort'),
        'mybank' => $this->t('MyBank'),
        'giropay' => $this->t('Giropay'),
        'venmo' => $this->t('Venmo'),
      ],
      '#default_value' => $this->configuration['disable_funding'],
      '#states' => $spb_states,
@@ -780,6 +781,9 @@ class Checkout extends OffsitePaymentGatewayBase implements CheckoutInterface {
      'remote_id' => $paypal_order['id'],
      'flow' => $flow,
      'intent' => strtolower($paypal_order['intent']),
      // It's safe to assume the last funding source set in the cookie was for
      // this order and note it in the data array for later use.
      'funding_source' => $request->cookies->get('lastFundingSource', NULL),
    ]);

    if (empty($order->getEmail())) {
+5 −0
Original line number Diff line number Diff line
@@ -68,6 +68,11 @@ class SmartPaymentButtonsBuilder implements SmartPaymentButtonsBuilderInterface
    if ($this->configFactory->get('commerce_paypal.credit_messaging_settings')->get('client_id')) {
      $options['query']['components'] = 'buttons,messages';
    }
    // Enable Venmo funding if it is not disabled.
    if (($key = array_search('venmo', $config['disable_funding'])) === FALSE) {
      $options['query']['enable-funding'] = 'venmo';
      unset($config['disable_funding'][$key]);
    }
    if (!empty($config['disable_funding'])) {
      $options['query']['disable-funding'] = implode(',', $config['disable_funding']);
    }