Created
March 13, 2020 17:43
-
-
Save ideadude/611878895c0a2a422b85f8f67769e4c3 to your computer and use it in GitHub Desktop.
Synchronize Stripe invoice.payment_succeeded Events in PMPro.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Synchronize Stripe invoice.payment_succeeded Events | |
* Warning. This code will update your database, | |
* create new orders, and potentially email your customers. | |
* Proceed with caution. Back up your database. | |
* You will likely want to pair this script with a plugin | |
* or script to disable all email. | |
* The default limit is 200 events. You may need to increase this | |
* or update the script to paginate and run events in batches. | |
* The default delay between event calls is 2 seconds. | |
* | |
* To run, add as a snippet or custom plugin, then visit | |
* yoursite.com/?sync_stripe_events=1 while logged in as | |
* an administrator. | |
*/ | |
function my_sync_stripe_events() { | |
if ( ! empty( $_REQUEST['my_sync_stripe_events'] ) && current_user_can( 'manage_options' ) ) { | |
$gateway = new PMProGateway_stripe(); | |
echo "Looking for invoice.payment_suceeded events.<br />"; | |
if ( !empty( $_REQUEST['limit'] ) ) { | |
$limit = intval( $_REQUEST['limit'] ); | |
} else { | |
$limit = 200; | |
} | |
$events = \Stripe\Event::all(['type' => 'invoice.payment_succeeded', 'limit' => $limit]); | |
$event_ids = array(); | |
foreach( $events as $event ) { | |
$event_ids[] = $event->id; | |
} | |
echo "<p>Found " . count( $event_ids ) . " invoice.payment_suceeded events to process.</p>"; | |
echo "<p><a id='start' href='#'>Click here to process these events.</a></p>"; | |
?> | |
<html> | |
<head> | |
<script type='text/javascript' src='/wp-includes/js/jquery/jquery.js'></script> | |
</head> | |
<body> | |
<textarea id="messages" rows="10" cols="100"></textarea> | |
<script> | |
var event_ids = <?php echo json_encode( $event_ids ); ?>; | |
var webhook_url = <?php echo json_encode( admin_url( "admin-ajax.php" ) . "?action=stripe_webhook" ); ?>; | |
var event_counter = 0; | |
var event_delay = 2000; // 2 seconds | |
jQuery('#start').click( function() { | |
process_next_event(); | |
}); | |
function process_next_event() { | |
if ( event_ids[event_counter].length ) { | |
jQuery.get({ | |
url: webhook_url + '&event_id=' + event_ids[event_counter], | |
success: function( data ) { | |
console.log( data ); | |
jQuery('#messages').append( data ); | |
} | |
}); | |
event_counter++; | |
setTimeout( 'process_next_event()', event_delay ); | |
} else { | |
jQuery( '#messages' ).append( 'All done!' ); | |
} | |
} | |
</script> | |
</body> | |
</html> | |
<?php | |
exit; | |
} | |
} | |
add_action( 'init', 'my_sync_stripe_events' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment