Skip to content

Instantly share code, notes, and snippets.

@steveoliver
Created September 29, 2016 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveoliver/fa106438ffed57eae1c5c386df380184 to your computer and use it in GitHub Desktop.
Save steveoliver/fa106438ffed57eae1c5c386df380184 to your computer and use it in GitHub Desktop.
Drupal Commerce 2.x Onsite payment gateway token register method
<?php
// Drupal Commerce 2.x Onsite payment method
// ...
/**
* {@inheritdoc}
*/
public function createPaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$required_keys = [
'response$type', 'response$paypageRegistrationId', 'expiration',
];
foreach ($required_keys as $required_key) {
if (empty($payment_details[$required_key])) {
throw new \InvalidArgumentException(sprintf('$payment_details must contain the %s key.', $required_key));
}
}
$expires = CreditCard::calculateExpirationTimestamp($payment_details['expiration']['month'], $payment_details['expiration']['year']);
$payment_method->card_type = Helper::getCommerceCreditCardType($payment_details['response$type']);
$payment_method->card_number = $payment_details['response$lastFour'];
$payment_method->card_exp_month = $payment_details['expiration']['month'];
$payment_method->card_exp_year = $payment_details['expiration']['year'];
$payment_method->setRemoteId($payment_details['response$paypageRegistrationId']);
$payment_method->setExpiresTime($expires);
$payment_method->save();
$this->registerToken($payment_method);
}
/**
* Registers a token with Vantiv from the AJAX provided registration id.
*
* @param \Drupal\commerce_payment\Entity\PaymentMethodInterface $payment_method
* The payment method.
*/
private function registerToken(PaymentMethodInterface $payment_method) {
$hash_in = Helper::getLitleHashIn($this->configuration);
/** @var ProfileInterface $billing_profile */
$billing_profile = $payment_method->getBillingProfile();
$request_data = [
'id' => $payment_method->getOriginalId(),
'customerId' => $billing_profile->getOwnerId(),
'paypageRegistrationId' => $payment_method->getRemoteId()
];
$request = new LitleOnlineRequest();
$response = $request->registerTokenRequest($hash_in + $request_data);
$response_array = Helper::getResponseArray($response, 'registerTokenResponse');
if (!Helper::isResponseSuccess($response_array['response'])) {
throw new SoftDeclineException('An error occurred registering a token for your payment method.');
}
$payment_method->setRemoteId($response_array['litleToken']);
$payment_method->save();
}
// ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment