Skip to content
Snippets Groups Projects

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

1 file
+ 42
17
Compare changes
  • Side-by-side
  • Inline
@@ -17,6 +17,7 @@ use Drupal\Component\Datetime\TimeInterface;
@@ -17,6 +17,7 @@ use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\Core\Url;
 
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Request;
@@ -185,11 +186,13 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
@@ -185,11 +186,13 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
];
];
$owner = $payment_method->getOwner();
$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
// Add remote customer ID if it's available; this is required for
// stored cards.
// stored cards.
// @see doCreatePaymentMethod()
// @see doCreatePaymentMethod()
$transaction_data['customer'] = $this->getRemoteCustomerId($owner);
$transaction_data['customer'] = $remoteCustomer['id'];
}
}
if (!empty($this->configuration['3d_secure'])) {
if (!empty($this->configuration['3d_secure'])) {
@@ -370,12 +373,8 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
@@ -370,12 +373,8 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
// Delete the remote record.
// Delete the remote record.
try {
try {
$owner = $payment_method->getOwner();
$owner = $payment_method->getOwner();
if ($owner && $remote_id = $this->getRemoteCustomerId($owner)) {
if ($owner
$customer = \OmiseCustomer::retrieve(
&& $customer = $this->getRemoteOmiseCustomer($owner)) {
$remote_id,
$this->configuration['public_key'],
$this->configuration['secret_key']
);
$customer->cards()->retrieve($payment_method->getRemoteId())->destroy();
$customer->cards()->retrieve($payment_method->getRemoteId())->destroy();
}
}
}
}
@@ -458,22 +457,17 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
@@ -458,22 +457,17 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
protected function doCreatePaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
protected function doCreatePaymentMethod(PaymentMethodInterface $payment_method, array $payment_details) {
$owner = $payment_method->getOwner();
$owner = $payment_method->getOwner();
$customer_id = NULL;
$customer = NULL;
if ($owner && $owner->isAuthenticated()) {
if ($owner && $owner->isAuthenticated()) {
$customer_id = $this->getRemoteCustomerId($owner);
$customer = $this->getRemoteOmiseCustomer($owner);
}
}
if ($customer_id) {
if ($customer) {
// If the customer id already exists
// If the customer already exists
// use the Omise form token to create the new card.
// use the Omise form token to create the new card.
// Retrieve card ID by token. It will be later used to identify the newly
// Retrieve card ID by token. It will be later used to identify the newly
// created card in the customer's cards list.
// created card in the customer's cards list.
$cardByToken = $this->retrieveCardIdByToken($payment_details['omise_token']);
$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.
// Create a payment method for an existing customer.
$customer->update(['card' => $payment_details['omise_token']]);
$customer->update(['card' => $payment_details['omise_token']]);
@@ -612,4 +606,35 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
@@ -612,4 +606,35 @@ class Omise extends OnsitePaymentGatewayBase implements OmiseInterface {
return $cardByToken['card']['id'];
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;
 
}
 
}
 
}
}
Loading