Skip to content

Instantly share code, notes, and snippets.

@pipethedev
Created October 2, 2023 22:06
Show Gist options
  • Save pipethedev/73c273d54a367136679786b9075ba0c8 to your computer and use it in GitHub Desktop.
Save pipethedev/73c273d54a367136679786b9075ba0c8 to your computer and use it in GitHub Desktop.
public function name(): string {
return PaymentMethod::STRIPE;
}
protected function createPaymentIntent(string $setupIntentId, $user, $transaction): array
{
$setupIntent = SetupIntent::retrieve($setupIntentId);
$paymentMethodId = $setupIntent->payment_method;
$paymentMethod = StripePaymentMethod::retrieve($paymentMethodId);
$customer = StripeCustomer::where('admin_id', $user['_id'])->where('environment', $this->environment)->first();
$paymentMethod->attach(['customer' => $customer->{'stripe_customer'} ]);
$stripeCustomer = Customer::retrieve($customer->{'stripe_customer'});
$stripeCustomer->invoice_settings = [
'default_payment_method' => $paymentMethodId,
];
$stripeCustomer->save();
$authorizedCard = array(
'card_type' => $paymentMethod->card->brand,
'last4' => $paymentMethod->card->last4,
'exp_month' => $paymentMethod->card->exp_month,
'exp_year' => $paymentMethod->card->exp_year,
'authorization_code' => Crypt::encryptString($paymentMethodId),
'provider' => $this->name(),
);
$payment = PaymentIntent::create([
'payment_method' => $paymentMethodId,
'amount' => $transaction['amount'] * 100,
'currency' => 'usd',
'customer' => $customer->{'stripe_customer'},
'confirm' => true,
'setup_future_usage' => 'off_session',
'receipt_email' => $user['email'],
'metadata' => [
'payment_reference' => $transaction->{'reference'}
],
]);
$isSuccessful = in_array($payment->status, TransactionAction::CHARGE_SUCCESS);
return array(
'status' => $isSuccessful,
'card' => $isSuccessful ? $authorizedCard : NULL
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment