stripe-php-files.php shown in the pic has this code:
require_once 'stripe/init.php';
use Stripe\Stripe;
add_action('init', function() {
Stripe::setApiKey('pk_test_aaa111etc);
//ShowMsgBox ("Stripe API key set"); THIS HAS APPEARED
});`
Created a submit button:
<form method="POST">
<!--Add a hidden field with the LOOKUP_KEY of your price:-->
<input type="hidden" name="lookup_key" value="C29-S0-M" />
<button type="submit" name="trigCheckout01" style="color:green">Pay monthly</button>
</form>
This code runs on the submit button:
function fnCheckout01 ($atts) {
//ShowMsgBox ("Running fnCheckout01..."); THIS APPEARED
require_once(ABSPATH . 'wp-config.php');
global $wpdb;
$wpdb->show_errors(); // this will debug any sql errors onto the webpage
if (isset($_POST['TrigCheckout01'])) {
//TrigCheckout01 is the name of the submit btn<br>";
$stripe = new \Stripe\StripeClient(
'pk_test_aaa111etc'
);
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' => 'https://abc.net/new-subscription-success/',
'cancel_url' => 'https://abc.net/checkout-cancelled/',
'line_items' => [
[
'quantity' => 1,
],
],
'mode' => 'subscription',
]);
}// ends fn
My product catalogue has a subscription item with two prices, the monthly one has key C29-S0-M and the annual one has key C29-S0-Y.
On clicking the button, nothing happens – no ‘how do you want to pay’ form, no redirect to success html.
Please could you review what I’ve done and tell me:
How can the submit button use the key from stripe-php-files.php so I have only one place to update it when I go live?
How can the function use the lookup_key from the button?
How do I retrieve a payment error code?
Thank you for any assistance.
It does prevent that and checking for the name of the button is the workround.
Thank you - case was the problem, I’m not used to programming in a case sensitive language!
I’ve found a subscription example in stripe docs and incorporated it in my function, which now reads:
function fnCheckout01 ($atts) {
require_once(ABSPATH . 'wp-config.php');
global $wpdb;
$wpdb->show_errors();
if (isset($_POST['TrigCheckout01'])) {
//header('Content-Type: application/json');
$stripe = new \Stripe\StripeClient(
'sk_test_aaa111etc'
);
try {
$prices = \Stripe\Price::all([
// retrieve lookup_key from form data POST body
'lookup_keys' => [$_POST['lookup_key']],
'expand' => ['data.product']
]);
$checkout_session = \Stripe\Checkout\Session::create([
'success_url' => 'https://aaa.net/new-subscription-success/',
'cancel_url' => 'https://aaa.net/checkout-cancelled/',
'mode' => 'subscription',
//'payment_method_types' => ['card'],
'line_items' => [
[
'price' => $prices->data[0]->id,
'quantity' => 1,
],
],
]);
//header("HTTP/1.1 303 See Other");
//header("Location: " . $checkout_session->url);
} catch (Error $e) {
http_response_code(500);
echo json_encode(['error' => $e->getMessage()]);
}
No error message appears, no payment details are requested & it doesn’t redirect.
Their header lines I’ve commented because WordPress doesn’t give me access to the header; do I need to achieve what they’re doing some other way?