You are here

class CustomCardFieldsBuilder in Commerce PayPal 8

Provides a helper for building the PayPal custom card fields form.

Hierarchy

Expanded class hierarchy of CustomCardFieldsBuilder

1 string reference to 'CustomCardFieldsBuilder'
commerce_paypal.services.yml in ./commerce_paypal.services.yml
commerce_paypal.services.yml
1 service uses CustomCardFieldsBuilder
commerce_paypal.custom_card_fields_builder in ./commerce_paypal.services.yml
Drupal\commerce_paypal\CustomCardFieldsBuilder

File

src/CustomCardFieldsBuilder.php, line 16

Namespace

Drupal\commerce_paypal
View source
class CustomCardFieldsBuilder implements CustomCardFieldsBuilderInterface {

  /**
   * The PayPal Checkout SDK factory.
   *
   * @var \Drupal\commerce_paypal\CheckoutSdkFactoryInterface
   */
  protected $checkoutSdkFactory;

  /**
   * The logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Constructs a new CustomCardFieldsBuilder object.
   *
   * @param \Drupal\commerce_paypal\CheckoutSdkFactoryInterface $checkout_sdk_factory
   *   The PayPal Checkout SDK factory.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger channel.
   */
  public function __construct(CheckoutSdkFactoryInterface $checkout_sdk_factory, LoggerInterface $logger) {
    $this->checkoutSdkFactory = $checkout_sdk_factory;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public function build(OrderInterface $order, PaymentGatewayInterface $payment_gateway) {
    $element = [];
    if (!$payment_gateway
      ->getPlugin() instanceof CheckoutInterface) {
      return $element;
    }
    $config = $payment_gateway
      ->getPlugin()
      ->getConfiguration();
    $sdk = $this->checkoutSdkFactory
      ->get($config);
    try {
      $response = $sdk
        ->getClientToken();
      $body = Json::decode($response
        ->getBody()
        ->getContents());
      $client_token = $body['client_token'];
    } catch (ClientException $exception) {
      $this->logger
        ->error($exception
        ->getMessage());
      return $element;
    }
    $create_url = Url::fromRoute('commerce_paypal.checkout.create', [
      'commerce_payment_gateway' => $payment_gateway
        ->id(),
      'commerce_order' => $order
        ->id(),
    ]);
    $options = [
      'query' => [
        'components' => 'hosted-fields',
        'client-id' => $config['client_id'],
        'intent' => $config['intent'],
        'currency' => $order
          ->getTotalPrice()
          ->getCurrencyCode(),
      ],
    ];
    $element['#attached']['library'][] = 'commerce_paypal/paypal_checkout_custom_card_fields';
    $element['#attached']['drupalSettings']['paypalCheckout'] = [
      'src' => Url::fromUri('https://www.paypal.com/sdk/js', $options)
        ->toString(),
      'onCreateUrl' => $create_url
        ->toString(),
      'clientToken' => $client_token,
      'cardFieldsSelector' => '#commerce-paypal-checkout-custom-card-fields',
    ];
    $element += [
      'card_fields_form' => [
        '#theme' => 'commerce_paypal_checkout_custom_card_fields',
        '#weight' => 0,
        '#intent' => $config['intent'],
      ],
      'paypal_remote_id' => [
        '#type' => 'hidden',
      ],
    ];
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CustomCardFieldsBuilder::$checkoutSdkFactory protected property The PayPal Checkout SDK factory.
CustomCardFieldsBuilder::$logger protected property The logger.
CustomCardFieldsBuilder::build public function Build the PayPal Checkout card form. Overrides CustomCardFieldsBuilderInterface::build
CustomCardFieldsBuilder::__construct public function Constructs a new CustomCardFieldsBuilder object.