Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save greenhornet79/5259c04f6ec6e6a9b222a01a112ac3b9 to your computer and use it in GitHub Desktop.

Select an option

Save greenhornet79/5259c04f6ec6e6a9b222a01a112ac3b9 to your computer and use it in GitHub Desktop.
<?php
add_action( 'leaky_paywall_stripe_invoice_payment_succeeded', 'mysite_send_stripe_renewal_thank_you', 10, 2 );
function mysite_send_stripe_renewal_thank_you( $user, $invoice ) {
if ( empty( $invoice->billing_reason ) || 'subscription_cycle' !== $invoice->billing_reason ) {
return;
}
// Idempotency: Stripe retries webhooks. Don't email twice for the same invoice.
$idempotency_key = 'mysite_renewal_email_' . $invoice->id;
if ( get_transient( $idempotency_key ) ) {
return;
}
set_transient( $idempotency_key, 1, WEEK_IN_SECONDS );
$first_name = $user->first_name ? $user->first_name : 'there';
$amount = isset( $invoice->amount_paid ) ? number_format( $invoice->amount_paid / 100, 2 ) : '';
$currency = isset( $invoice->currency ) ? strtoupper( $invoice->currency ) : '';
$subject = 'Thank you for renewing your subscription';
$message = "Hi {$first_name},\n\n";
$message .= "Your subscription renewal went through. Thank you for continuing to support us.\n\n";
if ( $amount && $currency ) {
$message .= "Amount renewed: {$amount} {$currency}\n\n";
}
$message .= "If you have any questions, just reply to this email.\n\n";
$message .= "— The Publisher\n";
wp_mail( $user->user_email, $subject, $message );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment