Skip to content

Commit

Permalink
Stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkIncognito85 committed May 23, 2021
1 parent 6903849 commit d1b2d38
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 258 deletions.
4 changes: 4 additions & 0 deletions .env.copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Please add this in your .env global
STRIPE_ENDPOINT=XXXX
STRIPE_SECRET=XXXX
STRIPE_PUBLIC=XXXX
34 changes: 29 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,33 @@
Merci d'avoir acheté le module Stripe !
# Stripe Module
### English

Il vous suffit de glisser ces fichiers dans la racine de ClientX !
![Stripe logo](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQFZVtgqQoU3xMNDMfYzfbAyC2LZJt_MwtHmJANd1MqAC4jjDpBY0opV2uR9FPL2V0qkA&usqp=CAU "Stripe logo")

Ajoutez la ligne ->addModule(StripeModule::class) dans index.php
The entire CLIENTXCMS team thanks you for this purchase. We hope you like it.

Puis rendez-vous dans Bases de Données dans l'espace Administration de ClientX puis faites Migrate.
To install this module please refer to the documentation.

Le tour est joué et vous avez maintenant accès au module Stripe.
## Support & Documentation #

For any questions regarding this module, you should contact the CLIENTXCMS service support:

- https://clientxcms.com/client/support Technical support
- https://clientxcms.com/docs/en/stripe Official documentation
- https://clientxcms.com/discord Community discord


CLIENTXCMS, Customer manager, easy billing made for everyone.


### French
Toute l'équipe de CLIENTXCMS vous remercie pour cette achat. Nous espérons que celui la vous plaisent.

Pour installer ce module veuillez vous référez à la documentation.

# Support & Documentation #
Pour toutes questions par rapport à ce module, vous devez vous adressez au support du service de CLIENTXCMS :
- https://clientxcms.com/client/support Support technique
- https://clientxcms.com/docs/fr/stripe documentation Officiel
- https://clientxcms.com/discord Discord communautaire

CLIENTXCMS, Customer manager, easy billing made for everyone.
4 changes: 4 additions & 0 deletions src/.env.copy
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Please add this in your .env global
STRIPE_ENDPOINT=XXXX
STRIPE_SECRET=XXXX
STRIPE_PUBLIC=XXXX
27 changes: 7 additions & 20 deletions src/Actions/StripeAdminAction.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,12 @@
<?php
namespace App\Stripe\Actions;

use App\Stripe\Database\StripeTable;
use ClientX\Actions\Action;
use ClientX\Renderer\RendererInterface;
use ClientX\Actions\Payment\PaymentAdminAction;

class StripeAdminAction extends Action {
class StripeAdminAction extends PaymentAdminAction
{

/**
* @var StripeTable
*/
private $table;

public function __construct(RendererInterface $renderer, StripeTable $table)
{
$this->renderer = $renderer;
$this->table = $table;
}

public function __invoke()
{
return $this->render('@stripe_admin/index', ['items' => $this->table->findAll()]);
}
}
protected $routePrefix = "stripe.admin";
protected $moduleName = "Stripe";
protected $paymenttype = "stripe";
}
71 changes: 32 additions & 39 deletions src/Actions/StripeApiAction.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
<?php
namespace App\Stripe\Actions;

use App\Shop\Database\InvoiceTable;
use \App\Stripe\Api\Stripe;
use App\Stripe\Database\StripeTable;
use App\Auth\Database\UserTable;
use App\Shop\Entity\Transaction;
use App\Shop\Services\TransactionService;
use App\Stripe\StripePaymentManager;
use ClientX\Actions\Action;
use Psr\Http\Message\ServerRequestInterface;

class StripeApiAction {

/**
* @var Stripe
*/
private $stripe;

/**
* @var StripeTable
*/
private $table;

/**
* @var InvoiceTable
*/
private $invoiceTable;

public function __construct(Stripe $stripe, StripeTable $table, InvoiceTable $invoiceTable)
class StripeApiAction extends Action
{
private UserTable $user;
private StripePaymentManager $manager;
private TransactionService $transaction;
public function __construct(StripePaymentManager $manager, TransactionService $transaction, UserTable $user)
{
$this->stripe = $stripe;
$this->table = $table;
$this->invoiceTable = $invoiceTable;
$this->manager = $manager;
$this->transaction = $transaction;
$this->user = $user;
}

public function __invoke(ServerRequestInterface $request)
{
$signature = $request->getServerParams()["HTTP_STRIPE_SIGNATURE"];
$webhook = $this->stripe->getWebhook($signature);
if ($webhook->type === 'checkout.session'){
$object = $webhook->data->object;
$id = $object->metadata->invoice;
if ($object->payment_status !== "paid"){
$this->invoiceTable->updateStatus(0, $id);
}
$this->invoiceTable->update($id, [
'paymentId' => $object->id
]);
$this->table->createTransaction($webhook);
var_dump($webhook);
$webhook = $this->manager->getWebhook($request->getServerParams()['HTTP_STRIPE_SIGNATURE']);
$object = $webhook->data->object;
if (empty($object->metadata) === false){

$response = $this->manager->confirm($request);
return $this->json(['success' => $response instanceof Transaction]);
}
$id = $object->metadata->transaction;

$userId = $object->metadata->user;

$user = $this->user->find($userId);
$transaction = $this->transaction->findTransaction($id);
if ($transaction != null && $transaction->getState() === $transaction::PENDING) {
$response = $this->manager->test($transaction, $request, $user);
return $this->json(['success' => $response instanceof Transaction]);
}
return $this->json(['error' => true]);

die();
}
}
}
5 changes: 3 additions & 2 deletions src/Api/Entity/StripeUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

use App\Account\User;

class StripeUser extends User {
class StripeUser extends User
{

/**
* @var string
Expand All @@ -19,4 +20,4 @@ public function setStripeId($stripeId)
{
$this->stripeId = $stripeId;
}
}
}
69 changes: 37 additions & 32 deletions src/Api/Stripe.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace App\Stripe\Api;

use App\Shop\Entity\Transaction;
use App\Stripe\Api\Entity\StripeUser;
use Exception;
use Psr\Log\LoggerInterface;
use Stripe\BalanceTransaction;
use Stripe\Checkout\Session;
Expand All @@ -13,7 +15,8 @@
use Stripe\StripeClient;
use Stripe\Webhook;

class Stripe {
class Stripe
{
/**
* @var StripeClient
*/
Expand All @@ -40,7 +43,7 @@ class Stripe {

const STRIPE_VERSION = "2020-08-27";

public function __construct($privateKey, $publicKey,$endpointkey, LoggerInterface $logger)
public function __construct($privateKey, $publicKey, $endpointkey, LoggerInterface $logger)
{
$this->logger = $logger;
$this->setPrivateKey($privateKey);
Expand Down Expand Up @@ -86,26 +89,31 @@ public function getPaymentIntent(string $id): PaymentIntent
return $this->stripe->paymentIntents->retrieve($id);
}

public function createPaymentSession(StripeUser $user, $items, array $urls, int $invoice): Session
public function createPaymentSession(StripeUser $user, $items, array $urls, Transaction $transaction): Session
{
$session = $this->stripe->checkout->sessions->create([
//'customer_email' => $user->getEmail(),
'cancel_url' => $urls[1],
'success_url' => $urls[0],
'mode' => 'payment',
'payment_method_types' => [
'card',
],

'metadata' => [
'invoice' => $invoice,
'user' => $user->getId()
],
'customer' => $user->getStripeId(),
'line_items' => $items,
]);
try {

return $session;
$session = $this->stripe->checkout->sessions->create([
//'customer_email' => $user->getEmail(),
'cancel_url' => $urls['cancel'],
'success_url' => $urls['return'],
'mode' => 'payment',
'payment_method_types' => [
'card',
],

'metadata' => [
'transaction' => $transaction->getId(),
'user' => $user->getId()
],
'customer' => $user->getStripeId(),
'line_items' => $items,
]);

return $session;
} catch (Exception $e){
dd($e->getMessage());
}
}

public function getCheckoutSessionFromIntent(string $paymentIntent): Session
Expand Down Expand Up @@ -135,12 +143,10 @@ public function getWebhook(string $signature)
{
$payload = @file_get_contents('php://input');
try {
$webhook = Webhook::constructEvent($payload, $signature, $this->endpointkey);
} catch(\UnexpectedValueException | SignatureVerificationException $e) {
$webhook = Webhook::constructEvent($payload, $signature, $this->endpointkey, 0);
} catch (\UnexpectedValueException | SignatureVerificationException $e) {
$this->logger->error($e->getMessage());
throw new \Exception($e->getMessage());


}
return $webhook;
}
Expand All @@ -150,8 +156,8 @@ private function setEndpointKey($endpointkey)
{

if ($endpointkey === null) {
$this->logger->error("La clée webhook stripe est nulle.");
throw new \Exception("Erreur interne.");
$this->logger->error("Endpoint key is null. Please add env STRIPE_ENDPOINT with your key");
throw new \Exception("Internal error");
}
$this->endpointkey = $endpointkey;
}
Expand All @@ -160,17 +166,17 @@ private function setPublicKey($publicKey)
{

if ($publicKey === null) {
$this->logger->error("La clée publique stripe est nulle.");
throw new \Exception("Erreur interne.");
$this->logger->error("public key is null. Please add env STRIPE_PUBLIC with your key.");
throw new \Exception("Internal error");
}
$this->publicKey = $publicKey;
}

private function setPrivateKey($privateKey)
{
if ($privateKey === null) {
$this->logger->error("La clée privée stripe est nulle.");
throw new \Exception("Erreur interne.");
$this->logger->error("private key is null. Please add env STRIPE_PRIVATE with your key.");
throw new \Exception("Internal error");
}
$this->privateKey = $privateKey;
\Stripe\Stripe::setApiKey($privateKey);
Expand All @@ -181,5 +187,4 @@ protected function setStripeVersion()
{
StripeStripe::setApiVersion(self::STRIPE_VERSION);
}

}
}
8 changes: 5 additions & 3 deletions src/StripeModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
use ClientX\Router;
use Psr\Container\ContainerInterface;

class StripeModule extends Module {
class StripeModule extends Module
{

const DEFINITIONS = __DIR__ . '/config.php';
const MIGRATIONS = __DIR__ . '/db/migrations';

public function __construct(Router $router, RendererInterface $renderer, ContainerInterface $container)
{
$renderer->addPath("stripe_admin", __DIR__ . '/Views');
$router->post('/api/stripe', StripeApiAction::class, 'stripe.webhook');
$router->post('/stripe/api', StripeApiAction::class, 'stripe.webhook');
if ($container->has('admin.prefix')) {
$prefix = $container->get('admin.prefix');
$router->get($prefix . "/stripe", StripeAdminAction::class, 'stripe.admin');
$router->get($prefix . "/stripe", StripeAdminAction::class, 'stripe.admin.index');
}
}
}
}
Loading

0 comments on commit d1b2d38

Please sign in to comment.