Sam666 Posted December 19, 2022 Share Posted December 19, 2022 Hello, I have JavaScript based apps and I am able to AddOrder via WHMCS API, but facing issue when trying to passing multiple customfields to WHMCS API. According to the API document, I will need to pass serialized and base64 value to WHMCS API, if I just pass a single custom field, I can just pass something like `YToxOntpOjczO3M6NjoiU2VydmVyIjt9` to API, and the API can accept the customfield value without issue. But if I put multiple custom fields, supporse I should pass an array to WHMCS API, on JavaScript I tried below different format but non of them works. ('YToxOntpOjczO3M6NjoiU2VydmVyIjt9', 'YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==') (YToxOntpOjczO3M6NjoiU2VydmVyIjt9, YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==) ('YToxOntpOjczO3M6NjoiU2VydmVyIjt9', 'YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==') Array( [0] => YToxOntpOjczO3M6NjoiU2VydmVyIjt9 [1] => YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==) JSON.stringify(["YToxOntpOjczO3M6NjoiU2VydmVyIjt9", "YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ=="]) Non of the above works. If I just pass a single field either `YToxOntpOjczO3M6NjoiU2VydmVyIjt9` or `YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==` it works just fine, the WHMCS can accept the value, but multiple custom fields just didn't work. Appreciate if anyone can help. 0 Quote Link to comment Share on other sites More sharing options...
Kian Posted December 19, 2022 Share Posted December 19, 2022 (edited) You should post your object/array. We can't do much with encoded strings. Anyway there's a catch that could be confusing. Maybe this is the mistake you are making. Here is how you add one product defining two custom fields in PHP: $postData = [ 'clientid' => 1, 'pid' => [ 1 ], 'billingcycle' => [ 'annually' ], 'customfields' => [ base64_encode(serialize([ 6 => 'Hello', 7 => 'World' ])) ], 'paymentmethod' => 'banktransfer', ]; $results = localAPI('AddOrder', $postData); As you can see there's a single array in customfields array since we're adding one product. The two custom fields are defined with two key-value paris. This is the array that needs to be serialized and encoded. Let's repeat the process but this time with an order that contains three products: $postData = [ 'clientid' => 1, 'pid' => [ 1, 1, 3 ], 'billingcycle' => [ 'annually' ], 'customfields' => [ base64_encode(serialize([ 6 => 'First', 7 => 'Produtc' ])), base64_encode(serialize([ 6 => 'Second', 7 => 'Produtc' ])), base64_encode(serialize([ 6 => 'Third', 7 => 'Produtc' ])), ], 'paymentmethod' => 'banktransfer', ]; $results = localAPI('AddOrder', $postData); As you can see each sub-array of "customfields" needs to be serialized and encoded individually. The first sub-array is for the first product (pid 1), the second is for the second (another pid 1) and the third for the third (pid 3). Make sure you are serializing and encoding the array in this structure. In essence there must be one serialize and one encode for each product. Edited December 19, 2022 by Kian 1 Quote Link to comment Share on other sites More sharing options...
Sam666 Posted December 19, 2022 Author Share Posted December 19, 2022 35 minutes ago, Kian said: You should post your object/array. We can't do much with encoded strings. Anyway there's a catch that could be confusing. Maybe this is the mistake you are making. Here is how you add one product defining two custom fields in PHP: $postData = [ 'clientid' => 1, 'pid' => [ 1 ], 'billingcycle' => [ 'annually' ], 'customfields' => [ base64_encode(serialize([ 6 => 'Hello', 7 => 'World' ])) ], 'paymentmethod' => 'banktransfer', ]; $results = localAPI('AddOrder', $postData); As you can see there's a single array in customfields array since we're adding one product. The two custom fields are defined with two key-value paris. This is the array that needs to be serialized and encoded. Let's repeat the process but this time with an order that contains three products: $postData = [ 'clientid' => 1, 'pid' => [ 1, 1, 3 ], 'billingcycle' => [ 'annually' ], 'customfields' => [ base64_encode(serialize([ 6 => 'First', 7 => 'Produtc' ])), base64_encode(serialize([ 6 => 'Second', 7 => 'Produtc' ])), base64_encode(serialize([ 6 => 'Third', 7 => 'Produtc' ])), ], 'paymentmethod' => 'banktransfer', ]; $results = localAPI('AddOrder', $postData); As you can see each sub-array of "customfields" needs to be serialized and encoded individually. The first sub-array is for the first product (pid 1), the second is for the second (another pid 1) and the third for the third (pid 3). Make sure you are serializing and encoding the array in this structure. In essence there must be one serialize and one encode for each product. Thank you very much @Kian for your help. I have fixed this issue, I make a mistake that base64_encoded and serialized each custom field separately, I should base64_encoded and serialized all fields for the same product. My problem solved, thanks again @Kian 0 Quote Link to comment Share on other sites More sharing options...
Naseem shah Posted August 7 Share Posted August 7 'customfields' => [ base64_encode(serialize([ 48 => 'Hello world' ])) ], I'm using this code to add a custom field to my Order API, but the custom field value is showing up as empty when I place an order. 0 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.