I was testing latest code from https://git.drupalcode.org/project/commerce_braintree/-/merge_requests/4...
The patch #9 itself is working because it includes css/commerce_braintree.apple_pay.css file which styles #braintree-apple-pay-button element as an apple pay button.
The latest code in the merge request does not have this css file and it also doesn't have the #suffix part of the following code which I found in earlier commits.
$element['apple_pay'] = [
'#type' => 'container',
'#id' => 'braintree-apple-pay-button',
'#suffix' => Markup::create('<apple-pay-button buttonstyle="black" type="plain" locale="' . $lang . '"></apple-pay-button>'),
];
Apple supports both css style and markup approach. But the latest code in the MR does not use either.
There is another issue I've noticed. Code below hardcodes merchantCapabilities and supportedNetworks. Braintree docs recommend leaving it to Braintree to figure this out. Not all merchants support this exact list of networks (visa, mastercard, amex, discover)
paymentButton.addEventListener('click', function (event) {
var paymentRequest = applePayInstance.createPaymentRequest({
countryCode: settings.countryCode,
currencyCode: settings.currencyCode,
merchantCapabilities: ['supports3DS', 'supportsEMV', 'supportsCredit', 'supportsDebit'],
supportedNetworks: [
"visa",
"masterCard",
"amex",
"discover"
],
total: {
label: 'Order' + settings.orderId,
type: 'final',
amount: settings.totalPrice
}
})
Last commit removed some rows from PaymentMethodAddForm.php without which Apple Pay button does not even display.
Attaching patch
Same issue as in comment #9.
$product_id is empty on Manage layout page of a product type (admin/commerce/config/product-types/default/edit/display/full)
Adding a check if $product_id is empty resolves the issue
public function ajaxAddToCartForm($product_id, $view_mode, $combine) {
/** @var \Drupal\commerce_order\OrderItemStorageInterface $order_item_storage */
$order_item_storage = $this->entityTypeManager->getStorage('commerce_order_item');
if (empty($product_id)) {
return [];
}
/** @var \Drupal\commerce_product\Entity\ProductInterface $product */
$product = $this->entityTypeManager->getStorage('commerce_product')->load($product_id);
$default_variation = $product->getDefaultVariation();
if (!$default_variation) {
return [];
}
$order_item = $order_item_storage->createFromPurchasableEntity($default_variation);
/** @var \Drupal\commerce_cart\Form\AddToCartFormInterface $form_object */
$form_object = $this->entityTypeManager->getFormObject('commerce_order_item', 'dc_ajax_add_cart');
$form_object->setEntity($order_item);
// The default form ID is based on the variation ID, but in this case the
// product ID is more reliable (the default variation might change between
// requests due to an availability change, for example).
$form_object->setFormId($form_object->getBaseFormId() . '_commerce_product_' . $product_id);
$form_state = (new FormState())->setFormState([
'product' => $product,
'view_mode' => $view_mode,
'settings' => [
'combine' => $combine,
],
]);
return $this->formBuilder->buildForm($form_object, $form_state);
}