protected function HostedFields::doCreatePaymentMethod in Commerce Braintree 8
Creates the payment method on the gateway.
Parameters
\Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method: The payment method.
array $payment_details: The gateway-specific payment details.
Return value
array The payment method information returned by the gateway. Notable keys:
- token: The remote ID.
Credit card specific keys:
- card_type: The card type.
- last4: The last 4 digits of the credit card number.
- expiration_month: The expiration month.
- expiration_year: The expiration year.
PayPal specific keys:
- email: The PayPal email address.
1 call to HostedFields::doCreatePaymentMethod()
- HostedFields::createPaymentMethod in src/
Plugin/ Commerce/ PaymentGateway/ HostedFields.php - Creates a payment method with the given payment details.
File
- src/
Plugin/ Commerce/ PaymentGateway/ HostedFields.php, line 407
Class
- HostedFields
- Provides the HostedFields payment gateway.
Namespace
Drupal\commerce_braintree\Plugin\Commerce\PaymentGatewayCode
protected function doCreatePaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$payment_method_type = $payment_method
->getType()
->getPluginId();
$owner = $payment_method
->getOwner();
// If the owner is anonymous, the created customer will be blank.
// https://developers.braintreepayments.com/reference/request/customer/create/php#blank-customer
$customer_id = NULL;
$customer_data = [];
if ($owner && $owner
->isAuthenticated()) {
$customer_id = $this
->getRemoteCustomerId($owner);
$customer_data['email'] = $owner
->getEmail();
}
$payment_method_data = [
'paymentMethodNonce' => $payment_details['payment_method_nonce'],
'options' => [
'verifyCard' => TRUE,
],
];
$billing_address_data = [];
if ($billing_profile = $payment_method
->getBillingProfile()) {
/** @var \Drupal\address\AddressInterface $address */
$address = $billing_profile
->get('address')
->first();
$billing_address_data = [
'billingAddress' => [
'firstName' => $address
->getGivenName(),
'lastName' => $address
->getFamilyName(),
'company' => $address
->getOrganization(),
'streetAddress' => $address
->getAddressLine1(),
'extendedAddress' => $address
->getAddressLine2(),
'locality' => $address
->getLocality(),
'region' => $address
->getAdministrativeArea(),
'postalCode' => $address
->getPostalCode(),
'countryCodeAlpha2' => $address
->getCountryCode(),
],
];
$payment_method_data['cardholderName'] = $address
->getGivenName() . ' ' . $address
->getFamilyName();
}
if ($customer_id) {
// Create a payment method for an existing customer.
try {
$data = $billing_address_data + $payment_method_data + [
'customerId' => $customer_id,
];
$result = $this->api
->paymentMethod()
->create($data);
ErrorHelper::handleErrors($result);
} catch (BraintreeException $e) {
ErrorHelper::handleException($e);
}
$remote_payment_method = $result->paymentMethod;
}
else {
// Create both the customer and the payment method.
try {
$data = $customer_data + [
'creditCard' => $billing_address_data + $payment_method_data,
];
$result = $this->api
->customer()
->create($data);
ErrorHelper::handleErrors($result);
} catch (BraintreeException $e) {
ErrorHelper::handleException($e);
}
$remote_payment_method = $result->customer->paymentMethods[0];
if ($owner && $owner
->isAuthenticated()) {
$this
->setRemoteCustomerId($owner, $result->customer->id);
$owner
->save();
}
}
if (in_array($payment_method_type, [
'paypal',
'paypal_credit',
])) {
return [
'token' => $remote_payment_method->token,
'email' => $remote_payment_method->email,
];
}
else {
return [
'token' => $remote_payment_method->token,
'card_type' => $remote_payment_method->cardType,
'last4' => $remote_payment_method->last4,
'expiration_month' => $remote_payment_method->expirationMonth,
'expiration_year' => $remote_payment_method->expirationYear,
];
}
}