diff --git a/app/Exceptions/PaymentRefundFailed.php b/app/Exceptions/PaymentRefundFailed.php index a5dee9cfdd8a..94a6fe455e01 100644 --- a/app/Exceptions/PaymentRefundFailed.php +++ b/app/Exceptions/PaymentRefundFailed.php @@ -26,8 +26,16 @@ class PaymentRefundFailed extends Exception */ public function render($request) { + + // $msg = 'Unable to refund the transaction'; + $msg = ctrans('texts.warning_local_refund'); + + if($this->getMessage() && strlen($this->getMessage()) >=1 ) + $msg = $this->getMessage(); + return response()->json([ - 'message' => 'Unable to refund the transaction', + 'message' => $msg, ], 401); + } } diff --git a/app/PaymentDrivers/BraintreePaymentDriver.php b/app/PaymentDrivers/BraintreePaymentDriver.php index d36f11a23eba..7c0cdd278685 100644 --- a/app/PaymentDrivers/BraintreePaymentDriver.php +++ b/app/PaymentDrivers/BraintreePaymentDriver.php @@ -124,24 +124,57 @@ class BraintreePaymentDriver extends BaseDriver { $this->init(); - try { + try{ + $response = $this->gateway->transaction()->refund($payment->transaction_reference, $amount); - - return [ - 'transaction_reference' => $response->id, - 'transaction_response' => json_encode($response), - 'success' => (bool)$response->success, - 'description' => $response->status, - 'code' => 0, - ]; + } catch (Exception $e) { - return [ + + $data = [ 'transaction_reference' => null, 'transaction_response' => json_encode($e->getMessage()), 'success' => false, 'description' => $e->getMessage(), 'code' => $e->getCode(), ]; + + SystemLogger::dispatch(['server_response' => null, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company); + + return $data; + } + + if($response->success) + { + + $data = [ + 'transaction_reference' => $response->id, + 'transaction_response' => json_encode($response), + 'success' => (bool)$response->success, + 'description' => $response->status, + 'code' => 0, + ]; + + SystemLogger::dispatch(['server_response' => $response, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company); + + return $data; + + } + else{ + + $error = $response->errors->deepAll()[0]; + + $data = [ + 'transaction_reference' => null, + 'transaction_response' => $response->errors->deepAll(), + 'success' => false, + 'description' => $error->message, + 'code' => $error->code, + ]; + + SystemLogger::dispatch(['server_response' => $response, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_BRAINTREE, $this->client, $this->client->company); + + return $data; + } } diff --git a/app/Services/Payment/RefundPayment.php b/app/Services/Payment/RefundPayment.php index 61ecfe7dd953..a1d733311e1e 100644 --- a/app/Services/Payment/RefundPayment.php +++ b/app/Services/Payment/RefundPayment.php @@ -81,7 +81,7 @@ class RefundPayment if ($response['success'] == false) { $this->payment->save(); - throw new PaymentRefundFailed(); + throw new PaymentRefundFailed($response['description']); } } } else {