Skip to content

Commit f6d42c9

Browse files
authored
Merge pull request #3 from niels-nijens/change-capture-successful-when-already-captured
Change an unsuccessful capture to successful when all payments are already captured
2 parents 4dc1aa8 + 6e7fc20 commit f6d42c9

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/Message/CaptureRequest.php

+33
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ protected function runTransaction(SoapClient $soapClient, array $data): stdClass
5454
unset($data['paymentOrderKey']);
5555

5656
$captureResponse = $soapClient->__soapCall('capture', [$data]);
57+
$this->modifyCaptureResponseToSuccessfulWhenAlreadyCaptured($captureResponse, $statusResponse);
5758

5859
return $this->mergeResponses($statusResponse, $captureResponse);
5960
}
@@ -116,6 +117,38 @@ private function isPaymentCaptured(stdClass $payment): bool
116117
return isset($payment->authorization->capture);
117118
}
118119

120+
/**
121+
* Modifies a erroneous capture response to successful when the payments are already captured.
122+
*
123+
* This is required to facilitate the same workflow for both directly captured payment methods (like iDeal)
124+
* and delayed captured payment methods (like ELV).
125+
*
126+
* @param stdClass $captureResponse
127+
* @param stdClass $statusResponse
128+
*/
129+
private function modifyCaptureResponseToSuccessfulWhenAlreadyCaptured(
130+
stdClass $captureResponse,
131+
stdClass $statusResponse
132+
): void {
133+
if (isset($captureResponse->captureSuccess)) {
134+
return;
135+
}
136+
137+
if (isset($statusResponse->statusSuccess) === false) {
138+
return;
139+
}
140+
141+
$statusResponseApproximateTotals = $statusResponse->statusSuccess->report->approximateTotals;
142+
if ($statusResponseApproximateTotals->totalRegistered !== $statusResponseApproximateTotals->totalCaptured) {
143+
return;
144+
}
145+
146+
$captureResponse->captureSuccess = new stdClass();
147+
$captureResponse->captureSuccess->success = new stdClass();
148+
$captureResponse->captureSuccess->success->code = 'SUCCESS';
149+
$captureResponse->captureSuccess->success->_ = $captureResponse->captureErrors->error->_;
150+
}
151+
119152
/**
120153
* Returns an stdClass with the multiple responses.
121154
*

tests/Message/CaptureRequestTest.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -237,13 +237,12 @@ public function testSendNotSuccessfulCaptureWhenStatusRequestFails(): void
237237

238238
/**
239239
* Tests if {@see CaptureRequest::send} returns an unsuccessful {@see CaptureResponse} when trying to capture
240-
* an already captured payment.
240+
* an incorrect payment ID.
241241
*
242242
* @depends testSendSuccessfulCapture
243243
*/
244244
public function testSendNotSuccessfulCaptureWhenPaymentIdIncorrect(): void
245245
{
246-
$this->markTestIncomplete();
247246
$this->soapClientMock->expects($this->exactly(2))
248247
->method('__soapCall')
249248
->withConsecutive(
@@ -290,12 +289,12 @@ public function testSendNotSuccessfulCaptureWhenPaymentIdIncorrect(): void
290289
}
291290

292291
/**
293-
* Tests if {@see CaptureRequest::send} returns an unsuccessful {@see CaptureResponse} when trying to capture
292+
* Tests if {@see CaptureRequest::send} returns a successful {@see CaptureResponse} when trying to capture
294293
* an already captured payment.
295294
*
296295
* @depends testSendSuccessfulCapture
297296
*/
298-
public function testSendNotSuccessfulCaptureWhenAlreadyCaptured(): void
297+
public function testSendSuccessfulCaptureWhenAlreadyCaptured(): void
299298
{
300299
$this->soapClientMock->expects($this->exactly(2))
301300
->method('__soapCall')
@@ -334,9 +333,14 @@ public function testSendNotSuccessfulCaptureWhenAlreadyCaptured(): void
334333

335334
$response = $this->request->send();
336335

337-
$this->assertFalse($response->isSuccessful());
336+
$this->assertTrue($response->isSuccessful());
337+
$this->assertSame('No amount authorized available to capture.', $response->getMessage());
338338

339339
$expectedData = $this->createCaptureAlreadyCapturedErrorResponse();
340+
$expectedData->captureSuccess = new stdClass();
341+
$expectedData->captureSuccess->success = new stdClass();
342+
$expectedData->captureSuccess->success->code = 'SUCCESS';
343+
$expectedData->captureSuccess->success->_ = 'No amount authorized available to capture.';
340344
$expectedData->statusSuccess = $this->createStatusSuccessResponseWithAlreadyCapturedPayment()->statusSuccess;
341345

342346
$this->assertEquals($expectedData, $response->getData());

0 commit comments

Comments
 (0)