Jump to content

How can I pass multiple custom fields via API AddOrder?


Sam666

Recommended Posts

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.

  1. ('YToxOntpOjczO3M6NjoiU2VydmVyIjt9', 'YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==')
  2. (YToxOntpOjczO3M6NjoiU2VydmVyIjt9, YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==)
  3. ('YToxOntpOjczO3M6NjoiU2VydmVyIjt9', 'YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==')
  4. Array( [0] => YToxOntpOjczO3M6NjoiU2VydmVyIjt9 [1] => YToxOntpOjcwO3M6NzI6IlNvdGVjdGlvbiI7fQ==)
  5. 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.

Link to comment
Share on other sites

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 by Kian
Link to comment
Share on other sites

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use & Guidelines and understand your posts will initially be pre-moderated