Skip to main content
Question

WP Simple Pay -> Stripe -> RevenueCat

  • February 20, 2025
  • 2 replies
  • 35 views

Forum|alt.badge.img+6

Hello all,

I have implemented the Stripe payments on our website using WP Simple Pay https://wpsimplepay.com/ and I also have set up webhooks from Stripe to RevenueCat.

Once I pay (in test mode) via our website, the subscription is registered in RevenueCat but obviously it’s not assigned to the user with the email in the payment form on our website. 

From the docs I understand that I need to send the token but it’s completely unclear how to implement this on out website. 

Any help will be appreciated!

Thanks in advance!

This post has been closed for comments

2 replies

MarcosC
RevenueCat Staff
Forum|alt.badge.img+7
  • RevenueCat Staff
  • 167 replies
  • February 21, 2025

Hi ​@Martin Géč 

 

I’m not super familiar with how WPSimplePay works, but there are two ways of tracking new purchases in RevenueCat:

  • Alternative 1: You can call our POST /receipt endpoint and send then stripe checkout session or subscription ID as a fetch token and the app user ID to associate that to → Endpoint definition is here: https://www.revenuecat.com/docs/api-v1#tag/transactions/operation/receipts
  • Alternative 2: Configure stripe webhooks with RevenueCat and turn on  Track new purchases from server-to-server notifications . You’ll need to also define how do we handle the app user ID part (do we create an anonymous one, do we use the stripe customer ID, or do you want to provide your own in metadata).

 

One thing to be aware of, It seems that WPSimplePay supports Recurring payment and One Time payment. I’m not sure how they do it, but we only accept checkout sessions or subscriptions. If the way they support those payments is not using any of those, then we won’t be able to ingest them.

 

Regards,

Marcos


Forum|alt.badge.img+6
  • Author
  • Helper
  • 9 replies
  • February 21, 2025

Thanks ​@MarcosC! Really appreciate your input. Meanwhile I figured it out and posting the solution here for everyone who would like to implement WP Simple Pay on their website to save a couple of hours (or days) of head scratching and sipping late-night coffees. :-)

First setup Stripe products, entitlements, webhooks, etc. as described in the doc - https://www.revenuecat.com/docs/web/integrations/stripe

Enable the "Track new purchases from server-to-server notifications" option

Install Wordpress Code Snippet plugin and add the following code snippet:

function send_to_revenuecat($app_user_id, $subscription_id) {
  $api_url = 'https://api.revenuecat.com/v1/receipts';
  $api_key = 'YOUR_REVENUECAT_STRIPE_APP_PUBLIC_API_KEY';

  $body = array(
      'app_user_id' => $app_user_id,
      'fetch_token' => $subscription_id
  );

  $args = array(
      'body' => json_encode($body),
      'headers' => array(
          'Content-Type' => 'application/json',
          'X-Platform' => 'stripe',
          'Authorization' => 'Bearer ' . $api_key
      ),
      'timeout' => 45
  );

  $response = wp_remote_post($api_url, $args);
  if (is_wp_error($response)) {
      error_log('RevenueCat API Error: ' . $response->get_error_message());
      return false;
  }

  $response_code = wp_remote_retrieve_response_code($response);
  $body = wp_remote_retrieve_body($response);
  error_log('RevenueCat Response Code: ' . $response_code);
  error_log('RevenueCat Response Body: ' . $body);

  $data = json_decode($body, true);
  if ($response_code !== 200 || isset($data['error'])) {
      error_log('RevenueCat API Error: Code ' . $response_code . ' - ' . ($data['error']['message'] ?? 'Unknown error'));
      return false;
  }
  return true;
}

// Hook into subscription creation webhook
add_action('simpay_webhook_subscription_created', 'handle_successful_subscription', 10, 2);

function handle_successful_subscription($event, $subscription) {
  error_log('=== Starting Subscription Handler ===');

  // Extract data from the event's Invoice and Subscription
  $invoice = $event->data->object;
  $subscription_id = $invoice->subscription ?? $subscription->id ?? 'Subscription ID not found';
  $email = $invoice->customer_email ?? $subscription->customer->email ?? 'Email not found';

  error_log('Customer Email: ' . $email);
  error_log('Stripe Subscription ID: ' . $subscription_id);

  // Your logic to set app_user_id goes here
  $app_user_id = '';
  error_log('app_user_id: ' . $app_user_id);

  // Send to RevenueCat
  $result = send_to_revenuecat($app_user_id, $subscription_id);

  if (!$result) {
      error_log('Failed to send subscription ' . $subscription_id . ' to RevenueCat for user ' . $email);
      error_log('app_user_id that failed: ' . $app_user_id);
  } else {
      error_log('Successfully sent to RevenueCat');
  }

  error_log('=== End Subscription Handler ===');
}

Use WP Simple Pay and Stripe test mode to test the payment for the subscription and you should see the debug logs in debug.log file.

In case you have questions, let me know.


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings