Changeset View
Changeset View
Standalone View
Standalone View
src/app/Providers/Payment/Stripe.php
Show All 23 Lines | class Stripe extends \App\Providers\PaymentProvider | ||||
* Get a link to the customer in the provider's control panel | * Get a link to the customer in the provider's control panel | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet | * @param \App\Wallet $wallet The wallet | ||||
* | * | ||||
* @return string|null The string representing <a> tag | * @return string|null The string representing <a> tag | ||||
*/ | */ | ||||
public function customerLink(Wallet $wallet): ?string | public function customerLink(Wallet $wallet): ?string | ||||
{ | { | ||||
$customer_id = self::stripeCustomerId($wallet); | $customer_id = self::stripeCustomerId($wallet, false); | ||||
if (!$customer_id) { | |||||
return null; | |||||
} | |||||
$location = 'https://dashboard.stripe.com'; | $location = 'https://dashboard.stripe.com'; | ||||
$key = \config('services.stripe.key'); | $key = \config('services.stripe.key'); | ||||
if (strpos($key, 'sk_test_') === 0) { | if (strpos($key, 'sk_test_') === 0) { | ||||
$location .= '/test'; | $location .= '/test'; | ||||
} | } | ||||
Show All 16 Lines | class Stripe extends \App\Providers\PaymentProvider | ||||
* - 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); | $customer_id = self::stripeCustomerId($wallet, true); | ||||
$request = [ | $request = [ | ||||
'customer' => $customer_id, | 'customer' => $customer_id, | ||||
'cancel_url' => \url('/wallet'), // required | 'cancel_url' => \url('/wallet'), // required | ||||
'success_url' => \url('/wallet'), // required | 'success_url' => \url('/wallet'), // required | ||||
'payment_method_types' => ['card'], // required | 'payment_method_types' => ['card'], // required | ||||
'locale' => 'en', | 'locale' => 'en', | ||||
'mode' => 'setup', | 'mode' => 'setup', | ||||
▲ Show 20 Lines • Show All 94 Lines • ▼ Show 20 Lines | class Stripe extends \App\Providers\PaymentProvider | ||||
*/ | */ | ||||
public function payment(Wallet $wallet, array $payment): ?array | public function payment(Wallet $wallet, array $payment): ?array | ||||
{ | { | ||||
if ($payment['type'] == self::TYPE_RECURRING) { | if ($payment['type'] == self::TYPE_RECURRING) { | ||||
return $this->paymentRecurring($wallet, $payment); | return $this->paymentRecurring($wallet, $payment); | ||||
} | } | ||||
// Register the user in Stripe, if not yet done | // Register the user in Stripe, if not yet done | ||||
$customer_id = self::stripeCustomerId($wallet); | $customer_id = self::stripeCustomerId($wallet, true); | ||||
$request = [ | $request = [ | ||||
'customer' => $customer_id, | 'customer' => $customer_id, | ||||
'cancel_url' => \url('/wallet'), // required | 'cancel_url' => \url('/wallet'), // required | ||||
'success_url' => \url('/wallet'), // required | 'success_url' => \url('/wallet'), // required | ||||
'payment_method_types' => ['card'], // required | 'payment_method_types' => ['card'], // required | ||||
'locale' => 'en', | 'locale' => 'en', | ||||
'line_items' => [ | 'line_items' => [ | ||||
▲ Show 20 Lines • Show All 181 Lines • ▼ Show 20 Lines | public function webhook(): int | ||||
return 200; | return 200; | ||||
} | } | ||||
/** | /** | ||||
* Get Stripe customer identifier for specified wallet. | * Get Stripe customer identifier for specified wallet. | ||||
* Create one if does not exist yet. | * Create one if does not exist yet. | ||||
* | * | ||||
* @param \App\Wallet $wallet The wallet | * @param \App\Wallet $wallet The wallet | ||||
* @param bool $create Create the customer if does not exist yet | |||||
* | * | ||||
* @return string Stripe customer identifier | * @return string|null Stripe customer identifier | ||||
*/ | */ | ||||
protected static function stripeCustomerId(Wallet $wallet): string | protected static function stripeCustomerId(Wallet $wallet, bool $create = false): ?string | ||||
{ | { | ||||
$customer_id = $wallet->getSetting('stripe_id'); | $customer_id = $wallet->getSetting('stripe_id'); | ||||
// Register the user in Stripe | // Register the user in Stripe | ||||
if (empty($customer_id)) { | if (empty($customer_id) && $create) { | ||||
$customer = StripeAPI\Customer::create([ | $customer = StripeAPI\Customer::create([ | ||||
'name' => $wallet->owner->name(), | 'name' => $wallet->owner->name(), | ||||
// Stripe will display the email on Checkout page, editable, | // Stripe will display the email on Checkout page, editable, | ||||
// and use it to send the receipt (?), use the user email here | // and use it to send the receipt (?), use the user email here | ||||
// 'email' => $wallet->id . '@private.' . \config('app.domain'), | // 'email' => $wallet->id . '@private.' . \config('app.domain'), | ||||
'email' => $wallet->owner->email, | 'email' => $wallet->owner->email, | ||||
]); | ]); | ||||
▲ Show 20 Lines • Show All 70 Lines • Show Last 20 Lines |