Installation du module Stripe
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
/**
|
||||
* This class has been generated by TheliaStudio
|
||||
* For more information, see https://github.com/thelia-modules/TheliaStudio
|
||||
*/
|
||||
|
||||
namespace StripePayment\Controller\Base;
|
||||
|
||||
use StripePayment\StripePayment;
|
||||
use Thelia\Controller\Admin\BaseAdminController;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use StripePayment\Model\Config\StripePaymentConfigValue;
|
||||
|
||||
/**
|
||||
* Class StripePaymentConfigController
|
||||
* @package StripePayment\Controller\Base
|
||||
* @author TheliaStudio
|
||||
*/
|
||||
class StripePaymentConfigController extends BaseAdminController
|
||||
{
|
||||
public function defaultAction()
|
||||
{
|
||||
if (null !== $response = $this->checkAuth([AdminResources::MODULE], ["stripepayment"], AccessManager::VIEW)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
return $this->render("stripepayment-configuration");
|
||||
}
|
||||
|
||||
public function saveAction()
|
||||
{
|
||||
if (null !== $response = $this->checkAuth([AdminResources::MODULE], ["stripepayment"], AccessManager::UPDATE)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$baseForm = $this->createForm("stripepayment.configuration");
|
||||
|
||||
$errorMessage = null;
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($baseForm);
|
||||
$data = $form->getData();
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::ENABLED, is_bool($data["enabled"]) ? (int) ($data["enabled"]) : $data["enabled"]);
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::STRIPE_ELEMENT, is_bool($data["stripe_element"]) ? (int) ($data["stripe_element"]) : $data["stripe_element"]);
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::ONE_CLICK_PAYMENT, is_bool($data["one_click_payment"]) ? (int) ($data["one_click_payment"]) : $data["one_click_payment"]);
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::SECRET_KEY, is_bool($data["secret_key"]) ? (int) ($data["secret_key"]) : $data["secret_key"]);
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::PUBLISHABLE_KEY, is_bool($data["publishable_key"]) ? (int) ($data["publishable_key"]) : $data["publishable_key"]);
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::WEBHOOKS_KEY, is_bool($data["webhooks_key"]) ? (int) ($data["webhooks_key"]) : $data["webhooks_key"]);
|
||||
StripePayment::setConfigValue(StripePaymentConfigValue::SECURE_URL, is_bool($data["secure_url"]) ? (int) ($data["secure_url"]) : $data["secure_url"]);
|
||||
} catch (FormValidationException $ex) {
|
||||
// Invalid data entered
|
||||
$errorMessage = $this->createStandardFormValidationErrorMessage($ex);
|
||||
} catch (\Exception $ex) {
|
||||
// Any other error
|
||||
$errorMessage = $this->getTranslator()->trans('Sorry, an error occurred: %err', ['%err' => $ex->getMessage()], [], StripePayment::MESSAGE_DOMAIN);
|
||||
}
|
||||
|
||||
if (null !== $errorMessage) {
|
||||
// Mark the form as with error
|
||||
$baseForm->setErrorMessage($errorMessage);
|
||||
|
||||
// Send the form and the error to the parser
|
||||
$this->getParserContext()
|
||||
->addForm($baseForm)
|
||||
->setGeneralError($errorMessage)
|
||||
;
|
||||
} else {
|
||||
$this->getParserContext()
|
||||
->set("success", true)
|
||||
;
|
||||
}
|
||||
|
||||
return $this->defaultAction();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
/**
|
||||
* This class has been generated by TheliaStudio
|
||||
* For more information, see https://github.com/thelia-modules/TheliaStudio
|
||||
*/
|
||||
|
||||
namespace StripePayment\Controller;
|
||||
|
||||
use StripePayment\Controller\Base\StripePaymentConfigController as BaseStripePaymentConfigController;
|
||||
|
||||
/**
|
||||
* Class StripePaymentConfigController
|
||||
* @package StripePayment\Controller
|
||||
*/
|
||||
class StripePaymentConfigController extends BaseStripePaymentConfigController
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace StripePayment\Controller;
|
||||
|
||||
use Thelia\Module\BasePaymentModuleController;
|
||||
|
||||
/**
|
||||
* Class StripePaymentController
|
||||
* @package StripePayment\Controller
|
||||
* @author Etienne Perriere - OpenStudio <eperriere@openstudio.fr>
|
||||
*/
|
||||
class StripePaymentController extends BasePaymentModuleController
|
||||
{
|
||||
/**
|
||||
* Return a module identifier used to calculate the name of the log file,
|
||||
* and in the log messages.
|
||||
*
|
||||
* @return string the module code
|
||||
*/
|
||||
protected function getModuleCode()
|
||||
{
|
||||
return 'StripePayment';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
|
||||
namespace StripePayment\Controller;
|
||||
|
||||
use Stripe\Checkout\Session;
|
||||
use Stripe\Error\SignatureVerification;
|
||||
use Stripe\Stripe;
|
||||
use Stripe\Webhook;
|
||||
use StripePayment\Classes\StripePaymentLog;
|
||||
use StripePayment\StripePayment;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Order\OrderEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Core\HttpFoundation\Response;
|
||||
use Thelia\Model\OrderQuery;
|
||||
use Thelia\Model\OrderStatusQuery;
|
||||
|
||||
class StripeWebHooksController extends BaseFrontController
|
||||
{
|
||||
public function listenAction($secure_url)
|
||||
{
|
||||
if (StripePayment::getConfigValue('secure_url') == $secure_url) {
|
||||
try {
|
||||
Stripe::setApiKey(StripePayment::getConfigValue('secret_key'));
|
||||
|
||||
// You can find your endpoint's secret in your webhook settings
|
||||
$endpointSecret = StripePayment::getConfigValue('webhooks_key');
|
||||
|
||||
$payload = file_get_contents('php://input');
|
||||
$sigHeader = $_SERVER['HTTP_STRIPE_SIGNATURE'];
|
||||
$event = null;
|
||||
|
||||
$event = Webhook::constructEvent(
|
||||
$payload, $sigHeader, $endpointSecret
|
||||
);
|
||||
|
||||
(new StripePaymentLog())->logText(serialize($event));
|
||||
|
||||
// Handle the event
|
||||
switch ($event->type) {
|
||||
case 'checkout.session.completed':
|
||||
/** @var Session $sessionCompleted */
|
||||
$sessionCompleted = $event->data->object;
|
||||
$this->handleSessionCompleted($sessionCompleted);
|
||||
break;
|
||||
case 'payment_intent.succeeded':
|
||||
// Needed to wait for order to be created (Stripe is faster than Thelia)
|
||||
sleep(5);
|
||||
/** @var Session $sessionCompleted */
|
||||
$paymentId = $event->data->object->id;
|
||||
$this->handlePaymentIntentSuccess($paymentId);
|
||||
break;
|
||||
case 'payment_intent.payment_failed':
|
||||
// Needed to wait for order to be created (Stripe is faster than Thelia)
|
||||
sleep(5);
|
||||
/** @var Session $sessionCompleted */
|
||||
$paymentId = $event->data->object->id;
|
||||
$this->handlePaymentIntentFail($paymentId);
|
||||
break;
|
||||
default:
|
||||
// Unexpected event type
|
||||
(new StripePaymentLog())->logText('Unexpected event type');
|
||||
|
||||
return new Response('Unexpected event type', 400);
|
||||
}
|
||||
|
||||
return new Response('Success', 200);
|
||||
} catch (\UnexpectedValueException $e) {
|
||||
// Invalid payload
|
||||
(new StripePaymentLog())->logText($e->getMessage());
|
||||
return new Response('Invalid payload', 400);
|
||||
} catch (SignatureVerification $e) {
|
||||
return new Response($e->getMessage(), 400);
|
||||
} catch (\Exception $e) {
|
||||
return new Response($e->getMessage(), 404);
|
||||
}
|
||||
}
|
||||
|
||||
return new Response('Bad request', 400);
|
||||
}
|
||||
|
||||
protected function handleSessionCompleted(Session $sessionCompleted)
|
||||
{
|
||||
$order = OrderQuery::create()
|
||||
->findOneByRef($sessionCompleted->client_reference_id);
|
||||
|
||||
if (null === $order) {
|
||||
throw new \Exception("Order with reference $sessionCompleted->client_reference_id not found");
|
||||
}
|
||||
|
||||
$this->setOrderToPaid($order);
|
||||
}
|
||||
|
||||
protected function handlePaymentIntentSuccess($paymentId)
|
||||
{
|
||||
$order = OrderQuery::create()
|
||||
->findOneByTransactionRef($paymentId);
|
||||
|
||||
if (null === $order) {
|
||||
throw new \Exception("Order with transaction ref $paymentId not found");
|
||||
}
|
||||
|
||||
$this->setOrderToPaid($order);
|
||||
}
|
||||
|
||||
protected function handlePaymentIntentFail($paymentId)
|
||||
{
|
||||
$order = OrderQuery::create()
|
||||
->findOneByTransactionRef($paymentId);
|
||||
|
||||
if (null === $order) {
|
||||
throw new \Exception("Order with transaction ref $paymentId not found");
|
||||
}
|
||||
|
||||
$this->setOrderToCanceled($order);
|
||||
}
|
||||
|
||||
protected function setOrderToPaid($order)
|
||||
{
|
||||
$paidStatusId = OrderStatusQuery::create()
|
||||
->filterByCode('paid')
|
||||
->select('ID')
|
||||
->findOne();
|
||||
|
||||
$event = new OrderEvent($order);
|
||||
$event->setStatus($paidStatusId);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_UPDATE_STATUS, $event);
|
||||
}
|
||||
|
||||
protected function setOrderToCanceled($order)
|
||||
{
|
||||
$canceledStatusId = OrderStatusQuery::create()
|
||||
->filterByCode('canceled')
|
||||
->select('ID')
|
||||
->findOne();
|
||||
|
||||
$event = new OrderEvent($order);
|
||||
$event->setStatus($canceledStatusId);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_UPDATE_STATUS, $event);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user