Skip to content

Commit de16898

Browse files
authored
Added custom httpBuildQuery - Fixes gh-51
1 parent 98f4483 commit de16898

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

QuickPay/API/Request.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function execute($request_type, $form = array())
159159

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

165165
// Store received headers in temporary memory file, remember sent headers
@@ -188,4 +188,19 @@ protected function execute($request_type, $form = array())
188188
// Return the response object.
189189
return new Response($response_code, $sent_headers, $received_headers, $response_data);
190190
}
191+
192+
/**
193+
* Improves http_build_query() for the QuickPay use case.
194+
*
195+
* Kept public for testing purposes.
196+
*
197+
* @param array $query
198+
* @return mixed|string
199+
*/
200+
public function httpBuildQuery($query)
201+
{
202+
$query = http_build_query($query);
203+
$query = preg_replace('/%5B[0-9]+%5D/i', '%5B%5D', $query);
204+
return $query;
205+
}
191206
}

Tests/api/RequestTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,38 @@ public function testFailedPostResponse()
6060

6161
$this->assertFalse($pingResponse->isSuccess());
6262
}
63+
64+
/**
65+
* Test function added to make sure that issue gh-54 is fixed.
66+
*/
67+
public function testBasket()
68+
{
69+
$basket = [];
70+
$basket[0] = [
71+
'qty' => 1,
72+
'item_no' => 2,
73+
'item_name' => 'Test 1',
74+
'item_price' => 100,
75+
'vat_rate' => 0.25,
76+
];
77+
$basket[1] = [
78+
'qty' => 1,
79+
'item_no' => 2,
80+
'item_name' => 'Test 2',
81+
'item_price' => 100,
82+
'vat_rate' => 0.25,
83+
];
84+
85+
$form = [
86+
'currency' => 'DKK',
87+
'order_id' => 1,
88+
'basket' => $basket,
89+
];
90+
91+
$query = $this->request->httpBuildQuery($form);
92+
93+
$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';
94+
95+
$this->assertEquals(urldecode($query), $expected);
96+
}
6397
}

0 commit comments

Comments
 (0)