Skip to content

Commit

Permalink
Merge pull request #14 from OXID-eSales/OX7-FIX_and_update-CustomerId…
Browse files Browse the repository at this point in the history
…-Check

FIX and Improvement :
  • Loading branch information
mariolorenz authored Dec 14, 2023
2 parents 05daa6a + 5cd2604 commit f85b872
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 6 deletions.
26 changes: 25 additions & 1 deletion Application/Helper/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}
}
8 changes: 5 additions & 3 deletions Application/Model/Request/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 6 additions & 0 deletions Application/Model/Request/PaymentIntent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion Application/Model/TransactionHandler/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 5 additions & 0 deletions assets/js/stripe.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
1 change: 1 addition & 0 deletions extend/Application/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public function validatepayment()
}
}
} catch (\Exception $oEx) {
Registry::getLogger()->error($oEx->getTraceAsString());
$mRet = 'payment';
}

Expand Down
2 changes: 1 addition & 1 deletion extend/Application/Model/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public function stripeMarkAsPaid()
}

/**
* Mark order as paid
* Mark order's reminder email as sent
*
* @return void
*/
Expand Down

0 comments on commit f85b872

Please sign in to comment.