Skip to content

Commit

Permalink
Issue #64 handle "failed payment" response type in the response factory
Browse files Browse the repository at this point in the history
  • Loading branch information
judgej committed Sep 1, 2021
1 parent 211ff57 commit bb4753a
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/Factory/ResponseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,50 +42,70 @@ public static function fromHttpResponse(ResponseInterface $response)
public static function fromData($data, $httpCode = null)
{
// An error or error collection.

if ($httpCode >= Http::BAD_REQUEST || Helper::dataGet($data, 'errors')) {
// 4xx and 5xx errors.
// Return an error collection.
return Response\ErrorCollection::fromData($data, $httpCode);
}

// Session key.

if (Helper::dataGet($data, 'merchantSessionKey') && Helper::dataGet($data, 'expiry')) {
return Response\SessionKey::fromData($data, $httpCode);
}

// A card identifier.

if (Helper::dataGet($data, 'cardIdentifier') || Helper::dataGet($data, 'card-identifier')) {
return Response\CardIdentifier::fromData($data, $httpCode);
}

// A payment.

if (Helper::dataGet($data, 'transactionId')) {
if (Helper::dataGet($data, 'transactionType') == AbstractRequest::TRANSACTION_TYPE_PAYMENT) {
return Response\Payment::fromData($data, $httpCode);
}
}

// A repeat payment.

if (Helper::dataGet($data, 'transactionId')) {
if (Helper::dataGet($data, 'transactionType') == AbstractRequest::TRANSACTION_TYPE_REPEAT) {
return Response\Repeat::fromData($data, $httpCode);
}
}

// A refund payment.

if (Helper::dataGet($data, 'transactionId')) {
if (Helper::dataGet($data, 'transactionType') == AbstractRequest::TRANSACTION_TYPE_REFUND) {
return Response\Refund::fromData($data, $httpCode);
}
}

// A deferred payment.

if (Helper::dataGet($data, 'transactionId')) {
if (Helper::dataGet($data, 'transactionType') == AbstractRequest::TRANSACTION_TYPE_DEFERRED) {
return Response\Deferred::fromData($data, $httpCode);
}
}

// A failed payment.
// This isn't documented, but it is a payment with no transactionType.
// It is returned, for example, when 3DS v2 fails user authentication.
// Just dump it into a Payment to access isSucess().

if (Helper::dataGet($data, 'transactionId')) {
if (Helper::dataGet($data, 'paymentMethod')
&& Helper::dataGet($data, 'amount')
&& Helper::dataGet($data, 'transactionType') === null) {
return Response\Payment::fromData($data, $httpCode);
}
}

// 3D Secure response.
// This is the simplest of all the messages - just a status and nothing else.
// Make sure there is no transactionType field.
Expand Down Expand Up @@ -139,13 +159,13 @@ public static function fromData($data, $httpCode = null)
return Response\InstructionCollection::fromData($data, $httpCode);
}

// A 3DS v2 callback result, aka notification (this is a server request).
// A 3DS v2 callback successful result, aka notification (this is a server request).

if (Helper::dataGet($data, 'cres')) {
return ServerRequest\Secure3Dv2Notification::fromData($data, $httpCode);
}

// A 3DS v1 callbacl result (this is a server request).
// A 3DS v1 callback successful result (this is a server request).

if (Helper::dataGet($data, 'PaRes')) {
return ServerRequest\Secure3DAcs::fromData($data, $httpCode);
Expand Down

0 comments on commit bb4753a

Please sign in to comment.