Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
Merge pull request #107 from 202-ecommerce/release/2.4.x
Browse files Browse the repository at this point in the history
Release 2.4.2
  • Loading branch information
clotairer authored Jan 5, 2022
2 parents 66359f8 + 314b97c commit bf58419
Show file tree
Hide file tree
Showing 35 changed files with 1,825 additions and 462 deletions.
2 changes: 1 addition & 1 deletion 202/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<property name="src-dir" value="${basedir}" />
<property name="TARGETNAME" value="stripe_official" />
<property name="TARGETBRANCH" value="${env.GIT_BRANCH}" />
<property name="TARGETVERSION" value="2.4.1" />
<property name="TARGETVERSION" value="2.4.2" />
<property name="PHPVERSION" value="5.6" />
<property name="PSVERSION" value="1.7.5.x" />

Expand Down
2 changes: 1 addition & 1 deletion 202/classlib.yml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version: release/2.1.1
version: release/2.3.1
29 changes: 23 additions & 6 deletions classes/StripeIdempotencyKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,10 @@ public function createNewOne($id_cart, $datasIntent)
]
);

$stripeIdempotencyKey = new StripeIdempotencyKey();
$stripeIdempotencyKey->getByIdCart($id_cart);
$stripeIdempotencyKey->id_cart = $id_cart;
$stripeIdempotencyKey->idempotency_key = $idempotency_key;
$stripeIdempotencyKey->id_payment_intent = $intent->id;
$stripeIdempotencyKey->save();
$this->id_cart = $id_cart;
$this->idempotency_key = $idempotency_key;
$this->id_payment_intent = $intent->id;
$this->save();

$paymentIntent = new StripePaymentIntent();
$paymentIntent->setIdPaymentIntent($intent->id);
Expand All @@ -154,4 +152,23 @@ public function createNewOne($id_cart, $datasIntent)

return $intent;
}

/**
* @throws \Stripe\Exception\ApiErrorException
* @throws PrestaShopException
*/
public function updateIntentData($intentData)
{
$intent = \Stripe\PaymentIntent::update($this->id_payment_intent, $intentData);

$paymentIntent = new StripePaymentIntent();
$paymentIntent->findByIdPaymentIntent($this->id_payment_intent);
$paymentIntent->setStatus($intent->status);
$paymentIntent->setAmount($intent->amount);
$paymentIntent->setCurrency($intent->currency);
$paymentIntent->setDateUpd(date("Y-m-d H:i:s"));
$paymentIntent->save(false, false);

return $intent;
}
}
35 changes: 14 additions & 21 deletions classes/actions/ValidationOrderActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,14 +302,6 @@ public function createOrder()

$this->conveyor['cart'] = new Cart((int)$this->conveyor['id_cart']);

$customer = new Customer($this->conveyor['cart']->id_customer);

if (isset($customer->secure_key)) {
$this->conveyor['secure_key'] = $customer->secure_key;
} else {
$this->conveyor['secure_key'] = false;
}

$paid = $this->conveyor['amount'];

/* Add transaction on Prestashop back Office (Order) */
Expand Down Expand Up @@ -344,15 +336,15 @@ public function createOrder()
$this->context->country = new Country($addressDelivery->id_country);

$this->module->validateOrder(
(int)$this->conveyor['id_cart'],
$this->conveyor['cart']->id,
(int)$orderStatus,
$paid,
$this->module->l(Tools::ucfirst(Stripe_official::$paymentMethods[$this->conveyor['datas']['type']]['name']).' via Stripe', 'ValidationOrderActions'),
$message,
array(),
null,
false,
$this->conveyor['secure_key']
$this->conveyor['cart']->secure_key
);

ProcessLoggerHandler::logInfo(
Expand All @@ -362,7 +354,7 @@ public function createOrder()
'ValidationOrderActions - createOrder'
);

$idOrder = Order::getOrderByCartId((int)$this->conveyor['id_cart']);
$idOrder = Order::getOrderByCartId($this->conveyor['cart']->id);
$order = new Order($idOrder);

// capture payment for card if no catch and authorize enabled
Expand Down Expand Up @@ -419,7 +411,7 @@ public function createOrder()
if ($this->conveyor['status'] == 'requires_capture') {
$stripeCapture = new StripeCapture();
$stripeCapture->id_payment_intent = $this->conveyor['paymentIntent'];
$stripeCapture->id_order = Order::getOrderByCartId((int)$this->conveyor['cart']->id);
$stripeCapture->id_order = Order::getOrderByCartId($this->conveyor['cart']->id);
$stripeCapture->expired = 0;
$stripeCapture->date_catch = date('Y-m-d H:i:s');
$stripeCapture->save();
Expand Down Expand Up @@ -518,33 +510,34 @@ public function sendMail()
public function saveCard()
{
try {
if ($this->conveyor['saveCard'] == null) {
if (empty($this->conveyor['saveCard']) === true || empty($this->conveyor['cart']->id_customer) === true) {
return true;
}

$cartCustomer = new Customer($this->conveyor['cart']->id_customer);

$stripeAccount = \Stripe\Account::retrieve();

$stripeCustomer = new StripeCustomer();
$stripeCustomer = $stripeCustomer->getCustomerById($this->context->customer->id, $stripeAccount->id);
$stripeCustomer = $stripeCustomer->getCustomerById($cartCustomer->id, $stripeAccount->id);

if ($stripeCustomer->id == null) {
if (empty($stripeCustomer->id) === true) {
$customer = \Stripe\Customer::create([
'description' => 'Customer created from Prestashop Stripe module',
'email' => $this->context->customer->email,
'name' => $this->context->customer->firstname.' '.$this->context->customer->lastname,
'email' => $cartCustomer->email,
'name' => $cartCustomer->firstname.' '.$cartCustomer->lastname,
]);

$stripeCustomer->id_customer = $this->context->customer->id;
$stripeCustomer->id_customer = $cartCustomer->id;
$stripeCustomer->stripe_customer_key = $customer->id;
$stripeCustomer->id_account = $stripeAccount->id;
$stripeCustomer->save();
}

$customer = \Stripe\Customer::retrieve($stripeCustomer->stripe_customer_key);

$stripeCard = new StripeCard();
$stripeCard->stripe_customer_key = $customer->id;
$stripeCard->stripe_customer_key = $stripeCustomer->stripe_customer_key;
$stripeCard->payment_method = $this->conveyor['token'];

if (!$stripeCard->save()) {
ProcessLoggerHandler::logError(
'Error during save card, card has not been registered',
Expand Down
70 changes: 48 additions & 22 deletions controllers/front/createIntent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,35 @@ public function initContent()
'createIntent - intiContent'
);

$amount = Tools::ps_round(Tools::getValue('amount'));
$currency = Tools::getValue('currency');
$paymentOption = Tools::getValue('payment_option');
$paymentMethodId = Tools::getValue('id_payment_method');
try {
$cart = $this->context->cart;

$currency = new Currency($cart->id_currency);
$amount = $cart->getOrderTotal();
$amount = Tools::ps_round($amount, 2);
$amount = $this->module->isZeroDecimalCurrency($currency->iso_code) ? $amount : $amount * 100;

$intentData = $this->constructIntentData($amount, $currency, $paymentOption, $paymentMethodId);
$paymentOption = Tools::getValue('payment_option');
$paymentMethodId = Tools::getValue('id_payment_method');

$cardData = $this->constructCardData($paymentMethodId);
$intentData = $this->constructIntentData($amount, $currency, $paymentOption, $paymentMethodId);

$intent = $this->createIdempotencyKey($intentData);
$cardData = $this->constructCardData($paymentMethodId);

$this->registerStripeEvent($intent);
$intent = $this->createIdempotencyKey($intentData);

$this->registerStripeEvent($intent);
} catch (Exception $e) {
ProcessLoggerHandler::logError(
"Retrieve Stripe Account Error => ".$e->getMessage(),
null,
null,
'createIntent'
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
}

ProcessLoggerHandler::logInfo(
'[ Intent Creation Ending ]',
Expand All @@ -62,11 +79,13 @@ public function initContent()
);
ProcessLoggerHandler::closeLogger();

echo Tools::jsonEncode(array(
'intent' => $intent,
'cardPayment' => $cardData['cardPayment'],
'saveCard' => $cardData['save_card']
));
$this->ajaxDie(
json_encode([
'intent' => $intent,
'cardPayment' => $cardData['cardPayment'],
'saveCard' => $cardData['save_card']
])
);
exit;
}

Expand Down Expand Up @@ -124,7 +143,7 @@ private function constructIntentData($amount, $currency, $paymentOption, $paymen
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
} catch (PrestaShopDatabaseException $e) {
ProcessLoggerHandler::logError(
"Retrieve Prestashop State Error => ".$e->getMessage(),
Expand All @@ -134,7 +153,7 @@ private function constructIntentData($amount, $currency, $paymentOption, $paymen
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
} catch (PrestaShopException $e) {
ProcessLoggerHandler::logError(
"Retrieve Prestashop State Error => ".$e->getMessage(),
Expand All @@ -144,7 +163,7 @@ private function constructIntentData($amount, $currency, $paymentOption, $paymen
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
}
}

Expand Down Expand Up @@ -204,8 +223,15 @@ private function constructCardData($paymentMethodId)
private function createIdempotencyKey($intentData)
{
try {
$cart = $this->context->cart;
$stripeIdempotencyKey = new StripeIdempotencyKey();
$intent = $stripeIdempotencyKey->createNewOne($this->context->cart->id, $intentData);
$stripeIdempotencyKey = $stripeIdempotencyKey->getByIdCart($cart->id);

if (empty($stripeIdempotencyKey->id) === true) {
$intent = $stripeIdempotencyKey->createNewOne($cart->id, $intentData);
} else {
$intent = $stripeIdempotencyKey->updateIntentData($intentData);
}

ProcessLoggerHandler::logInfo(
'Intent => '.$intent,
Expand All @@ -224,7 +250,7 @@ private function createIdempotencyKey($intentData)
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die($e->getMessage());
$this->ajaxDie($e->getMessage());
} catch (PrestaShopException $e) {
ProcessLoggerHandler::logError(
"Save Stripe Idempotency Key Error => ".$e->getMessage(),
Expand All @@ -234,7 +260,7 @@ private function createIdempotencyKey($intentData)
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
} catch (PrestaShopException $e) {
ProcessLoggerHandler::logError(
"Save Stripe Payment Intent Error => ".$e->getMessage(),
Expand All @@ -244,7 +270,7 @@ private function createIdempotencyKey($intentData)
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
}
}

Expand Down Expand Up @@ -273,7 +299,7 @@ private function registerStripeEvent($intent)
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
}
} catch (PrestaShopException $e) {
ProcessLoggerHandler::logError(
Expand All @@ -284,7 +310,7 @@ private function registerStripeEvent($intent)
);
ProcessLoggerHandler::closeLogger();
http_response_code(400);
die('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
$this->ajaxDie('An unexpected problem has occurred. Please contact the support : https://addons.prestashop.com/en/contact-us?id_product=24922');
}
}

Expand Down
Loading

0 comments on commit bf58419

Please sign in to comment.