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 784af0f..6dee76c 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)Registry::getConfig()->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/Application/views/frontend/tpl/stripecreditcard.tpl b/Application/views/frontend/tpl/stripecreditcard.tpl index fc64b11..f7daa23 100644 --- a/Application/views/frontend/tpl/stripecreditcard.tpl +++ b/Application/views/frontend/tpl/stripecreditcard.tpl @@ -22,7 +22,6 @@ -[{oxstyle include=$oViewConf->getModuleUrl('stripe','out/src/css/stripe.css')}] [{oxscript include="https://js.stripe.com/v3/"}] [{capture name="stripeComponentsLoad"}] var pubKey = '[{$oPaymentModel->getPublishableKey()}]'; diff --git a/Application/views/frontend/tpl/stripesofort.tpl b/Application/views/frontend/tpl/stripesofort.tpl index 547e743..d0ac7dc 100644 --- a/Application/views/frontend/tpl/stripesofort.tpl +++ b/Application/views/frontend/tpl/stripesofort.tpl @@ -17,4 +17,3 @@ -[{oxstyle include=$oViewConf->getModuleUrl('stripe','out/src/css/stripe.css')}] diff --git a/extend/Application/Controller/PaymentController.php b/extend/Application/Controller/PaymentController.php index 9bc4117..cd1683b 100644 --- a/extend/Application/Controller/PaymentController.php +++ b/extend/Application/Controller/PaymentController.php @@ -138,6 +138,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 3847234..9569802 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 */