Attach metadata to Stripe Checkout Subscription - php

I'm trying to use Stripe Checkout for a subscription service.
Using the PHP SDK
Session::create([
'customer' => $user->stripeCustomerId,
'payment_method_types' => ['card'],
'line_items' => [[
'price' => 'price_0MACBYAGG6RS7KP5c1fNa6v9',
'quantity' => $amount,
]],
'subscription_data' => [
'metadata' => [
'message' => $message,
],
],
'mode' => 'subscription',
'success_url' => UrlHelper::url('?session_id={CHECKOUT_SESSION_ID}'),
'cancel_url' => UrlHelper::url('?cancel=true'),
]);
but metadata is never attached as per the docs https://support.stripe.com/questions/using-metadata-with-checkout-sessions?locale=en-GB
I have a single charge option that passes:
'payment_intent_data' => [
'metadata' => [
'message' => $message,
],
],
and that attaches perfectly. What am I missing?

Using subscription_data[metadata] (API ref) as you've done is the way this is intended to work. Have you confirmed in your request logs that the metadata was included in your request as you expect? It's possible your $message might be empty in the subscription case.
If your Checkout session request did include the subscription_data metadata as expected, then you should also see this on the subscription object after the customer completes the Checkout session. Where are you looking at the resulting subscription which makes you conclude that it does not have metadata?

Turns out that I was looking in the wrong place. The meta data is not attached to each payment but on the actual subscription itself.

Related

How to retrieve stripe session id php

I hate to ask such a simple question on here but I can't seem to figure this out. I create a stripe checkout session that goes through successfully, but am having trouble getting a response. I have read through stripe docs and answers on here, but still cannot seem to get the stripe session id returned. Here's what I have tried
<?php
require_once('stripe-php-7.86.0/init.php');
$priceId = $_POST['priceId'];
$stripe = new \Stripe\StripeClient(
'my secret key here'
);
$stripe->checkout->sessions->create([
'success_url' => 'http://localhost/stripe_test/success.html',
'cancel_url' => 'http://localhost/stripe_test/cancel.html',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'mode' => 'subscription',
]);
// I've tried all of these individually, none of them work.
echo json_encode($stripe);
echo $stripe['id'];
echo json_encode($stripe['id']);
echo $stripe->checkout->sessions['id'];
echo json_encode(['sessionId' => $stripe['id']]);
As per the official Stripe documentation, you can to assign the returned value of $stripe->checkout->sessions->create to the variable you want to use, then access the property id.
$stripe->checkout->sessions->create returns the Session object
For example
$stripe_session = $stripe->checkout->sessions->create([
'success_url' => 'http://localhost/stripe_test/success.html',
'cancel_url' => 'http://localhost/stripe_test/cancel.html',
'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $priceId,
'quantity' => 1,
],
],
'mode' => 'subscription',
]);
echo $stripe_session->id;
I think response->id will be you session id

Stripe API and ApplePay "Billing Address Invalid" issue

I am using this code to create Stripe checkout:
$checkout_session = \Stripe\Checkout\Session::create([
'customer_email' => 'my_test_email#gmail.com',
'payment_method_types' => ['card'],
'line_items' => [[
'price_data' => [
'currency' => 'usd',
'unit_amount' => 1000,
'product_data' => [
'name' => 'Balance top-up',
],
],
'quantity' => 1,
]],
'mode' => 'payment',
'client_reference_id' => $clientReferenceId,
'success_url' => 'https://example.com/user-deposit/?result=success',
'cancel_url' => 'https://example.com/user-deposit/?result=failed',
'metadata' => [
'user_id' => '1',
],
]);
Everything works fine, but if I try to pay with ApplePay from the mobile browser - I got such an error (although everything is fine with this card and the billing address is filled in in the Apple account settings):
What should I do? In fact, I don't want to ask my users for the billing address, could I use some stored info (like country and city) to pass to this API request, or perhaps I could somehow force Stripe to do not ask for billing/shipping addresses (I do not sell goods, just for top-up an internal balance in my service)?

Stripe Checkout subscription mode does not return a PaymentIntent

I'm using the PHP client SDK, but I'm guessing this is the behavior in general.
After creating a Stripe Checkout Session in 'subscription' mode, the session that gets returned is missing the payment_intent field.
The 'subscription' field in the created Checkout Session object is also null.
Is this intentional? Am I doing something wrong?
Here is the object I pass on to Stripe SDK for Checkout Session creation:
$session_data = array(
'success_url' => $success_url,
'cancel_url' => $cancel_url,
'payment_method_types' => ['card'],
'customer' => $customer->id,
'line_items' => $line_items,
'mode' => 'subscription'
);

WooCommerce REST API v3 remote ordering and payment and sending all data into WP

I started creating a program where we can browse products by category. Add items to our cart (in messenger) and update cart etc.
We have Stripe / PayPal already connected to our App and would like to handle everything on our end, in the app. Then, when the order is completed, send the entire order as the data array into WP.
A few questions since I'm stuck.
I'd use this part from the API docs:
https://woocommerce.github.io/woocommerce-rest-api-docs/?php#create-an-order
<?php
$data = [
'payment_method' => 'bacs',
'payment_method_title' => 'Direct Bank Transfer',
'set_paid' => true,
'billing' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US',
'email' => 'john.doe#example.com',
'phone' => '(555) 555-5555'
],
'shipping' => [
'first_name' => 'John',
'last_name' => 'Doe',
'address_1' => '969 Market',
'address_2' => '',
'city' => 'San Francisco',
'state' => 'CA',
'postcode' => '94103',
'country' => 'US'
],
'line_items' => [
[
'product_id' => 93,
'quantity' => 2
],
[
'product_id' => 22,
'variation_id' => 23,
'quantity' => 1
]
],
'shipping_lines' => [
[
'method_id' => 'flat_rate',
'method_title' => 'Flat Rate',
'total' => 10
]
]
];
print_r($woocommerce->post('orders', $data));
I have some issues with that though. I haven't tested it yet because we use it on a live store, but I'd like to know some stuff that isn't mentioned anywhere. Except for when using a 3rd party plugin, which we don't want. We want to only use the native, clean WooCommerce plugin.
• Is it possible to use a payment method as the reference, even though it does not exist in WP itself? So, if we use Stripe to create and pay for the order in messenger even though Stripe itself isn't in use in our store, can we still push that as the payment reference in the $data array here:
'payment_method' => 'stripe',
'payment_method_title' => 'Offsite Payment Method',
I read you need to create the order first normally if you want to get the payment token, and then update the status after payment, but since we're doing the entire ordering and payment processing offsite (only utilizing the WooCommerce catalog) and want to shoot in the order after a successful ordering process outside of WP, I'm a bit puzzled.
There's a lot of info to be found about missing cart features unless you use something like cocart, which we don't want since we can't ask customers to all install another to be able to use our tool.
Anybody came across a similar use case?
Is it possible to use a payment method as the reference, even though it does not exist in WP itself?
Yes, they are basically strings you can pass any data which is of string type it doesn't matter whether it exists in wp or not
And as you said "you need to create the order first normally if you want to get the payment token, and then update the status after payment"
This is true first you need to create order ..
Why ?
Because in most of the payment gateways there will be a field called orderId to bind order id with that transaction and to keep things clean it's used ...So you need to create an order first when you create the order you need to set the status to pending. Then you will get orderId now redirect the user to payment gateway. Once user comes back again update the order status to processing if it success or failure if it failed

Use Amazon SNS to send SMS messages using PHP AWS SDK v2?

I inherited a PHP project and it's heavily integrated with AWS SDK v2. Using v3 is not an option at this time.
My question is how can I leverage SNS to send text messages to specific numbers as needed? That is, I don't want to send mass notifications to a bunch of phone numbers subscribed to a specific topic when an action occurs, I want to send a notification to a specific phone number when an action occurs.
I came across this https://stackoverflow.com/a/41268045/664881 but it appears to be using v3 of the AWS SDK. Is there an equivalent with v2 of the AWS SDK?
I managed to make it work in PHP AWS SDK v2, but you need to add new parameter in the source code.
// update file: aws-sdk-php/src/Aws/Sdk/Resources/sns-2010-03-31.php
'Publish' => array(
'parameters' => array(
'PhoneNumber' => array( // new parameter
'type' => 'string',
'location' => 'aws.query',
),
),
),
// You just need to publish it and include the `PhoneNumber` parameter
$snsClientResult = $snsClient->publish([
'Message' => 'YOUR_MESSAGE',
'PhoneNumber' => 'PHONE_NUMBER',
'MessageStructure' => 'SMS',
'MessageAttributes' => [
'AWS.SNS.SMS.SenderID' => [
'DataType' => 'String',
'StringValue' => 'SENDER_ID',
],
'AWS.SNS.SMS.SMSType' => [
'DataType' => 'String',
'StringValue' => 'Promotional', // Transactional
]
]
]);
// Get the response
$snsClientResult['MessageId']

Categories