• Resolved michael432

    (@michael432)


    hello, hope you’re doing well.
    I need some help regarding this payment plugin.
    I am using woocmmerce and ‘Payment Plugins for Stripe WooCommerce’ plugin in my client’s website. And my scenario is that when customer goes to checkout page and selects a main product and places an order, I am redirecting user to page called upsell-purchase page using woocommerce ‘thank-you’ page hook, instead of redirecting him to ‘thank-you’ page. The ‘upsell-purchase’ page is designed by elementor and has 2 buttons ‘Accept’ and ‘Reject’ button. If user ‘accept’ i am creating new order pragmatically and redirect user to ‘Thank You’ page, if he Click Reject then he is redirected to Thank You page without creating any order. Important thing is new pragmatically created order must be created using the same user information and payment method which is present in the main product. And payment method is stripe in this case and this where I am having trouble as I am unable to fetch payment method for custom created product.

    So basically I have created small simple one click upsell snippet and I have developed almost all the code which is redirecting user to page and so on and even creating order but with any payment method attached I unable to get the main product’s Payment method since I am using ‘this ‘Payment Plugins for Stripe WooCommerce’ strip Plugin for payment method and not other payment method.
    I cannot use upsell plugins as they are not compatible with ‘payment plugin by stripe’ plugin.

    Please help me by guiding in which file or document I can find the code for payment method for strip that I can implement in my custom order creation function. Or is even possible. I will really appreciate any help or any new direction as this is the last issue that I am the site.

    Extra Info: Using custom Meta Box I attach a specific product to main product and this is the product which I am showing on upsell-purchase page when user is redirected to that page. so ‘_ocu_upsell_product_id’ is the meta key where upsell product I is saved and I am using this in my code.
    I can provide full code if needed.
    Thanks in advance.

    This is part of my code that I tried but failed:

    add_action('template_redirect', 'process_upsell_with_payment');

    function process_upsell_with_payment() {
    if (isset($_GET['buy_now']) && $_GET['buy_now'] == 1 && isset($_GET['order_id'])) {
    $order_id = absint($_GET['order_id']);
    $product_id = 0;
    $order = wc_get_order($order_id);
    // Check if the order object exists
    if ($order) {
    // Loop through each item in the order
    foreach ($order->get_items() as $item) {
    // Get the product ID for each item
    $product_id = $item->get_product_id();
    }
    }

    if($product_id < 1){
    return;
    }
    $order = wc_get_order($order_id);
    if ($order) {
    $upsell_product_id = get_post_meta($post->ID, '_ocu_upsell_product_id', true);
    // Retrieve the saved payment token
    $payment_token = get_post_meta($order_id, '_saved_payment_token', true);

    if ($payment_token) {
    // Create a new WooCommerce order for the upsell product
    $new_order = wc_create_order();

    // Add the upsell product
    $new_order->add_product(wc_get_product($upsell_product_id), 1);

    // Copy customer details from the original order
    $new_order->set_customer_id($order->get_customer_id());
    $new_order->set_billing_first_name($order->get_billing_first_name());
    $new_order->set_billing_last_name($order->get_billing_last_name());
    $new_order->set_billing_address_1($order->get_billing_address_1());
    $new_order->set_billing_address_2($order->get_billing_address_2());
    $new_order->set_billing_city($order->get_billing_city());
    $new_order->set_billing_state($order->get_billing_state());
    $new_order->set_billing_postcode($order->get_billing_postcode());
    $new_order->set_billing_country($order->get_billing_country());
    $new_order->set_billing_email($order->get_billing_email());
    $new_order->set_billing_phone($order->get_billing_phone());

    $new_order->set_shipping_first_name($order->get_shipping_first_name());
    $new_order->set_shipping_last_name($order->get_shipping_last_name());
    $new_order->set_shipping_address_1($order->get_shipping_address_1());
    $new_order->set_shipping_address_2($order->get_shipping_address_2());
    $new_order->set_shipping_city($order->get_shipping_city());
    $new_order->set_shipping_state($order->get_shipping_state());
    $new_order->set_shipping_postcode($order->get_shipping_postcode());
    $new_order->set_shipping_country($order->get_shipping_country());

    // Calculate totals for the new order
    $new_order->calculate_totals();

    // Process payment with the saved token
    $result = wc_get_payment_gateway_by_order($order)->process_payment_with_token($new_order, $payment_token);

    if ($result['result'] === 'success') {
    $new_order->payment_complete();
    wp_redirect(site_url("/thank-you/order-received/?order_id={$order_id}&upsell_order_id={$new_order->get_id()}"));
    exit;
    } else {
    wc_add_notice('Payment failed. Please try again.', 'error');
    }
    }
    }
    }
    add_action('woocommerce_thankyou', 'redirect_to_upsell_page', 1);

    function redirect_to_upsell_page($order_id) {
    $order = wc_get_order($order_id);
    if (!$order || get_post_meta($order_id, '_upsell_shown', true)) {
    return;
    }

    // Mark this order to avoid redirection loops
    update_post_meta($order_id, '_upsell_shown', true);

    // URL to your custom Elementor upsell page
    $upsell_page_url = site_url('/upsell-purchase/?order_id=' . $order_id);
    wp_redirect($upsell_page_url);
    exit;
    }
    function redirect_to_upsell_page($order_id) {
    $order = wc_get_order($order_id);
    if (!$order || get_post_meta($order_id, '_upsell_shown', true)) {
    return;
    }

    // Mark this order to avoid redirection loops
    update_post_meta($order_id, '_upsell_shown', true);

    // URL to your custom Elementor upsell page
    $upsell_page_url = site_url('/upsell-page/?order_id=' . $order_id);
    wp_redirect($upsell_page_url);
    exit;
    }


    function accept_btn(){
    $odid = $_GET['order_id'];
    return '<a href="'.site_url().'/upsell-purchase/?order_id='.$odid.'&buy_now=1">Accept</a>';
    }
    add_shortcode('ocu_accept_button', 'accept_btn');
    function skip_btn(){
    $odid = $_GET['order_id'];
    return '<a href="'.site_url().'/thank-you/order-received/order_id='.$odid.'">Skip</a>';
    }
    add_shortcode('ocu_reject_button', 'skip_btn');
    • This topic was modified 1 year, 3 months ago by michael432.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Clayton R

    (@mrclayton)

    Hi @michael432

    I cannot use upsell plugins as they are not compatible with ‘payment plugin by stripe’ plugin.

    This plugin is compatible with upsell plugins like cartflows and funnelkit. If they indicated otherwise, they are mistaken.

    The easiest thing for you to do in your custom code is save the customer’s payment method during the checkout process, using filter

    add_filter('wc_stripe_force_save_payment_method', '__return_true');

    This will save the customer’s payment method and the payment method ID will be stored in the order’s metadata with key _payment_method_token.

    You can then use that payment method ID to process your upsell order.

    Kind Regards

    Thread Starter michael432

    (@michael432)

    hi,
    thank you for quick replay and the code.
    but I am bit confuse with this. You mentioned I should “save the customer’s payment method during the checkout process” using the given filter. Are you suggesting I should use ‘woocommerce_checkout_update_order_meta’ hook and save customer’s payment method  and method ID in metadata? if so then I did a little search and came up with this: is it correct?
    add_action('woocommerce_checkout_update_order_meta', 'save_payment_method_data', 10, 2);
    function save_payment_method_data($order_id, $data) {
    $payment_method = WC()->session->get('chosen_payment_method');
    $payment_method_id = get_user_meta(get_current_user_id(), '_payment_method_token', true);
    if ($payment_method) {
    update_post_meta($order_id, '_payment_method_used', sanitize_text_field($payment_method));
    }
    if ($payment_method_id) {
    update_post_meta($order_id, '_payment_method_id', sanitize_text_field($payment_method_id));
    }
    }

    but how can I use this info inside my function which is:
    add_action('template_redirect', 'process_upsell_with_payment');
    function process_upsell_with_payment() { ... Payment method to get... }

    right now this is the code which was suppose to add payment method inside new order, the line right after ‘$new_order->calculate_totals();

    $result = wc_get_payment_gateway_by_order($order)->process_payment_with_token($new_order, $payment_token);

    should I replace this code? but with what method I should replace it with? that the issue I am facing that I cannot find the right method and default method is not working .
    what could be the function that is used by ‘Stripe payment plugin’ for adding payment method in order?If I know where the code is in file I can try that.
    can you guide me to what file I should check?
    If this is not possible then can show some sample code or document or article that I can check?
    I am sorry if this is asking to much, its just is this last step I stuck on in this task and I will highly apricate any help. thank you in advance

    • This reply was modified 1 year, 3 months ago by michael432.
    • This reply was modified 1 year, 3 months ago by Yui.
    Plugin Author Clayton R

    (@mrclayton)

    Hi @michael432

    Are you suggesting I should use ‘woocommerce_checkout_update_order_meta’ hook and save customer’s payment method  and method ID in metadata?

    No, that is not what my previous reply was suggesting. Per the previous reply, you should use the filter wc_stripe_force_save_payment_method. That will indicate to the plugin that you want to save the customer’s payment method.

    You can then use that saved payment method to process the payment for your upsell. The payment method ID is stored in the WC_Order metadata with a key of _payment_method_token.

    should I replace this code? but with what method I should replace it with? 

    I think you should just use the methods already available which is the WC_Payment_Gateway_Stripe::process_payment method. It already accepts an order ID as the first argument. Your function process_upsell_with_payment is close to being correct. Here are my suggested changes which will make it much easier.

    // get the saved payment method token from the order object.
    $payment_method_token = $order->get_meta('_payment_method_token');
    // get the payment gateway from the order
    $payment_gateway = wc_get_payment_gateway_by_order($order);
    // set the saved payment method token so it can be used to process the upsell order
    $payment_gateway->payment_method_token($payment_method_token);
    // call the standard process_payment method.
    $result = $payment_gateway->process_payment($new_order->get_id());
    Thread Starter michael432

    (@michael432)

    I am sorry it took me long to reply. the given worked and task is resolved.
    thank you very much for help and support 🙂

    Plugin Author Clayton R

    (@mrclayton)

    Hi @michael432

    You’re welcome. Good reviews are always welcome. https://ww.wp.xz.cn/support/plugin/woo-stripe-payment/reviews/

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘implementing strip plugin payment method in order created pragmatically’ is closed to new replies.