You are here

class SmartPaymentButtonsBuilder in Commerce PayPal 8

Provides a helper for building the Smart payment buttons.

Hierarchy

Expanded class hierarchy of SmartPaymentButtonsBuilder

1 string reference to 'SmartPaymentButtonsBuilder'
commerce_paypal.services.yml in ./commerce_paypal.services.yml
commerce_paypal.services.yml
1 service uses SmartPaymentButtonsBuilder
commerce_paypal.smart_payment_buttons_builder in ./commerce_paypal.services.yml
Drupal\commerce_paypal\SmartPaymentButtonsBuilder

File

src/SmartPaymentButtonsBuilder.php, line 15

Namespace

Drupal\commerce_paypal
View source
class SmartPaymentButtonsBuilder implements SmartPaymentButtonsBuilderInterface {

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a new SmartPaymentButtonsBuilder object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function build(OrderInterface $order, PaymentGatewayInterface $payment_gateway, $commit) {
    $element = [];
    if (!$payment_gateway
      ->getPlugin() instanceof CheckoutInterface) {
      return $element;
    }
    $config = $payment_gateway
      ->getPlugin()
      ->getConfiguration();
    $create_url = Url::fromRoute('commerce_paypal.checkout.create', [
      'commerce_order' => $order
        ->id(),
      'commerce_payment_gateway' => $payment_gateway
        ->id(),
    ]);

    // Note that we're not making use of the payment return route since it
    // cannot be called from the cart page because of the checkout step
    // validation.
    $return_url_options = [
      'query' => [
        'skip_payment_creation' => 1,
      ],
    ];
    $return_url = Url::fromRoute('commerce_paypal.checkout.approve', [
      'commerce_order' => $order
        ->id(),
      'commerce_payment_gateway' => $payment_gateway
        ->id(),
    ], $return_url_options);
    $options = [
      'query' => [
        'client-id' => $config['client_id'],
        'intent' => $config['intent'],
        'commit' => $commit ? 'true' : 'false',
        'currency' => $order
          ->getTotalPrice()
          ->getCurrencyCode(),
      ],
    ];

    // Include the "messages" component, only if credit messaging is configured.
    if ($this->configFactory
      ->get('commerce_paypal.credit_messaging_settings')
      ->get('client_id')) {
      $options['query']['components'] = 'buttons,messages';
    }
    if (!empty($config['disable_funding'])) {
      $options['query']['disable-funding'] = implode(',', $config['disable_funding']);
    }
    if (!empty($config['disable_card'])) {
      $options['query']['disable-card'] = implode(',', $config['disable_card']);
    }
    $element['#attached']['library'][] = 'commerce_paypal/paypal_checkout';
    $element_id = Html::getUniqueId('paypal-buttons-container');
    $element['#attached']['drupalSettings']['paypalCheckout'][$order
      ->id()] = [
      'src' => Url::fromUri('https://www.paypal.com/sdk/js', $options)
        ->toString(),
      'elementId' => $element_id,
      'onCreateUrl' => $create_url
        ->toString(),
      'onApproveUrl' => $return_url
        ->toString(),
      'flow' => $commit ? 'mark' : 'shortcut',
      'style' => $config['style'],
    ];
    $element += [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#weight' => 100,
      '#attributes' => [
        'class' => [
          'paypal-buttons-container',
        ],
        'id' => $element_id,
      ],
    ];
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SmartPaymentButtonsBuilder::$configFactory protected property The config factory.
SmartPaymentButtonsBuilder::build public function Builds the Smart payment buttons. Overrides SmartPaymentButtonsBuilderInterface::build
SmartPaymentButtonsBuilder::__construct public function Constructs a new SmartPaymentButtonsBuilder object.