Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvdsteege/03458f0da98089af0c673c32d6f93df8 to your computer and use it in GitHub Desktop.
Save rvdsteege/03458f0da98089af0c673c32d6f93df8 to your computer and use it in GitHub Desktop.
Auto complete Pronamic Pay recurring payments within given period.
<?php
// Add below code to the functions.php file of your WordPress theme and set date range in `$auto_complete_period`.
/**
* Pronamic Pay auto complete recurring payments within period.
*/
add_filter( 'wp_insert_post_data', 'pronamic_pay_recurring_payment_zero_amount', 9, 2 );
function pronamic_pay_recurring_payment_zero_amount( $data, $postarr ) {
/*
* Auto complete period.
*
* Recurring payments with a start date in this date range will be completed immediately
* (by updating the amount to `0`) without being started at gateway.
*/
$auto_complete_period = array(
'from' => new \DateTime( '2021-08-01 00:00:00' ),
'to' => new \DateTime( '2021-08-31 23:59:59' ),
);
// Check payment.
if ( ! array_key_exists( 'pronamic_payment', $postarr ) ) {
return $data;
}
$payment = $postarr['pronamic_payment'];
// Check if this is a recurring payment.
if ( 'recurring' !== $payment->get_meta( 'mollie_sequence_type' ) ) {
return $data;
}
// Check payment periods.
$periods = $payment->get_periods();
if ( null === $periods ) {
return $data;
}
// Check if period start is within auto complete period.
$period = reset( $periods );
$period_start_date = $period->get_start_date();
if ( $period_start_date < $auto_complete_period['from'] || $period_start_date > $auto_complete_period['to'] ) {
return $data;
}
// Set zero amount, payment will be completed immediately.
$payment->set_total_amount(
new \Pronamic\WordPress\Money\TaxedMoney(
0,
$payment->get_total_amount()->get_currency()->get_alphabetic_code()
)
);
$payment->set_status( 'Success' );
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment