Why is my stripe invoice showing 0 as total? - laravel

I am trying to create a one time charge with a pdf invoice with Laravel Cashier for stripe.
The form submit successfuly, however, the invoice total is 0 and there is no payment in the Stripe page of payment list.
I have created one product with multiple price.
I saved the price ids in a table with the product name.
public function purchase(Request $request, SubscriptionPlan $subscriptionPlan)
{
try {
auth()->user()->createOrGetStripeCustomer();
auth()->user()->updateStripeCustomer(['address' => ['country' => 'CA'],'preferred_locales' => ['fr-CA']]);
auth()->user()->updateDefaultPaymentMethod($request->paymentMethod);
auth()->user()->invoicePrice($subscriptionPlan->stripe_price_id, 1); // Product ID from stripe
} catch (\Exception $exception) {
\Log::debug($exception->getMessage());
return back()->with('error', $exception->getMessage());
}
return back()->with('success', 'Paiement effectué avec succès!');
}
Edit:
I found that there are as many "pending invoice items" as $0 invoices.
Pending invoice items

it sound likes a subscription with a trial period, where the first invoice is $0.
Did you specify a trial_period_days or trial_end when creating this subscription? If not, you can reach out to Stripe Support and give them the subscription ID to investigate further.

Related

How to cancel a Stripe subscription price change?

The customer, through the checkout page, subscribes to the product at "price 1". He has 3D confirmation on the card!!! He later states his desire to change the price to "price 2". I use the following code to implement this:
try {
$subscription->anchorBillingCycleOn()->swapAndInvoice($plan_id);
} catch (IncompletePayment $exception) {
return redirect()->route('cashier.payment', [$exception->payment->id, 'redirect' => route('subscription.success')]);
}
The subscription price is updated and the invoice is marked as "pending", and the customer is redirected to the payment confirmation page.
And then the customer changed his mind and did not confirm the payment. For example, he saw that he chose the wrong price. The subscription is considered inactive.
How can I cancel/delete the last invoice and return to the previous subscription price?

Laravel left join check if a conditon is greater than a count

I would like to check a limit in number of user payment where a limit is set on user table.
So i have the following database structure
table user
id,name,...,pay_limit
and payment table
table payment
id, user_id, payment_ref
So i have created the following code
$query = User::query();
//other stuff
$query->leftJoin('payment','payment.user_id','=','user.id')
//stuck
Am stuck on how to check if the totals of payments on a user is not greater than the user pay_limit
How can i check the above in a query
Simple with relations. Suppose payment model is Payment and payment amount in payment_amount column
class User extends Model{
public function payments()
{
return $this->hasMany(Payment::class);
}
public function getIsOverLimitedAttribute(): bool
{
//if you check amount
return $this->payments()->sum('payment_amount') > $this->pay_limit;
//or if you check count of payments
return $this->payments()->count() > $this->pay_limit;
}
public function scopeNoOverLimited($query){
return $query->withCount('payments')->having('payments_count', '<', $this->pay_limit);
}
}
And use
if($user->isOverLimited){
//do stuff
}
Or get not over limited users:
User::noOverLimited()->get();
In terms of performance (since you want to be able to return all such users), the best approach is to store a summary column in your users table... total_payment
You can update all current users maybe via a migration file as such:
$table->integer('total_payment')->unsigned()->nullable();
DB::update("update users set total_payment = ('select count(id) from payments where user_id = id')");
Then to get all such users, you can do:
User::whereRaw('total_payment > pay_limit')->get();
Then add a PaymentObserver to increase the total_payment on new successful payments.
If you don't have access to modify the table, you can still use a scope like, but this can be performance intensive if being run all the time such as on user login without caching:
public function scopeAboveLimit($query)
{
$id = $this->id;//user id
return $query->whereRaw("pay_limit < ('select count(id) from payments where user_id = $id')");
}

laravel cashier with stripe no monthly plan error

I am having trouble using laravel cashier with stripe new product and plan integration. So i created a product and within that product i created a plan. but when i submit the request to i get an error checked my database the user table contains the card information the subscription table is empty. How do integrate laravel cashier with stripe new system product & plan?
error:
No such plan: Monthly
here my code
public function store(Request $request)
{
$token = $request->stripeToken;
auth()->user()->newSubscription('main', 'Monthly')->create($token);
return 'Done';
}
You need to create a product on stripe(when creating it you can set the billing cycle). Once you have created the product on stripe it will then have a product ID. Instead of using the nickname you have assigned it on stripe you need to use the ID like so
$nickname = 'Monthly';
$plan_id = 'plan_xxxxxxxxxxx';
auth()->user()->newSubscription($nickname, $plan_id)->create($request->stripeToken);

Laravel Cashier list all user invoices

How to display all user invoices for referencing in the admin section of the application.
I can get a user invoices by
$userinvoices = $user->invoices();
Or I can get all invoice by stripe API:
$invoices = \Stripe\Invoice::all(array("limit" => 30));
in the second case, I can't get the details of the user the invoice belongs to.
Or is there any way to save invoice data to database on every creation of the invoice in stripe.
Your first option is the better way to go since you have all the information on the invoice and also the user info on the object.
If you want, you can go to your stripe dashboard -> Your account -> account settings -> webhooks -> add endpoint -> select events and select the invoiceitem.created event. setup your endpoint in the application and do whatever you need with it.
Example:
public function invoiceCreated(Request $request){
$payload = $request->all();
if($payload['type'] == 'invoiceitem.created'){
// do whatever you want with the $payload["data"]...
}
}
Good Luck :)

Creating An Invoice from an Order over the SOAP API

Im running into an issue when using the soap api to talk to magento that prevents me from creating an invoice from an order. The issue is in the sales_order_invoice.create call from my tool. When i call this one of the arguments passed in that call is the product id and the quantity to invoice, formatted in a nested array. For some reason no matter how i send this data to magento, magento will create the invoice with the amount as seen on the order but it dosent add any of the products to the invoice page. Its like its completely ignoring the itemQtys array. Also i cant figure out if i can change the quantity i want to invoice.
This is the call im using:
http://www.magentocommerce.com/wiki/doc/webservices-api/api/sales_order_invoice#sales_order_invoice.create
As an example imagine that customer places an order for some number of products but we only have a certain number on hand. I would like to invoice the number that we have on hand and ship that invoice then invoice the rest of the order at a later date. This of course needs to be done all "programmatically". Is this possible to do over the SOAP api? or in magento period?
Thanks.
if(!$order->getId()){
return;
}
try {
if(!$order->canInvoice())
{
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice.'));
}
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
if (!$invoice->getTotalQty()) {
Mage::throwException(Mage::helper('core')->__('Cannot create an invoice without products.'));
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
}
catch (Mage_Core_Exception $e) {
}
you can have some idea from the above code.

Resources