From 5cd2604630a42286692e16fe64a9123f5c0d2e94 Mon Sep 17 00:00:00 2001 From: Vincent Boulanger Date: Wed, 13 Dec 2023 12:52:49 +0100 Subject: [PATCH] FIX and Improvement : - Fix : Add validity check for Saved Stripe customerId before usage - Update : 24h time format for transaction logs - Update : add missing error/exception logging --- Application/Helper/User.php | 26 ++++++++++++++++++- Application/Model/Request/Card.php | 8 +++--- Application/Model/Request/PaymentIntent.php | 6 +++++ Application/Model/TransactionHandler/Base.php | 2 +- assets/js/stripe.js | 5 ++++ .../Controller/PaymentController.php | 1 + extend/Application/Model/Order.php | 2 +- 7 files changed, 44 insertions(+), 6 deletions(-) diff --git a/Application/Helper/User.php b/Application/Helper/User.php index 7b9ec71..bfd2b1b 100644 --- a/Application/Helper/User.php +++ b/Application/Helper/User.php @@ -31,9 +31,10 @@ public static function getInstance() /** * Creates Stripe API user and adds customerId to user model + * Returns customerId for direct usage * * @param CoreUser $oUser - * @return void + * @return string */ public function createStripeUser(CoreUser &$oUser) { @@ -46,5 +47,28 @@ public function createStripeUser(CoreUser &$oUser) $oUser->oxuser__stripecustomerid = new Field($oResponse->id); $oUser->save(); } + + return $oUser->oxuser__stripecustomerid->value; + } + + /** + * Checks if given CustomerId is still valid on Stripe account side + * + * @param string $sStripeCustomerId + * @return bool + */ + public function isValidCustomerId($sStripeCustomerId) + { + if (empty($sStripeCustomerId)) { + return false; + } + + $oResponse = Payment::getInstance()->loadStripeApi()->customers->retrieve($sStripeCustomerId); + + if ($oResponse->deleted) { + return false; + } + + return !empty($oResponse->email); } } diff --git a/Application/Model/Request/Card.php b/Application/Model/Request/Card.php index 61e7ef2..d31bdbb 100644 --- a/Application/Model/Request/Card.php +++ b/Application/Model/Request/Card.php @@ -23,10 +23,12 @@ class Card extends Base */ public function addRequestParameters($sStripeCardToken, CoreUser $oUser) { - if (empty($this->getCustomerId($oUser))) { - UserHelper::getInstance()->createStripeUser($oUser); + $sStripeCustomerId = $this->getCustomerId($oUser); + if (!UserHelper::getInstance()->isValidCustomerId($sStripeCustomerId)) { + $sStripeCustomerId = UserHelper::getInstance()->createStripeUser($oUser); } - $this->sStripeCustomerId = $this->getCustomerId($oUser); + + $this->sStripeCustomerId = $sStripeCustomerId; $this->addParameter('source', $sStripeCardToken); } diff --git a/Application/Model/Request/PaymentIntent.php b/Application/Model/Request/PaymentIntent.php index e097965..f92f7d6 100644 --- a/Application/Model/Request/PaymentIntent.php +++ b/Application/Model/Request/PaymentIntent.php @@ -8,6 +8,7 @@ use OxidSolutionCatalysts\Stripe\Application\Helper\Order as OrderHelper; use OxidSolutionCatalysts\Stripe\Application\Helper\Payment as PaymentHelper; +use OxidSolutionCatalysts\Stripe\Application\Helper\User as UserHelper; use OxidSolutionCatalysts\Stripe\Application\Model\RequestLog; use OxidEsales\Eshop\Application\Model\Order as CoreOrder; @@ -39,9 +40,14 @@ public function addRequestParameters(CoreOrder $oOrder, $dAmount, $sReturnUrl, $ $oCoreUser = $oOrder->getUser(); $sStripeCustomerId = $this->getCustomerId($oCoreUser); + if (!UserHelper::getInstance()->isValidCustomerId($sStripeCustomerId)) { + $sStripeCustomerId = UserHelper::getInstance()->createStripeUser($oCoreUser); + } + if (!empty($sStripeCustomerId)) { $this->addParameter('customer', $sStripeCustomerId); } + $this->addParameter('receipt_email', $this->getCustomerEmail($oCoreUser)); if ($oPaymentModel->isRedirectUrlNeeded($oOrder) === true) { diff --git a/Application/Model/TransactionHandler/Base.php b/Application/Model/TransactionHandler/Base.php index 2a50835..cbcb512 100644 --- a/Application/Model/TransactionHandler/Base.php +++ b/Application/Model/TransactionHandler/Base.php @@ -29,7 +29,7 @@ abstract class Base protected function logResult($aResult) { if ((bool)PaymentHelper::getInstance()->getShopConfVar('blStripeLogTransactionInfo') === true) { - $sMessage = date("Y-m-d h:i:s")." Transaction handled: ".print_r($aResult, true)." \n"; + $sMessage = (new \DateTimeImmutable())->format('Y-m-d H:i:s')." Transaction handled: ".print_r($aResult, true)." \n"; $sLogFilePath = getShopBasePath().'/log/'.$this->sLogFileName; $oLogFile = fopen($sLogFilePath, "a"); diff --git a/assets/js/stripe.js b/assets/js/stripe.js index 7d38d96..d3c2a58 100644 --- a/assets/js/stripe.js +++ b/assets/js/stripe.js @@ -1,3 +1,8 @@ +/** + * Copyright © OXID eSales AG. All rights reserved. + * See LICENSE file for license details. + */ + function stripeGetSelectedPaymentMethod() { var paymentForm = document.getElementById('payment'); if (paymentForm && paymentForm.paymentid) { diff --git a/extend/Application/Controller/PaymentController.php b/extend/Application/Controller/PaymentController.php index 894bdc1..34c9051 100644 --- a/extend/Application/Controller/PaymentController.php +++ b/extend/Application/Controller/PaymentController.php @@ -142,6 +142,7 @@ public function validatepayment() } } } catch (\Exception $oEx) { + Registry::getLogger()->error($oEx->getTraceAsString()); $mRet = 'payment'; } diff --git a/extend/Application/Model/Order.php b/extend/Application/Model/Order.php index 6d4b268..4f912c8 100644 --- a/extend/Application/Model/Order.php +++ b/extend/Application/Model/Order.php @@ -207,7 +207,7 @@ public function stripeMarkAsPaid() } /** - * Mark order as paid + * Mark order's reminder email as sent * * @return void */