Skip to content

Commit

Permalink
Added custom httpBuildQuery - Fixes gh-51
Browse files Browse the repository at this point in the history
  • Loading branch information
lsolesen authored Sep 14, 2017
1 parent 98f4483 commit de16898
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
17 changes: 16 additions & 1 deletion QuickPay/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ protected function execute($request_type, $form = array())

// If additional data is delivered, we will send it along with the API request
if (is_array($form) && ! empty($form)) {
curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, http_build_query($form, '', '&'));
curl_setopt($this->client->ch, CURLOPT_POSTFIELDS, $this->httpBuildQuery($form, '', '&'));
}

// Store received headers in temporary memory file, remember sent headers
Expand Down Expand Up @@ -188,4 +188,19 @@ protected function execute($request_type, $form = array())
// Return the response object.
return new Response($response_code, $sent_headers, $received_headers, $response_data);
}

/**
* Improves http_build_query() for the QuickPay use case.
*
* Kept public for testing purposes.
*
* @param array $query
* @return mixed|string
*/
public function httpBuildQuery($query)
{
$query = http_build_query($query);
$query = preg_replace('/%5B[0-9]+%5D/i', '%5B%5D', $query);
return $query;
}
}
34 changes: 34 additions & 0 deletions Tests/api/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,38 @@ public function testFailedPostResponse()

$this->assertFalse($pingResponse->isSuccess());
}

/**
* Test function added to make sure that issue gh-54 is fixed.
*/
public function testBasket()
{
$basket = [];
$basket[0] = [
'qty' => 1,
'item_no' => 2,
'item_name' => 'Test 1',
'item_price' => 100,
'vat_rate' => 0.25,
];
$basket[1] = [
'qty' => 1,
'item_no' => 2,
'item_name' => 'Test 2',
'item_price' => 100,
'vat_rate' => 0.25,
];

$form = [
'currency' => 'DKK',
'order_id' => 1,
'basket' => $basket,
];

$query = $this->request->httpBuildQuery($form);

$expected = 'currency=DKK&order_id=1&basket[][qty]=1&basket[][item_no]=2&basket[][item_name]=Test 1&basket[][item_price]=100&basket[][vat_rate]=0.25&basket[][qty]=1&basket[][item_no]=2&basket[][item_name]=Test 2&basket[][item_price]=100&basket[][vat_rate]=0.25';

$this->assertEquals(urldecode($query), $expected);
}
}

0 comments on commit de16898

Please sign in to comment.