public function Stripe::createPaymentMethod in Commerce Stripe 8
Creates a payment method with the given payment details.
Parameters
\Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method: The payment method.
array $payment_details: The gateway-specific payment details provided by the payment method form for on-site gateways, or the incoming request for off-site gateways.
Throws
\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.
Overrides SupportsCreatingPaymentMethodsInterface::createPaymentMethod
File
- src/
Plugin/ Commerce/ PaymentGateway/ Stripe.php, line 389
Class
- Stripe
- Provides the Stripe payment gateway.
Namespace
Drupal\commerce_stripe\Plugin\Commerce\PaymentGatewayCode
public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$required_keys = [
// The expected keys are payment gateway specific and usually match
// the PaymentMethodAddForm form elements. They are expected to be valid.
'stripe_payment_method_id',
];
foreach ($required_keys as $required_key) {
if (empty($payment_details[$required_key])) {
throw new InvalidRequestException(sprintf('$payment_details must contain the %s key.', $required_key));
}
}
$remote_payment_method = $this
->doCreatePaymentMethod($payment_method, $payment_details);
$payment_method->card_type = $this
->mapCreditCardType($remote_payment_method['brand']);
$payment_method->card_number = $remote_payment_method['last4'];
$payment_method->card_exp_month = $remote_payment_method['exp_month'];
$payment_method->card_exp_year = $remote_payment_method['exp_year'];
$expires = CreditCard::calculateExpirationTimestamp($remote_payment_method['exp_month'], $remote_payment_method['exp_year']);
$payment_method
->setRemoteId($payment_details['stripe_payment_method_id']);
$payment_method
->setExpiresTime($expires);
$payment_method
->save();
}