Stripe invoice items not adding to pending invoice - api

I have an existing customer in Stripe and we plan to add a subscription to the customer. First I want to add an invoice item so the subscription picks it up when created and adds it to the pending invoice. The code below is run in order, however, the invoice item in stripe is shown as paid immediately as opposed to added to the pending invoice on the subscription. The docs say I have it correct as far as I can tell, any idea why the invoice item is not added to the pending invoice?
try {
\Stripe\InvoiceItem::create(
array(
"customer" => $customer['stripe_customer_id'],
"amount" => $invoice_item_amount,
"currency" => "usd",
"description" => $product['description']
)
);
} catch(Error $e) {
// do something
}
try {
$result = $stripe_customerObj->subscriptions->create(
array(
"coupon" => $coupon,
"plan" => $plan_id,
"quantity" => $quantity,
"trial_end" => $trial_end_timestamp,
"metadata" => $metadata
)
);
} catch(Error $e) {
// do something
}
It worked when I moved the InvoiceItem::create to after the creation of the subscription.

This is expected behaviour. The code you have creates an invoice item and then a subscription with a trial. That trial creates a $0 invoice that automatically picks up the invoice item(s) pending including the one you just created. This is the flow you'd use if you wanted to charge a fee for a trial period for example.
If you want the pending invoice item added to the next invoice, you'd create it after the subscription instead.

Related

Find Payment links by the Price ID

I created Stripe products, and prices and added payment links to each of them. I am going to use these Payment Links in the future, show them on the site, etc. The PHP backend pulls products, prices, and payment links and shows a pretty formatted picture for the user.
I successfully get products and prices. I didn't find how the PaymentLink API can seek payment links by the price ID. I looked inside the Stripe dashboard and found that it requests payment links in the following way.
https://dashboard.stripe.com/v1/payment_links?include_only[]=data.id&price=price_1MWn4sEnbft6LZBGeHuaLVwf&active=true&limit=2
You can see that it passes the "price" parameter. In the same way, I want to find payment links.
My code
<?php
require_once __DIR__.'/../vendor/autoload.php';
$data = [];// Collected data
$stripe
= new \Stripe\StripeClient('sk_test_');
$products = $stripe->products->all([
'active' => true,
'limit' => 100
]);
foreach ($products as $product) {
$prices = $stripe->prices->all([
'active' => true,
'product' => $product->id,
'limit' => 100,
]);
foreach ($prices as $price) {
$payment_links = $stripe->paymentLinks->all([
'limit' => 100,
// Stripe doesn't have such an option.
//'price' => $price->id
]);
}
// Put found info into the $data var
}
print_r($data);
There is no direct way of searching for PaymentLinks with a certain price ID as you mentioned. The only way is to retrieve a Payment Link's line_items and search whether an item contains the price you're searching for.

Google Adwords saving Server Side PHP conversion Tracking through Measurement Protocol

I am trying to track customer checkout and purchase through "TheIconic\Tracking\GoogleAnalytics" using shopify google hook. I set up webhook on shopify end so when a customer checkout and purchase it will call particular php script(checkout.php and purchase.php). I was curious if I set up this script in the seperate php file how google analytics knows the same person checkout and purchase products.
$analytics
->setProtocolVersion('1')
->setTrackingId('UA-25099702-3')
->setClientId($productList -> token);
foreach( $productList -> line_items as $item ){ $product = [
'sku' => $item -> sku, 'variant' => $item -> variant_id, 'name' => $item -> title, 'price' => $item -> price, 'quantity' => $item -> quantity, ]; $analytics->addProduct($product); } // Don't forget to set the product action, in this case to PURCHASE
$analytics->setProductActionToCheckout();
// Finally, you must send a hit, in this case we send an Event
$analytics
->setEventCategory('Checkout')
->setEventAction('Checkout')
->sendEvent();
$analytics->sendPageview();

Prestashop Add custom column called "number of products", displaying quantity ordered from "ps_order" table

I am trying to find a way to display product ordered quantity in Orders Page, in back-end of a Prestashop v.1.6.1.9 installation.
I already managed to add 2 custom columns by overriding AdminOrdersController.php. I have added phone_mobile and custom notes in this manner:
$this->fields_list['phone_mobile'] = array(
'title' => $this->l('Phone Number')
);
$this->fields_list['note'] = array(
'title' => $this->l('Notes')
);
Any way I can override this file to show the quantity ordered?
First of all let me clear one thing; quantity ordered is not getting stored in {DB_PREFIX}order table; it is stored in {DB_PREFIX}order_detail table.
To add total_qty total quantities ordered you need to get quantity from {DB_PREFIX}order_detail table and to achieve this you can do below things in your override.
<?php
/**
* #override AdminOrdersController
*/
class AdminOrdersController extends AdminOrdersControllerCore
{
public function __construct()
{
parent::__construct();
$this->_select .= ', (SELECT SUM(od.product_quantity) FROM `'._DB_PREFIX_.'order_detail` od WHERE od.id_order = a.id_order GROUP BY od.id_order) as total_qty';
$this->fields_list = array_merge($this->fields_list, array(
'total_qty' => array(
'title' => $this->l('Number of products'),
'havingFilter' => true,
),
));
}
}
You can add your fields like phone_mobile and custom_notes accordingly.
Hope it helps!

Shopify Order API - Passing Discount

I'm getting some very strange results from Shopify API and I'm hoping someone can help me out.
I'm trying to create an order, with a discount. Its actually saving the order, with the discount information... however the amount is ALWAYS wrong
order_params = {
:browser_ip => webhook[:browser_ip],
:buyer_accepts_marketing => webhook[:buyer_accepts_marketing],
:currency => webhook[:currency],
:email => webhook[:email],
:financial_status => webhook[:financial_status],
:landing_site => webhook[:landing_site],
:note => webhook[:note],
:referring_site => webhook[:referring_site],
:line_items => line_items,
:tag => tags,
:transactions => transactions,
:discount_codes => webhook[:discount_codes],
:total_discounts => webhook[:total_discounts],
:shipping_address => webhook[:shipping_address],
:shipping_lines => webhook[:shipping_lines],
:customer_id => #options[:customer_id],
:billing_address => webhook[:billing_address]
}
#shopify_order = ShopifyAPI::Order.create(order_params)
As you can see its created from webhook data. This is giving me back...(truncated)
"reference"=>nil,
"user_id"=>nil,
"subtotal_price"=>"55.00",
"total_discounts"=>"55.00",
"location_id"=>nil,
"source_identifier"=>nil,
"source_url"=>nil,
"processed_at"=>"2017-05-31T15:53:03-04:00",
"device_id"=>nil,
"phone"=>nil,
"browser_ip"=>nil,
"landing_site_ref"=>nil,
"order_number"=>1140,
"discount_codes"=>
[#<ShopifyAPI::Order::DiscountCode:0x007ffbec42ccb0
#attributes={"code"=>"50% OFF", "amount"=>"55.00", "type"=>""},
#persisted=true,
#prefix_options={}>]
So far, so good all data is correct.. then I save and this happens.. the amount discounted is incorrect... it should be £55.
I haven't dug too deep into this, but I'm pretty sure the code needs to be unique.
i.e. you probably already have a Discount Code with the same code 50% OFF with amount: 60.50 defined somewhere else on that shop.
Try using a new, unique discount code and testing with that.
I would suggest creating a new discount code like 50OFF that has amount: 50 with type: percentage, then you can re-use that for orders with a 50% discount.
See the Price Rules reference for more information >

Bigcommerce API Tracking Number Create PHP

I am trying to update the tracking number of an order in Bigcommerce using the API. This is the code I am using:
//update BC of order status
$filter = array('status_id' => 2);
$order_status_update = BigCommerce::updateResource('/orders/' . 105, $filter);
$order = Bigcommerce::getOrder(105);
foreach($order->products as $shipment)
{
$filter = array(
'order_address_id' => $shipment->order_address_id,
'items'=> array(
'order_product_id' => $shipment->product_id,
'quantity' => $shipment->quantity
),
'tracking_number' => 'tracking number'
);
$add_tracking = BigCommerce::createResource('/orders/105/shipments', $filter);
var_dump($add_tracking);
}
I have followed the instructions from here:
https://developer.bigcommerce.com/api/stores/v2/orders/shipments#list-shipments
BigCommerce Uploading Tracking Numbers
But I can't seem to get it to work. Can someone help?
In advance, thanks for your help!
Akshay
The payload for creating a shipment is invalid due to the items field needing to be formatted as an object array and the use of the product_id as opposed to the ID of the product within the order.
The code provided is attempting to create one shipment per product in the order, is this intended? Ideally you would ship all items in one shipment, which is why the items field is meant to be an array of product objects.
Additionally, by creating a shipment for an order the order status will automatically change to "Shipped", so the first PUT request is unnecessary.
If you were trying to create a single shipment per order and are assuming no orders will have multiple addresses then this code should work.
$order = Bigcommerce::getOrder(105);
$filter = array(
'order_address_id' => $order->shipping_addresses[0]->id,
'tracking_number' => '123456'
);
foreach($order->products as $product) {
$items[] = array(
'order_product_id' => $product->id,
'quantity' => $product->quantity
);
}
$filter['items'] => $items;
$add_tracking = BigCommerce::createResource('/orders/105/shipments', $filter);
var_dump($add_tracking);