public function CustomCardFieldsBuilder::build in Commerce PayPal 8
Build the PayPal Checkout card form.
Parameters
\Drupal\commerce_order\Entity\OrderInterface $order: The order.
\Drupal\commerce_payment\Entity\PaymentGatewayInterface $payment_gateway: The payment gateway.
Return value
array A renderable array representing the card form.
Overrides CustomCardFieldsBuilderInterface::build
File
- src/
CustomCardFieldsBuilder.php, line 48
Class
- CustomCardFieldsBuilder
- Provides a helper for building the PayPal custom card fields form.
Namespace
Drupal\commerce_paypalCode
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;
}