Reputation: 21
I created customer and product in stripe. I create paymentIntent, invoiceItems and invoice for this customer. How i can connect invoice and payment? My controller:
//check user in stripe
$stripeCustomer = $this->stripe->customers->search([
'query' => "email:'$user->email'",
]);
if (isset($stripeCustomer['data']) && !count($stripeCustomer['data']) ) {
//create new user
$stripeCustomer = $this->stripe->customers->create([
'email' => $user->email,
'name' => $user->name
]);
$stripeCustomerId = $stripeCustomer->id ?: 0;
} else {
$stripeCustomerId = $stripeCustomer['data'][0]->id;
}
$invoiceItems = $this->stripe->invoiceItems->create([
'customer' => $stripeCustomerId,
'price' => $product ? $product->stripe_price_id : null,
]);
//create draft invoice
$invoice = $this->stripe->invoices->create([
'customer' => $stripeCustomerId,
]);
//create payment
$paymentIntent = $this->stripe->paymentIntents->create([
'customer' => $stripeCustomerId,
'amount' => $invoiceItems->amount,
'currency' => Payment::CURRENCY_EUR,
'payment_method_types' => ['card']
]);
$clientSecret = $paymentIntent->client_secret;
After submitting form (number card, etc...) I am confirmPayment in view:
const { error } = await stripe.confirmPayment({
elements,
confirmParams: {
// Make sure to change this to your payment completion page
return_url: "{{route('payment-success'), [ 'token' => $token ])}}",
},
});
My paymentSuccess method:
public function paymentSuccess($token)
{
$data = json_decode(base64_decode($token));
//$data->paymentId
// maybe here i must pay invoice for my paymentId???
}
Upvotes: 2
Views: 869
Reputation: 426
Invoices can be paid in two ways
Stripe automatically creates and then attempts to collect payment on invoices for customers on subscriptions according to your subscriptions settings. However, if you’d like to attempt payment on an invoice out of the normal collection schedule or for some other reason, you can do so as follows.
$this->stripe->invoices->finalizeInvoice(
$invoice->id,
[]
);
$this->stripe->invoices->pay(
$invoice->id,
[]
);
OR
You can manually send an invoice through email to your customer to pay. You can do so as follow
$this->stripe->invoices->finalizeInvoice(
$invoice->id,
[]
);
$this->stripe->invoices->sendInvoice(
$invoice->id,
[]
);
Upvotes: 1