diff --git a/app/Models/SystemLog.php b/app/Models/SystemLog.php index 9f43d5bf42fe..f7b517314226 100644 --- a/app/Models/SystemLog.php +++ b/app/Models/SystemLog.php @@ -36,6 +36,7 @@ class SystemLog extends Model const TYPE_LEDGER = 302; const TYPE_FAILURE = 303; const TYPE_CHECKOUT = 304; + const TYPE_AUTHORIZE = 305; const TYPE_QUOTA_EXCEEDED = 400; const TYPE_UPSTREAM_FAILURE = 401; diff --git a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php index 0482c3c2422d..71c4bb8d563e 100644 --- a/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php +++ b/app/PaymentDrivers/Authorize/AuthorizeCreditCard.php @@ -12,12 +12,15 @@ namespace App\PaymentDrivers\Authorize; +use App\Factory\PaymentFactory; use App\Models\ClientGatewayToken; use App\Models\GatewayType; +use App\Models\Payment; use App\PaymentDrivers\AuthorizePaymentDriver; use App\PaymentDrivers\Authorize\AuthorizeCreateCustomer; use App\PaymentDrivers\Authorize\ChargePaymentProfile; use App\Utils\Traits\MakesHash; +use Illuminate\Support\Carbon; /** * Class AuthorizeCreditCard @@ -79,11 +82,41 @@ class AuthorizeCreditCard { $client_gateway_token = ClientGatewayToken::find($this->decodePrimaryKey($request->token)); - $response = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($client_gateway_token->gateway_customer_reference, $client_gateway_token->token, $request->input('amount')); - + $data = (new ChargePaymentProfile($this->authorize))->chargeCustomerProfile($client_gateway_token->gateway_customer_reference, $client_gateway_token->token, $request->input('amount')); + + $this->handleResponse($data, $request); } - private function handleResponse($response) + private function handleResponse($data, $request) + { + //info(print_r( $response->getTransactionResponse()->getMessages(),1)); + + $response = $data['response']; + + if($response != null && $response->getMessages()->getResultCode() == "Ok") + return $this->processSuccessfulResponse($data, $request); + + return $this->processFailedResponse($data, $request); + } + + private function processSuccessfulResponse($data, $request) + { + //create a payment record and fire notifications and then return + + $payment = PaymentFactory::create($this->authorize->client->company_id, $this->authorize->client->user_id); + $payment->client_id = $this->client->id; + $payment->company_gateway_id = $this->authorize->company_gateway->id; + $payment->status_id = Payment::STATUS_PAID; + $payment->currency_id = $this->authorize->client->getSetting('currency_id'); + $payment->date = Carbon::now(); + + $this->authorize->attachInvoices($payment, $request->hashed_ids); + + return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); + + } + + private function processFailedResponse($data) { } diff --git a/app/PaymentDrivers/Authorize/ChargePaymentProfile.php b/app/PaymentDrivers/Authorize/ChargePaymentProfile.php index 1b6a0e23b208..9a3249c15ec5 100644 --- a/app/PaymentDrivers/Authorize/ChargePaymentProfile.php +++ b/app/PaymentDrivers/Authorize/ChargePaymentProfile.php @@ -70,8 +70,14 @@ class ChargePaymentProfile info( "Charge Customer Profile APPROVED :" ); info(" Charge Customer Profile AUTH CODE : " . $tresponse->getAuthCode() ); info(" Charge Customer Profile TRANS ID : " . $tresponse->getTransId() ); + info(" Code : " . $tresponse->getMessages()[0]->getCode()); + info(" Description : " . $tresponse->getMessages()[0]->getDescription()); + //info(" Charge Customer Profile TRANS STATUS : " . $tresponse->getTransactionStatus() ); + //info(" Charge Customer Profile Amount : " . $tresponse->getAuthAmount()); + info(" Code : " . $tresponse->getMessages()[0]->getCode() ); info(" Description : " . $tresponse->getMessages()[0]->getDescription() ); + info(print_r($tresponse->getMessages()[0],1)); } else { @@ -80,6 +86,7 @@ class ChargePaymentProfile { info(" Error code : " . $tresponse->getErrors()[0]->getErrorCode() ); info(" Error message : " . $tresponse->getErrors()[0]->getErrorText() ); + info(print_r($tresponse->getErrors()[0],1)); } } } @@ -91,6 +98,7 @@ class ChargePaymentProfile { info(" Error code : " . $tresponse->getErrors()[0]->getErrorCode() ); info(" Error message : " . $tresponse->getErrors()[0]->getErrorText() ); + info(print_r($tresponse->getErrors()[0],1)); } else { @@ -99,7 +107,13 @@ class ChargePaymentProfile } } - return $response; + return [ + 'response' => $response, + 'amount' => $amount, + 'profile_id' => $profile_id, + 'payment_profile_id' => $payment_profile_id + ]; + } } \ No newline at end of file diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index d58a267d4459..bc6aeabc641a 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -14,6 +14,8 @@ namespace App\PaymentDrivers; use App\Models\Client; use App\Models\CompanyGateway; +use App\Models\Invoice; +use App\Models\Payment; use App\PaymentDrivers\AbstractPaymentDriver; use App\Utils\Traits\MakesHash; use App\Utils\Traits\SystemLogTrait; @@ -94,5 +96,25 @@ class BaseDriver extends AbstractPaymentDriver */ public function setPaymentMethod($payment_method_id){} + /** + * Helper method to attach invoices to a payment + * + * @param Payment $payment The payment + * @param array $hashed_ids The array of invoice hashed_ids + * @return Payment The payment object + */ + public function attachInvoices(Payment $payment, $hashed_ids): Payment + { + $transformed = $this->transformKeys($hashed_ids); + $array = is_array($transformed) ? $transformed : [$transformed]; + $invoices = Invoice::whereIn('id', $array) + ->whereClientId($this->client->id) + ->get(); + + $payment->invoices()->sync($invoices); + $payment->save(); + + return $payment; + } }