Having trouble integrating the various parts for a Stripe subscription order form on WordPress

I’m attempting to integrate Stripe for the first time, on a WordPress site. This is what I’ve done so far:

  1. Downloaded the Stripe library and put it under the folder structure shown in the picture.
  2. 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
});`
  1. 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>
  1. 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:

  1. 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?
  2. How can the function use the lookup_key from the button?
  3. How do I retrieve a payment error code?
    Thank you for any assistance.

Isn’t the issue here:

<button type="submit" name="trigCheckout01" style="color:green">Pay monthly</button>

and here:

if (isset($_POST['TrigCheckout01'])) {

remembering that PHP variable names are case sensitive? Normally you’d check for a form submission using

if ($_SERVER['REQUEST_METHOD'] == "POST") {

but I don’t know if your environment prevents this. But if in doubt, you can always var_dump($_POST) to see what you’re receiving.

Also, if I’m counting correctly, this line:

}// ends fn

doesn’t in fact end the function, it ends the if() clause.

2 Likes

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?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.