How to cancel a Stripe subscription price change? - laravel

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?

Related

Why is my stripe invoice showing 0 as total?

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.

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 :)

Magento coupon code applied, can I update order status to "Processing"?

When a customer pays, partially or fully, by coupon code the order status is set to "Pending Payment". We use a third party order management application which only pulls in orders with the status "Processing".
Normal orders are automatically set to "Processing", so it's only when a coupon code is used that we have a problem.
Is there a way of automatically updating the order status to "Processing" when a customers applies a coupon code?
Thanks for your help
(Magento Community 1.7)
I think this is possible, but you might have to combine it with creating an invoice - since the coupon amount covers the order value and the order is technically paid. I would create an observer to catch sales_order_place_after and use it for the following:
$order = $observer->getOrder();
/**
add code to check if the coupon amount covers the order value
*/
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice();
$invoice->register();
$invoice->getOrder()->setIsInProcess(true);
$invoice->getOrder()->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, '', false);
$transactionSave = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transactionSave->save();
$invoice->getOrder()->save();

Magento: showing prices ex/inc TAX/VAT depending on customer group

Previously I have implemented a 'trade only' store front that shows both in and ex-VAT prices for trade customers. They still get billed the VAT, it is just the catalog shows ex-VAT prices as well as the regular inc VAT prices.
I would like to be able to implement the same functionality without creating a new store front, i.e. if someone is in the 'trade' customer group, they get prices shown inc and ex vat. They are still on the same tax rate as everyone else, so I am not looking to have a 0% tax group, what I want is just to be able to switch the prices based on their group. This also includes the labels, so there isn't just a price but a clear indication of inc/ex VAT/TAX.
It took me a while to Google this with 'tax' instead of 'VAT', however, to date I haven't found many clues as to where to start. If there is a reason why this cannot be done easily then I would like to know. Failing that, if there is a frontend hack to try, e.g. some conditional prototype to un-css-hide the prices/labels then that will have to be the route to go.
EDIT
Inspired by Clockworkgeek I did this, not yet the perfect module solution, but something that works for me for now:
Cloned core file to app/code/local/Mage/Tax/Model/Config.php and updated the getPriceDisplayType function:
public function getPriceDisplayType($store = null)
{ $customer = Mage::helper('customer')->getCustomer();
if ($customer->getGroupId() > 1) {
return self::DISPLAY_TYPE_BOTH;
} else {
return (int)Mage::getStoreConfig(self::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE, $store);
}
}
This relies on 0 being not logged in, 1 being normal customers and any 'special' group higher than that. I did not think it was working at first, but then I logged in...
There is a config setting for whether to include or exclude tax but it is not customer-specific. I believe the most direct way would be to override the point where this is read. The following is pseudocode...
class Your_Module_Model_Config extends Mage_Tax_Model_Config
{
public function getPriceDisplayType($store = null)
{
$customer = Mage::helper('customer')->getCustomer();
if ($customer->getGroupId() == 'TRADE') {
return self::DISPLAY_TYPE_BOTH;
}
return parent::getPriceDisplayType($store);
}
}
You can try this to show prices without tax only for specified customer groups:
http://www.magentocommerce.com/magento-connect/catalog/product/view/id/18364/

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