Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/Payment/Stripe.php
Show First 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | public function customerLink(Wallet $wallet): ?string | ||||
); | ); | ||||
} | } | ||||
/** | /** | ||||
* Create a new auto-payment mandate for a wallet. | * Create a new auto-payment mandate for a wallet. | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet | * @param \App\Wallet $wallet The wallet | ||||
* @param array $payment Payment data: | * @param array $payment Payment data: | ||||
* - amount: Value in cents | * - amount: Value in cents (not used) | ||||
* - currency: The operation currency | * - currency: The operation currency | ||||
* - description: Operation desc. | * - description: Operation desc. | ||||
* | * | ||||
* @return array Provider payment/session data: | * @return array Provider payment/session data: | ||||
* - id: Session identifier | * - id: Session identifier | ||||
*/ | */ | ||||
public function createMandate(Wallet $wallet, array $payment): ?array | public function createMandate(Wallet $wallet, array $payment): ?array | ||||
{ | { | ||||
// Register the user in Stripe, if not yet done | // Register the user in Stripe, if not yet done | ||||
$customer_id = self::stripeCustomerId($wallet, true); | $customer_id = self::stripeCustomerId($wallet, true); | ||||
$request = [ | $request = [ | ||||
'customer' => $customer_id, | 'customer' => $customer_id, | ||||
'cancel_url' => Utils::serviceUrl('/wallet'), // required | 'cancel_url' => Utils::serviceUrl('/wallet'), // required | ||||
'success_url' => Utils::serviceUrl('/wallet'), // required | 'success_url' => Utils::serviceUrl('/wallet'), // required | ||||
'payment_method_types' => ['card'], // required | 'payment_method_types' => ['card'], // required | ||||
'locale' => 'en', | 'locale' => 'en', | ||||
'mode' => 'setup', | 'mode' => 'setup', | ||||
]; | ]; | ||||
// Note: Stripe does not allow to set amount for 'setup' operation | |||||
// We'll dispatch WalletCharge job when we receive a webhook request | |||||
$session = StripeAPI\Checkout\Session::create($request); | $session = StripeAPI\Checkout\Session::create($request); | ||||
$payment = [ | $payment['amount'] = 0; | ||||
'id' => $session->setup_intent, | $payment['id'] = $session->setup_intent; | ||||
'type' => self::TYPE_MANDATE, | $payment['type'] = self::TYPE_MANDATE; | ||||
]; | |||||
$this->storePayment($payment, $wallet->id); | $this->storePayment($payment, $wallet->id); | ||||
return [ | return [ | ||||
'id' => $session->id, | 'id' => $session->id, | ||||
]; | ]; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 256 Lines • ▼ Show 20 Lines | public function webhook(): int | ||||
$status = self::STATUS_PAID; | $status = self::STATUS_PAID; | ||||
break; | break; | ||||
default: | default: | ||||
$status = self::STATUS_FAILED; | $status = self::STATUS_FAILED; | ||||
} | } | ||||
if ($status == self::STATUS_PAID) { | if ($status == self::STATUS_PAID) { | ||||
$payment->wallet->setSetting('stripe_mandate_id', $intent->id); | $payment->wallet->setSetting('stripe_mandate_id', $intent->id); | ||||
// Update the balance, if it wasn't already | |||||
if ($payment->wallet->balance < 0 && $payment->status != self::STATUS_PAID) { | |||||
\App\Jobs\WalletCharge::dispatch($payment->wallet); | |||||
} | |||||
} | } | ||||
$payment->status = $status; | $payment->status = $status; | ||||
$payment->save(); | $payment->save(); | ||||
break; | break; | ||||
default: | default: | ||||
▲ Show 20 Lines • Show All 100 Lines • Show Last 20 Lines |