How to describe line items in Stripe checkout? - php

This is the way how I create checkout function:
foreach($request->arrayOfObjects as $req) {
array_push($lineItems, [
'price' => $req['priceID'],
'quantity' => (int) $req['quantity']
]);
}
if (Auth::check()) {
$charge = $stripeClient->checkout->sessions->create([
'payment_method_types' => [
'card',
'sepa_debit',
'giropay',
'sofort',
'alipay'
],
'success_url' => env('APP_URL').'/success',
'cancel_url' => env('APP_URL').'/cancel',
'shipping_address_collection' => [
'allowed_countries' => ['DE'],
],
'shipping_options' => [
[
'shipping_rate' => $request->lieferung ? env('SHIPPING_RATE_LIEFERUNG') : env('SHIPPING_RATE_ABHOLUNG')
],
],
'line_items' => [$lineItems],
'mode' => 'payment',
'metadata' => [
'lieferung' => $request->lieferung,
'isCustomer' => true
],
'allow_promotion_codes' => true,
'customer' => Auth::user()->stripe_id
]);
}
It works fine though... However, I'd like to know is there way to describe measurements of an article?
For example I have ordered 10 packages of Parquet Flooring Classic and each package has 3m2 of parquet.
I'd like to see it in the bill, like:
Article 1001 - Parquet Flooring Classic (10x Packages - 3m2 each)
I found that metadata could be provided but only for the global checkout level, not per item.
Is there any way to do this?

You can provide metadata per item using Stripe Checkout. Here's an example:
$charge = $stripeClient->checkout->sessions->create([
...
'line_items' => [
// item 1
[
'price_data' => [
'product_data' => [
'name' => 'Item A',
],
// 10.99 USD
'currency' => 'usd',
'unit_amount' => 1099,
],
'quantity' => 3,
],
// item 2
[
'price_data' => [
'product_data' => [
'name' => 'Item B',
],
// 5.49 USD
'currency' => 'usd',
'unit_amount' => 549,
],
'quantity' => 2,
],
],
...
]);
At checkout, it will give you a neat list of
3 x Item A ....... 32.97 $ USD
2 x Item B ....... 10.98 $ USD
Check all the information on line_items in The stripe API documentation (click on the 'show child attributes' button for all the possible keys and what they're for.)

Related

Stripe PHP Redirect after Purchase

I just want to add a redirect to a certain url after purchase by using the Stripe API for PHP. My approach so far is:
$link = $stripe->paymentLinks->create([
'line_items' => [
[
'price' => $product['default_price'],
'quantity' => 1,
],
],
'after_completion' => [
[
'redirect' => [
'url' => 'https://www.trainer-va.de/Trainer-Cloud/123.php?product=' . $product['default_price'] . '',
],
'type' => 'redirect'
],
]);
but it's not working. Any hints what's going wrong here?
Thanks in advance!
Your request body is malformatted at the after_completion attribute level[1].
You should do like this instead:
$link = $stripe->paymentLinks->create([
'line_items' => [
[
'price' => $product['default_price'],
'quantity' => 1,
],
],
'after_completion' => [
'redirect' => [
'url' => 'https://www.trainer-va.de/Trainer-Cloud/123.php?product=' . $product['default_price'] . '',
],
'type' => 'redirect'
],
]);
[1] https://stripe.com/docs/api/payment_links/payment_links/create#create_payment_link-after_completion

Stripe creating Checkout session throws error ( Stripe\Exception\InvalidRequestException Invalid array )

I've been trying to create a checkout session for a payment which should be directed in connected account.
whenever I'm trying to create a session using the code below I get InvalidRequestException saying Invalid array
Here's my code below,
\Stripe\Stripe::setApiKey(env('STRIPE_SECRET'));
$stripe = new \Stripe\StripeClient(env('STRIPE_SECRET'));
$session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [
'price_data' => [
'unit_amount' => 25000,
'currency' => 'usd',
'product_data' => ['name' => 'Product8', 'active' => true],
],
'quantity' => 2,
],
'mode' => 'payment',
'success_url' => 'http://devweb.drivinggradebook.com/',
'cancel_url' => 'https://www.drivinggradebook.com/',
'payment_intent_data' => [
'application_fee_amount' => 10,
],
], ['stripe_account' => 'acct_1L7ugjSJzLhcy6eF']);
Please help me out with it,
Thanks
In my case I have it running as follows with laravel 8.75 and stripe/stripe-php 10.1
public function checkout(Request $request)
{
\Stripe\Stripe::setApiKey(STRIPE_SECRET);
header('Content-Type: application/json');
$checkout_session = \Stripe\Checkout\Session::create([
'line_items' => [
[
'price_data' => [
'currency' => 'eur',
'product_data' => [
'name' => 'Home'
],
'unit_amount' => 500
],
'quantity' => 1
],
],
'mode' => 'payment',
'success_url' => url("/stripe_success?session_id={CHECKOUT_SESSION_ID}&tenant_id=$request->id"),
'cancel_url' => route('payments.index'),
]);
return redirect()->away($checkout_session->url);
}

Stripe checkout: description in dashboard instead of pi

Can anyone help me? I migrate form v2 to v3 checkout.
how can I send my custom description order in stripe dashboard description column?
now I get only the payment id pi_1IrhQALKfdoxxl3X07seJ5anto
with old API by description I would do:
$charge = \Stripe\Charge::create(array(
"amount" => $_POST['amount'],
"currency" => "EUR",
"description" => "Order #".$_POST["order"],
"source" => $token,
));
with the new API :
$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
]);
Thank you
according to the stripe documentation : https://stripe.com/docs/api/checkout/sessions/create#create_checkout_session-payment_intent_data-description
$stripe->checkout->sessions->create([
'success_url' => 'https://example.com/success',
'cancel_url' => 'https://example.com/cancel',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => 'price_H5ggYwtDq4fbrJ',
'quantity' => 2,
],
],
'mode' => 'payment',
'payment_intent_data' => [
'description' => "Order #".$_POST["order"]
]
]);

Etsy API PHP, how to create custom property_values

Im using Etsy API (PHP) from : https://github.com/gentor/etsy-php-laravel.
Im having problems trying to create a listing which contains these custom attributes : "color","height". There is an example code but i don't know how to get the "property_id" field from my custom attributes, also i have no clue how to add these custom attributes to my list.
I did read their developer document but still can't figure out anything myself, been 2 days and they keep locking my accounts from here means i can't test my code much.
Thanks in advance, here is example code i tried:
dd($conn->updateInventory(
[
'params' => [
'listing_id' => '710642930'
],
'data' => [
'products' => [
'json' => json_encode([
[
'sku' => 'sku-1',
'property_values' => [
[
'property_id' => 2000,
'property_name' => 'color',
'values' => 'red'
],
[
'property_name' => 'height',
'property_id' => 5000,
'value' => '57 cm'
]
],
'offerings' => [
[
'price' => 10,
'quantity' => 3
]
]
],
[
'sku' => 'sku-2',
'property_values' => [
[
'property_name' => 'color',
'property_id' => 2000,
'value' => 'red'
],
[
'property_name' => 'height',
'property_id' => 5000,
'value' => '68 cm'
]
],
'offerings' => [
[
'price' => 11,
'quantity' => 4
]
]
],
[
'sku' => 'sku-3',
'property_values' => [
[
'property_name' => 'color',
'property_id' => 2000,
'value' => 'blue'
],
[
'property_name' => 'height',
'property_id' => 5000,
'value' => '57 cm'
]
],
'offerings' => [
[
'price' => 12,
'quantity' => 5
]
]
],
[
'sku' => 'sku-4',
'property_values' => [
[
'property_name' => 'color',
'property_id' => 2000,
'value' => 'blue'
],
[
'property_name' => 'height',
'property_id' => 5000,
'value' => '68 cm'
]
],
'offerings' => [
[
'price' => 14,
'quantity' => 6
]
]
],
])
],
'price_on_property' => [2000, 5000],
'quantity_on_property' => [2000, 5000],
'sku_on_property' => [2000, 5000],
],
]));
The property ids that you are looking for are what Etsy considers their "Structured Data". You will want to make an API call using the getTaxonomyNodeProperties method. This method takes a taxonomy id representing the taxonomy of the product you are trying to list and will return a list of available TaxonomyNodeProperty objects that describe the options that are available to set up your listings variations.
If I recall correctly, height is in there (with its ID) and color is in there as 'Primary color' with its own ID. Keep in mind however that some of these structured node properties have a list of 'possible values' where you will be limited in the values you can supply. I remember that primary color has a specific list of possible values that you are limited to.
If you find that your colors are not in the list, then the list of pre-defined taxonomy node properties usually does include two 'custom' properties that can be used to make variations of any type at all. If you use any of the custom options, then you just use the pre-defined IDs of the custom properties.
Hope that helps.

RBAC in YII2: user->can() in PHPManager

I am trying to configure rbac with phpmanager in my project using the Yii2 advance app version. But \Yii::$app->user->can is not returning the expected.
I wrote the RbacController and executed sucessfully yii rbac/init
That updated common/components/items.php as shown
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => 'create a X',],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX => [ 'type' => 2, 'description' => 'update a X', ],
];
In SignupForm::signup, I added it:
$auth = Yii::$app->authManager;
$roleObj = $auth->getRole('user'); // this role is defined by the RBAC Controller's init action
$auth->assign($roleObj, $user->getId());
assignments.php
return [ 2 => [ 'user', ], ];
I think that 2 corresponds to the user id.
rules.php
return [];
common/main.php
...
'components' => [
...
'authManager' => [
'class' => 'yii\rbac\PhpManager',
'defaultRoles' => ['user','admin'],
'itemFile' => '#common/components/rbac/items.php',
'assignmentFile' => '#common/components/rbac/assignments.php',
'ruleFile' => '#common/components/rbac/rules.php'
],
],
...
When I got that role permissions, it prints:
Array ( [createX] => yii\rbac\Permission Object ( [type] => 2 [name] => createX [description] => create a X [ruleName] => [data] => [createdAt] => 1438601819 [updatedAt] => 1438601819 ) )
So I'm expecting that user doesn't have updating permission, but in the method XController::update
echo \Yii::$app->user->can('updateX');
// returns 1, just the same than \Yii::$app->user->can('createX') returning
Please some help
First items file should be:
<?php
return [
'user' => [ 'type' => 1, 'children' => [ 'createX', ], ],
'createX' => [ 'type' => 2, 'description' => [ 'create a X', ], ],
'admin' => [ 'type' => 1, 'children' => [ 'updateX', ], ],
'updateX' => [ 'type' => 2, 'description' => ['update a X', ], ],
];
You can verify if the code is fine with var_dump:
var_dump(\Yii::$app->authManager);

Categories