#JCoDog-error
1 messages · Page 1 of 1 (latest)
@cold hawk hi! not sure what I'm looking at. Can you give a lot more context? What specifically did you try(share the code), what did you expect to see ,etc.
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$upOne = dirname(__DIR__, 1);
require $upOne.'/resources/vendor/autoload.php';
$envDir = $upOne.'/resources/includes';
Dotenv\Dotenv::createImmutable($envDir)->load('./env');
$key = $_ENV['STRIPETESTKEY'];
\Stripe\Stripe::setApiKey($key);
$stripe = new \Stripe\StripeClient($key);
header('Content-Type: application/json');
$YOUR_DOMAIN = 'https://jconet.co.uk/credits';
ini_set('session.cookie_domain', '.jconet.co.uk');
session_start();
define('dbHost', $_ENV['DATABASEHOSTNAME']);
define('dbUser', $_ENV['DATABASEUSERNAME']);
define('dbPass', $_ENV['DATABASEPASSWORD']);
define('dbName', $_ENV['DATABASENAME']);
$conn = mysqli_connect(dbHost,dbUser,dbPass,dbName);
define('price', $_POST['price']);
$result = mysqli_query($conn, "SELECT * FROM customers WHERE ID ='".$_SESSION['userID']."'");
$num_rows = mysqli_num_rows($result);
$customerDB = mysqli_fetch_all($result, MYSQLI_ASSOC);
if ($num_rows > 0) {
$custID = $customerDB[0]['stripeID'];
$stripeSession = \Stripe\Checkout\Session::create([
'payment_method_types' => [
'card',
],
'line_items' => [[
# TODO: replace this with the `price` of the product you want to sell
'price' => price,
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 1,
'maximum' => 10,
],
'quantity' => 1,
]],
'mode' => 'payment',
'customer' => 'cus_KEndsK8XykbkYC',
'allow_promotion_codes' => true,
'success_url' => $YOUR_DOMAIN . '/return.php?status=SUCCESS',
'cancel_url' => $YOUR_DOMAIN . '/return.php?status=CANCEL',
]);
echo ("What went wrong?");
} else {
echo("no result");
$result2 = mysqli_query($conn, "SELECT * FROM users WHERE ID ='".$_SESSION['userID']."'");
$num_rows2 = mysqli_num_rows($result2);
$userDB = mysqli_fetch_all($result2, MYSQLI_ASSOC);
$stripeSession = \Stripe\Checkout\Session::create([
'payment_method_types' => [
'card',
],
'line_items' => [[
# TODO: replace this with the `price` of the product you want to sell
'price' => price,
'adjustable_quantity' => [
'enabled' => true,
'minimum' => 1,
'maximum' => 10,
],
'quantity' => 1,
]],
'mode' => 'payment',
'customer_email' => $userDB[0]['Email'],
'allow_promotion_codes' => true,
'success_url' => $YOUR_DOMAIN . '/return.php?status=SUCCESS',
'cancel_url' => $YOUR_DOMAIN . '/return.php?status=CANCEL',
]);
};
?>```
seems expected then.
your code has echo ("What went wrong?"); so that is what it will return in the HTTP response when you call that script.
what did you expect to get instead?
Sent to the checkout
ok but you don't have any code to do that , so why?
Oh... I completely missed that...
I was trying to just get this test set up so I could test the webhook and set up what happens when the payment completes and totally forgot to redirect.
cool cool —you'd want to try to actually access $stripeSession->url and redirect to it directly with a 3xx , or return the URL to the frontend and then redirect by setting window.location there
Also, the subtotal is not adjusting in checkout now...
I go through setting new quantities but the price doesnt change
what's the price_xxx ? but it looks like a tiered price to me based on the '$5 for every 6000' language) , and 1 and 2 are probably in the same tier so the amount doesn't change.
OH...
Package pricing
So there is no way to change the amount of that package a user can buy?
looks about right then, the overall amount is not going to change between quantity:1 and quantity:2 with that price setup
do the minimum and maximum fields on the adjustable_quantity parameter help you?
No they are not changing the price shown to the user, so I am not sure if that is allowing the user to buy more than one of the package.
they do allow them(you just tested buying more than one).
you have the maximum set to 10 though, but the pricing is that anything between 1 and 6000 is the same (I think, you did not share the price_xxx ID so I can't look).
so you'd have to pick more than 6000 for the price to change
price_1KH7rtEOJsqRXL0RZEGmt475
which is possible but clunky in Checkout, it's not really optimised for large amounts like that
Ok so as this is for in game currency purchase... Package pricing isnt the way to go?
it might be!
I think you were expecting that the quantity in the Checkout page is the quantity of the groups, like it's equivalent to picking a group of 6000. But it's not, it's for picking a quantity of 1 individual unit
Right...
unfortuantely Checkout isn't really optimised for this
Then I dont think package pricing is what I need... I think I need normal pricing but make each price = a different amount in the backend.
I would instead ask the user in your own page for the amount and build your own UI for it, and then just pass the quantity in to the API call, don't use adjustable_quantity
That option also work... Thanks.
However I think I will change how the prices work as it is easier for me to manage that way
Would volume be the better option?
Because it being like this is not ideal for telling a user how many credits they have...
I think this is what you meant when you said allow the user to adjust quantity on the page itself and not within the checkout?
Hey, taking over here! Let me get caught up
Yes, exactly!
Ok thanks. It shows weird in the checkout but I think my users will understand it
Will this tell me how many were purchased within the webhook reply?
Which event are you listening for?
// Handle the event
switch ($event->type) {
case 'checkout.session.async_payment_failed':
$session = $event->data->object;
case 'checkout.session.async_payment_succeeded':
$session = $event->data->object;
// ... handle other event types
default:
echo 'Received unknown event type ' . $event->type;
}
http_response_code(200);
I know if succeeded I will need to be able to tell what product it was that was purchased, the customer that paid and the quantity
That data will be on the line_items field on the Checkout Session object: https://stripe.com/docs/api/checkout/sessions/object#checkout_session_object-line_items
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
But they aren't on the webhook response by default, so you'll need to retrieve the Checkout Session manually and expand it
isnt that what $event->data->object does?
That will give you the Checkout Session object, yes. But they don't include the line_items field by default. Needs to be expanded which isn't possible directly on a webhook
Ok but I can do it through retrieving the object in the webhook, just have to then make a request for the checkout session inside of the code that handles the response?
So you'll need to retrieve the Checkout Session object and expand the line_items parameter:
$stripe->checkout->sessions->retrieve(
'cs_xxx',
['expand' => ['line_items']]
);
Yep, exactly! You'd use the id from the webhook payload and make that API request
thanks
Ok why does the guide show two ways of calling stripe?
Which is the one to use?
Either works, but the bottom options is considered the standard
Ok thank you because depending on which part of the docs I read it shows both
Yeah it's just preference really
With the lineitems does it show product name/id so I dont have to do a check for both prices?
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
Full shape is there
line_items.data.id string so this with be the prod_xxx?
No, the actual prod_xxx ID will be in the price hash (see price.product): https://stripe.com/docs/api/checkout/sessions/object?lang=php#checkout_session_object-line_items-data-price
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
So this?
line_items will be an array so I guess you'd need to loop over that
ok.
This?
Sure, looks ok to me. Does it work as you need?
Still checking the docs to see what info i can get that will make it useful for adding the credits to the account