Execute Easy Digital Downloads Pending Order Programmatically

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');```

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật