Skip to content

Commit

Permalink
Merge pull request #13 from OXID-eSales/FIX_and_update-CustomerId-Check
Browse files Browse the repository at this point in the history
FIX and Improvement
  • Loading branch information
mariolorenz authored Dec 14, 2023
2 parents ae2ec47 + 7ab3143 commit a859770
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 8 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)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");
Expand Down
1 change: 0 additions & 1 deletion Application/views/frontend/tpl/stripecreditcard.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
</div>
</div>

[{oxstyle include=$oViewConf->getModuleUrl('stripe','out/src/css/stripe.css')}]
[{oxscript include="https://js.stripe.com/v3/"}]
[{capture name="stripeComponentsLoad"}]
var pubKey = '[{$oPaymentModel->getPublishableKey()}]';
Expand Down
1 change: 0 additions & 1 deletion Application/views/frontend/tpl/stripesofort.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@
</select>
</div>
</div>
[{oxstyle include=$oViewConf->getModuleUrl('stripe','out/src/css/stripe.css')}]
1 change: 1 addition & 0 deletions extend/Application/Controller/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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 a859770

Please sign in to comment.