Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

http_build_query and invalid basket #51

Closed
RBech opened this issue Apr 25, 2017 · 13 comments
Closed

http_build_query and invalid basket #51

RBech opened this issue Apr 25, 2017 · 13 comments
Assignees

Comments

@RBech
Copy link

RBech commented Apr 25, 2017

Using the code provided here https://kopy.io/p43cq#387KIDk5r4HH51 I'm trying to create a payment with the basket information supplied, however this results in {"message":"Validation error","errors":{"basket":["is invalid"]},"error_code":null} despite the supplying all the information needed according to the documentation.

I think the culprit may be http_build_query but I'm not sure what workaround would be needed, if I use the same data and perform a json_encoded request without using the php-client things are working as expected.

@lsolesen
Copy link
Contributor

lsolesen commented Apr 26, 2017 via email

@jkhwsd
Copy link

jkhwsd commented May 18, 2017

I have the same problem.

Using http_build_query on my data results in the following (replaced [, ] and added linebreak for readability):

currency=DKK&
order_id=test&
basket[0][qty]=1&
basket[0][item_no]=0001&
basket[0][item_name]=test1&
basket[0][item_price]=12345&
basket[0][vat_rate]=0.25 
basket[1][qty]=1&
basket[1][item_no]=0002&
basket[1][item_name]=test2&
basket[1][item_price]=98765&
basket[1][vat_rate]=0.25 

Those [0] and [1] are supposed to be [] or else the basket is invalid.
If you were to put this in to an array (avoiding nested arrays) like this $params['basket[][qty]'] = 1 which would work for a single item but the next item would override the previous since they have the same key.

@lsolesen
Copy link
Contributor

Please supply exact code used, so I can debug it?

@jkhwsd
Copy link

jkhwsd commented May 19, 2017

Something like this:

$basket = [];
foreach ($orderItems as $orderItem) {
    $basket[] = [
        'qty' => $orderItem->quantity,
        'item_no' => $orderItem->id,
        'item_name' => $orderItem->title,
        'item_price' => $orderItem->amount,
        'vat_rate' => 0.25,
    ];
}
$form = [
    'currency' => 'DKK',
    'order_id' => $orderId,
    'basket' => $basket,
];
$payment = $client->request->post('/payments', $form);

@lsolesen
Copy link
Contributor

Could you please try to add this method https://github.com/QuickPay/quickpay-php-client/pull/54/files#diff-1a98bf2e113e437e34e01b15690a1005R103 to the Request.php and change the http_build_query() used in execute() to that method to see whether it fixes the problem?

@lsolesen
Copy link
Contributor

or this alternative:

function handleArray($query) {
    $query = http_build_query($query);
    $query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
    return $query;
}

@jkhwsd
Copy link

jkhwsd commented May 22, 2017

I tried your alternative, and it seems to work.

I used it to the execute function line 163.

@lsolesen
Copy link
Contributor

lsolesen commented May 22, 2017 via email

@jkhwsd
Copy link

jkhwsd commented May 22, 2017

I used this one

function handleArray($query) {
    $query = http_build_query($query);
    $query = preg_replace('/%5B[0-9]+%5D/simU', '%5B%5D', $query);
    return $query;
}

I was wondering if all those pattern modifiers are necessary? The caseless (i) seems fine but the rest seems needless.

@jkhwsd
Copy link

jkhwsd commented Jun 30, 2017

Will there be released a new version with the error fixed?

@lsolesen
Copy link
Contributor

lsolesen commented Jun 30, 2017 via email

@lsolesen
Copy link
Contributor

@jkhwsd and @RBech Could you please review this pull request to see whether it fixes the issue properly? #54

@jkhwsd
Copy link

jkhwsd commented Jul 31, 2017

Looks good to me.

Sthing added a commit that referenced this issue Dec 16, 2019
Parameters for http_build_query() was not moved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants