Skip to content

Commit

Permalink
Ajout création de clients
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkIncognito85 committed Nov 10, 2022
1 parent 12581a1 commit 491a899
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/Stripe/Api/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@ public function getPaymentIntent(string $id): PaymentIntent
return $this->stripe->paymentIntents->retrieve($id);
}

public function createPaymentSession(StripeUser $user, $items, array $urls, Transaction $transaction): Session
public function createPaymentSession(StripeUser $user, $items, array $urls, Transaction $transaction)
{
try {

$session = $this->stripe->checkout->sessions->create([
//'customer_email' => $user->getEmail(),
'cancel_url' => $urls['cancel'],
'success_url' => $urls['return'],
'mode' => 'payment',
Expand All @@ -115,6 +114,10 @@ public function createPaymentSession(StripeUser $user, $items, array $urls, Tran

return $session;
} catch (Exception $e){
if ("No such customer: '$stripeId'" == $e->getMessage()){
$this->createCustomer($user);
return $user;
}
dd($e->getMessage());
}
}
Expand Down
25 changes: 17 additions & 8 deletions src/Stripe/StripePaymentManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ClientX\Payment\PaymentManagerInterface;
use ClientX\Renderer\RendererInterface;
use ClientX\Router;
use Stripe\Checkout\Session;
use Psr\Http\Message\ServerRequestInterface as Request;

class StripePaymentManager extends AbstractPaymentManager implements PaymentManagerInterface
Expand Down Expand Up @@ -49,12 +50,12 @@ public function process(Transaction $transaction, Request $request, User $user)

$items = collect($transaction->getItems())->filter(function($item) { return $item->price() > 0;})->map(function (TransactionItem $item, $i) use ($transaction) {
$discount = 0;
$next = $transaction->getItems()[$i+1] ?? null;
if ($next != null) {
if ($next->price() < 0) {
$discount = $next->price();
}
}
$next = $transaction->getItems()[$i+1] ?? null;
if ($next != null) {
if ($next->price() < 0) {
$discount = $next->price();
}
}
return
[
'price_data' =>
Expand All @@ -69,8 +70,16 @@ public function process(Transaction $transaction, Request $request, User $user)

$user = $this->createStripeUser($this->auth->getUser());
$session = $this->stripe->createPaymentSession($user, $items, $this->getRedirectsLinks($request, $transaction), $transaction);
$params = ['session' => $session, 'key' => $this->stripe->getPublicKey()];
return $this->renderer->render("@stripe_admin/autoredirect", $params);
if ($session instanceof Session){
$params = ['session' => $session, 'key' => $this->stripe->getPublicKey()];
return $this->renderer->render("@stripe_admin/autoredirect", $params);
} else {
$this->table->update($session->getId(), [
'stripe_id' => json_encode($session->getStripeId(true))
]);
$params = ['session' => $session, 'key' => $this->stripe->getPublicKey()];
return $this->renderer->render("@stripe_admin/autoredirect", $params);
}
}

public function refund(array $items): bool
Expand Down

0 comments on commit 491a899

Please sign in to comment.