| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | namespace Worldline\Connect\Sdk\V1; |
| 7: | |
| 8: | use Worldline\Connect\Sdk\Domain\DataObject; |
| 9: | use Worldline\Connect\Sdk\V1\Domain\CreatePaymentResult; |
| 10: | use Worldline\Connect\Sdk\V1\Domain\PaymentErrorResponse; |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | class DeclinedPaymentException extends ResponseException |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | public function __construct($httpStatusCode, DataObject $response, $message = null) |
| 25: | { |
| 26: | if (is_null($message)) { |
| 27: | $message = DeclinedPaymentException::buildMessage($response); |
| 28: | } |
| 29: | parent::__construct($httpStatusCode, $response, $message); |
| 30: | } |
| 31: | |
| 32: | private static function buildMessage(DataObject $response) |
| 33: | { |
| 34: | if ($response instanceof PaymentErrorResponse && $response->paymentResult != null && $response->paymentResult->payment != null) { |
| 35: | $payment = $response->paymentResult->payment; |
| 36: | return "declined payment '$payment->id' with status '$payment->status'"; |
| 37: | } |
| 38: | return 'the Worldline Global Collect platform returned a declined payment response'; |
| 39: | } |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | public function getCreatePaymentResult() |
| 45: | { |
| 46: | $responseVariables = get_object_vars($this->getResponse()); |
| 47: | if (!array_key_exists('paymentResult', $responseVariables)) { |
| 48: | return new CreatePaymentResult(); |
| 49: | } |
| 50: | $paymentResult = $responseVariables['paymentResult']; |
| 51: | if (!($paymentResult instanceof CreatePaymentResult)) { |
| 52: | return new CreatePaymentResult(); |
| 53: | } |
| 54: | return $paymentResult; |
| 55: | } |
| 56: | } |
| 57: | |