If this is your first visit, check our documentation. To post, register or login.
Forums / Addon Payment Modules / Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

Started by buttons on 9 Apr 2024, 12:47 AM · Views: 5,280

Results 1 to 13 of 13
9 Apr 2024, 12:47 AM
#1
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

Hello Zen Cart Community,

I'm working with a custom payment module on my Zen Cart site that relies on an iframe for processing payments. The flow is as follows:

The customer completes their order and confirms it.
An iframe is generated for payment processing.
Upon payment completion, a success page is displayed.
Simultaneously, a webhook is triggered to handle post-payment actions.
The challenge I'm encountering is related to the externalUserId parameter in the webhook. The externalUserId should contain the most recent order ID generated by Zen Cart upon order confirmation. However, attempts to retrieve this ID have been unsuccessful – I either get an outdated order number (the previous order) or no result at all.

The expected behavior is to capture the latest order ID in time to include it in the webhook payload. However, there seems to be a timing issue or a session management problem that prevents the accurate retrieval of the order ID after the order confirmation and before the webhook is sent.

Here's what I have tried so far:

Using session variables to pass the order ID, which has resulted in incorrect or empty values.
Directly querying the database for the latest order ID, which also doesn't seem to capture the correct ID in time.
This has been a persistent issue affecting the payment confirmation process, and I'm looking for insights or suggestions on how to reliably capture the latest order ID in the webhook after the customer has confirmed their order.

I have spent over a month on this pulling my hair out. I now am hoping that someone else that has been through this, could possibly direct me in the right direction.

Thank you in advance.

9 Apr 2024, 1:07 AM
#2
lat9 avatar

lat9

Administrator

Join Date:
Sep 2009
Location:
Stuart, FL
Posts:
14,142
Plugin Contributions:
56

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

The actual orders_id isn't generated and available to the payment module until the module's after_order_create method is called with that just-created order-id.

9 Apr 2024, 4:22 AM
#3
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

lat9:

The actual orders_id isn't generated and available to the payment module until the module's after_order_create method is called with that just-created order-id.

Thank you for your reply. Is there a way to take the cartID and match that up with an order id? I'm trying to figure out how to match up with an order. I am still having problems trying to get the order number.

9 Apr 2024, 1:18 PM
#4
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

buttons:

Thank you for your reply. Is there a way to take the cartID and match that up with an order id? I'm trying to figure out how to match up with an order. I am still having problems trying to get the order number.

get the next order number from the orders table, use that, and move on.

i like the method in this post.

best.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

9 Apr 2024, 4:20 PM
#5
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

carlwhat:

get the next order number from the orders table, use that, and move on.

i like the method in this post.

best.

When I try to grab the orders_id, it is empty.

Here's the sequence I have implemented so far:

In the before_process method, I generate an iframe for the payment interface.
I've attempted to capture the order ID in the after_order_create method, which seems to be the logical place to get the latest order ID. This is the code snippet I've used:

public function after_order_create($insert_id) {
    global $db;

    // Logging the order ID for debugging
    file_log("after_order_create called. Order ID: {$insert_id}");

    // Prepare the webhook data with the order ID
    $webhookData = [
        'amount' => $amount,
        'currency' => $currency,
        'externalUserId' => $insert_id,
    ];

    // Send the webhook with the prepared data
    $this->send_webhook($webhookData);
}

I've also tried fetching the order ID directly in the webhook.php file.

I am aware that guessing the next order number is not feasible due to the risk of duplication. My concern is ensuring the after_order_create method is triggered at the correct time so I can send the order ID via the webhook without issues.

Could you advise on the proper sequence to handle this process, especially concerning the placement of the after_order_create in relation to the iframe generation in the before_process method? Is there an established best practice for this scenario that I might be overlooking?

Any insights or suggestions would be greatly appreciated, as I might be missing something obvious due to my close involvement with the project.

9 Apr 2024, 4:24 PM
#6
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

carlwhat:

get the next order number from the orders table, use that, and move on.

i like the method in this post.

best.

carlwhat:

get the next order number from the orders table, use that, and move on.

i like the method in this post.

best.

When I try to grab the orders_id, it is empty.

Here's the sequence I have implemented so far:

In the before_process method, I generate an iframe for the payment interface.
I've attempted to capture the order ID in the after_order_create method, which seems to be the logical place to get the latest order ID. This is the code snippet I've used:

public function after_order_create($insert_id) {
global $db;

// Logging the order ID for debugging
file_log("after_order_create called. Order ID: {$insert_id}");

// Prepare the webhook data with the order ID
$webhookData = [
    'amount' => $amount,
    'currency' => $currency,
    'externalUserId' => $insert_id,
];

// Send the webhook with the prepared data
$this->send_webhook($webhookData);

}

I've also tried fetching the order ID directly in the webhook.php file.

I am aware that guessing the next order number is not feasible due to the risk of duplication. My concern is ensuring the after_order_create method is triggered at the correct time so I can send the order ID via the webhook without issues.

Could you advise on the proper sequence to handle this process, especially concerning the placement of the after_order_create in relation to the iframe generation in the before_process method? Is there an established best practice for this scenario that I might be overlooking?

Any insights or suggestions would be greatly appreciated, as I might be missing something obvious.

9 Apr 2024, 4:47 PM
#7
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

i have never used the after_order_create method.

does your log have the proper order number?

i would use the trigger_error method so that the log goes to the standard apache log and then zen would order override it.

your code looks fine to me. but the question is whether your webhook is run at the right time.

have you tried in the after_process method?

        public function after_process()
        {
            global $insert_id;

// one would need the code to get the currency and amount

// Prepare the webhook data with the order ID
$webhookData = [
'amount' => $amount,
'currency' => $currency,
'externalUserId' => $insert_id,
];

// Send the webhook with the prepared data
$this->send_webhook($webhookData);
}

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

9 Apr 2024, 5:15 PM
#8
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

Thank you for your reply.

I sometimes get the correct order number. in a log file. I can also get an old order number sometimes. I can always get the CartID, but there isn't much I can do with that, I cannot tie to an order. The post above yours said the order ID comes with the after_order_create. I am trying to use this to grab the order_id. Timing is very much the issue and it's finding that right timing.

9 Apr 2024, 6:43 PM
#9
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

buttons:

Thank you for your reply.

I sometimes get the correct order number. in a log file. I can also get an old order number sometimes. I can always get the CartID, but there isn't much I can do with that, I cannot tie to an order. The post above yours said the order ID comes with the after_order_create. I am trying to use this to grab the order_id. Timing is very much the issue and it's finding that right timing.

my friend, do you want my help? if not, please let me know, and i will stop.

i look at all posts prior to posting on a thread.... well at least on short threads...

i saw the post referenced above written by my friend lat9, who i have tremendous respect for and who does so much for this community. when it comes to payment modules, i'm pretty confident she would say the same about me.... maybe not...

the after_order_create should work. but you are saying it does not. i am offering a different solution that i use in the multiple payment modules that i have created for zen-cart. in fact, it is the same methodology used in payment modules that come with the base package.

see this module.

so you can either try it that way and let the community know if it works or if does not. and if it does not, i would argue that something else is going on in your code.

best.

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

9 Apr 2024, 6:50 PM
#10
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

carlwhat:

my friend, do you want my help? if not, please let me know, and i will stop.

i look at all posts prior to posting on a thread.... well at least on short threads...

i saw the post referenced above written by my friend lat9, who i have tremendous respect for and who does so much for this community. when it comes to payment modules, i'm pretty confident she would say the same about me.... maybe not...

the after_order_create should work. but you are saying it does not. i am offering a different solution that i use in the multiple payment modules that i have created for zen-cart. in fact, it is the same methodology used in payment modules that come with the base package.

see this module.

so you can either try it that way and let the community know if it works or if does not. and if it does not, i would argue that something else is going on in your code.

best.

Absolutely I welcome your input and help :) I will take a look at the file you have provided and try to implement. I will update once done.

Thank you so much.

10 Apr 2024, 2:57 AM
#11
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

carlwhat:

my friend, do you want my help? if not, please let me know, and i will stop.

i look at all posts prior to posting on a thread.... well at least on short threads...

i saw the post referenced above written by my friend lat9, who i have tremendous respect for and who does so much for this community. when it comes to payment modules, i'm pretty confident she would say the same about me.... maybe not...

the after_order_create should work. but you are saying it does not. i am offering a different solution that i use in the multiple payment modules that i have created for zen-cart. in fact, it is the same methodology used in payment modules that come with the base package.

see this module.

so you can either try it that way and let the community know if it works or if does not. and if it does not, i would argue that something else is going on in your code.

best.

Good evening.

You had put me in the right direction with that page you found :) I tweaked it a bit for my needs and now I have a webhook with externalUserId and the order id matches! Because you took the time to look, you helped me greatly. I want to thank you very much for that. I truly appreciate it.

Now to write it so the status can update in the backend on the return of the externalUserId order.

10 Apr 2024, 1:40 PM
#12
carlwhat avatar

carlwhat

zennedOut

Join Date:
Nov 2005
Location:
los angeles
Posts:
2,974
Plugin Contributions:
8

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

:thumbsup:

author of square Webpay, now with Apple Pay and Google Pay.
mXworks
donations: venmo or paypal accepted.
premium consistent excellent support. available for hire.

10 Apr 2024, 7:14 PM
#13
buttons avatar

buttons

New Zenner

Join Date:
Apr 2024
Posts:
8
Plugin Contributions:
0

Re: Issues with Custom Payment Module Retrieving Latest Order ID for Webhook

carlwhat:

:thumbsup:

I spoke too soon :(

It's giving me the payload in a webhook log from my script not from the processor or passing onto there. I have found the following.

When using session management to get the order_id, it's giving me the previous order_id due to an old session. If I delete all the sessions and do a new order, the externalUserid is empty.

So, I will get back to figuring it out.