I’m trying to implement a paid trial version with EDD. This functionality doesn’t exist so I’m trying to hack my way around it.
The current flow is a user will purchase a trial subscription and I want to create a pending order for the next monthly/yearly subscription and run a cron job 7 days later for the full subscription charge.
I’ve gotten everything working with generating the pending order, but I cannot get the pending order to execute to have the payment process through stripe.
Currently as a user in the front-end, I can go to that pending order and execute it. I want to avoid that, and have that process automatically.
Am I missing anything? Here’s my code:
This creates the pending order.
// Function to create a pending order
function edd_custom_subscription_create_pending_order($customer_id, $original_payment_id, $subscription_product_id, $customer_email) {
edd_debug_log('---------------------- EDD Custom Subscription Order Creation Log Start ----------------------', true);
edd_debug_log('EDD Custom Subscription: Creating new subscription order for customer ID: ' . $customer_id . ' with product ID: ' . $subscription_product_id, true);
// Create a new payment using EDD_Payment class
$payment = new EDD_Payment();
$payment->customer_id = $customer_id;
$payment->email = $customer_email;
$payment->status = 'pending';
$payment->add_download($subscription_product_id);
$payment->parent_payment = $original_payment_id; // Link to the original payment
// Set payment details
$payment->date = date('Y-m-d H:i:s');
$payment->total = edd_get_download_price($subscription_product_id);
$payment->currency = edd_get_currency();
$payment->user_info = array(
'id' => $customer_id,
'email' => $customer_email,
'first_name' => get_user_meta($customer_id, 'first_name', true),
'last_name' => get_user_meta($customer_id, 'last_name', true),
'discount' => 'none'
);
$payment->gateway = edd_get_option('gateway', 'stripe');
// Debugging additional information
edd_debug_log('EDD Custom Subscription: Payment details before save: ' . print_r($payment, true), true);
// Save the payment
$payment_id = $payment->save();
if ($payment_id) {
edd_debug_log('EDD Custom Subscription: New subscription order created with payment ID: ' . $payment_id, true);
edd_debug_log('EDD Custom Subscription: Payment status after creation: ' . $payment->status, true);
// Schedule the completion of the payment 7 days from now
$completion_timestamp = time() + 7 * 24 * 60 * 60; // 7 days from now
wp_schedule_single_event($completion_timestamp, 'edd_custom_subscription_complete_payment', array($payment_id));
} else {
edd_debug_log('EDD Custom Subscription: Failed to create new subscription order. Payment details after save attempt: ' . print_r($payment, true), true);
}
edd_debug_log('---------------------- EDD Custom Subscription Order Creation Log End ----------------------', true);
}
add_action('edd_custom_subscription_create_pending_order', 'edd_custom_subscription_create_pending_order', 10, 4);
This is the function to complete the payment:
// Function to complete the payment
function edd_custom_subscription_complete_payment($payment_id) {
edd_debug_log('---------------------- EDD Custom Subscription Payment Completion Log Start ----------------------', true);
edd_debug_log('EDD Custom Subscription: Completing payment with ID: ' . $payment_id, true);
$payment = new EDD_Payment($payment_id);
edd_debug_log('EDD Custom Subscription: Payment status before completion: ' . $payment->status, true);
if ($payment && $payment->status == 'pending') {
$payment->status = 'complete';
$payment->save();
// Process payment through the gateway
edd_debug_log('EDD Custom Subscription: Initiating payment processing through gateway: ' . $payment->gateway, true);
$gateway = edd_get_gateway_class($payment->gateway);
if ($gateway) {
$gateway->process_payment($payment->ID);
edd_debug_log('EDD Custom Subscription: Payment processed through gateway: ' . $payment->gateway, true);
} else {
edd_debug_log('EDD Custom Subscription: Gateway not found: ' . $payment->gateway, true);
}
} else {
edd_debug_log('EDD Custom Subscription: Payment with ID: ' . $payment_id . ' is not in pending status.', true);
}
edd_debug_log('---------------------- EDD Custom Subscription Payment Completion Log End ----------------------', true);
}
add_action('edd_custom_subscription_complete_payment', 'edd_custom_subscription_complete_payment');```