Skip to content
Snippets Groups Projects
Commit a53fb5d8 authored by Vadym Abramchuk's avatar Vadym Abramchuk
Browse files

Issue #3271932: Payment processing error after changing the API key

parent c56519ad
Branches
Tags
1 merge request!7Issue #3271932: Payment processing error after changing the API key
......@@ -17,6 +17,7 @@ use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
......@@ -185,11 +186,13 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
];
$owner = $payment_method->getOwner();
if ($owner && $owner->isAuthenticated()) {
if ($owner
&& $owner->isAuthenticated()
&& $remoteCustomer = $this->getRemoteOmiseCustomer($owner)) {
// Add remote customer ID if it's available; this is required for
// stored cards.
// @see doCreatePaymentMethod()
$transaction_data['customer'] = $this->getRemoteCustomerId($owner);
$transaction_data['customer'] = $remoteCustomer['id'];
}
if (!empty($this->configuration['3d_secure'])) {
......@@ -370,12 +373,8 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
// Delete the remote record.
try {
$owner = $payment_method->getOwner();
if ($owner && $remote_id = $this->getRemoteCustomerId($owner)) {
$customer = \OmiseCustomer::retrieve(
$remote_id,
$this->configuration['public_key'],
$this->configuration['secret_key']
);
if ($owner
&& $customer = $this->getRemoteOmiseCustomer($owner)) {
$customer->cards()->retrieve($payment_method->getRemoteId())->destroy();
}
}
......@@ -458,22 +457,17 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
protected function doCreatePaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$owner = $payment_method->getOwner();
$customer_id = NULL;
$customer = NULL;
if ($owner && $owner->isAuthenticated()) {
$customer_id = $this->getRemoteCustomerId($owner);
$customer = $this->getRemoteOmiseCustomer($owner);
}
if ($customer_id) {
// If the customer id already exists
if ($customer) {
// If the customer already exists
// use the Omise form token to create the new card.
// Retrieve card ID by token. It will be later used to identify the newly
// created card in the customer's cards list.
$cardByToken = $this->retrieveCardIdByToken($payment_details['omise_token']);
$customer = \OmiseCustomer::retrieve(
$customer_id,
$this->configuration['public_key'],
$this->configuration['secret_key']
);
// Create a payment method for an existing customer.
$customer->update(['card' => $payment_details['omise_token']]);
......@@ -612,4 +606,35 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
return $cardByToken['card']['id'];
}
/**
* Retrieves remote Omise customer object.
*
* @param \Drupal\user\UserInterface $account
* The user account.
*
* @return \OmiseCustomer|false
* Omise customer object or FALSE if not found.
*/
protected function getRemoteOmiseCustomer(UserInterface $account) {
$customerId = $this->getRemoteCustomerId($account);
if (!$customerId) {
return FALSE;
}
try {
return \OmiseCustomer::retrieve(
$customerId,
$this->configuration['public_key'],
$this->configuration['secret_key']
);
}
catch (\OmiseNotFoundException $e) {
if (!$account->isAnonymous()) {
$this->setRemoteCustomerId($account, NULL);
$account->save();
}
return FALSE;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment