go_cardless = $go_cardless; $this->go_cardless->init(); } /** * Handle authorization for SEPA. * * @param array $data * @return Redirector|RedirectResponse|void */ public function authorizeView(array $data) { $session_token = \Illuminate\Support\Str::uuid()->toString(); try { $redirect = $this->go_cardless->gateway->redirectFlows()->create([ 'params' => [ 'scheme' => 'sepa_core', 'session_token' => $session_token, 'success_redirect_url' => route('client.payment_methods.confirm', [ 'method' => GatewayType::SEPA, 'session_token' => $session_token, ]), 'prefilled_customer' => [ 'given_name' => auth('contact')->user()->first_name, 'family_name' => auth('contact')->user()->last_name, 'email' => auth('contact')->user()->email, 'address_line1' => auth('contact')->user()->client->address1, 'city' => auth('contact')->user()->client->city, 'postal_code' => auth('contact')->user()->client->postal_code, ], ], ]); return redirect( $redirect->redirect_url ); } catch (\Exception $exception) { return $this->processUnsuccessfulAuthorization($exception); } } /** * Handle unsuccessful authorization for SEPA. * * @param Exception $exception * @return void */ public function processUnsuccessfulAuthorization(\Exception $exception): void { $this->go_cardless->sendFailureMail($exception->getMessage()); SystemLogger::dispatch( $exception->getMessage(), SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_GOCARDLESS, $this->go_cardless->client, $this->go_cardless->client->company, ); throw new PaymentFailed($exception->getMessage(), $exception->getCode()); } /** * Handle authorization response for SEPA. * * @param Request $request * @return RedirectResponse|void */ public function authorizeResponse(Request $request) { try { $redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete( $request->redirect_flow_id, ['params' => [ 'session_token' => $request->session_token ]], ); $payment_meta = new \stdClass; $payment_meta->brand = ctrans('texts.sepa'); $payment_meta->type = GatewayType::SEPA; $payment_meta->state = 'authorized'; $data = [ 'payment_meta' => $payment_meta, 'token' => $redirect_flow->links->mandate, 'payment_method_id' => GatewayType::SEPA, ]; $payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $redirect_flow->links->customer]); return redirect()->route('client.payment_methods.show', $payment_method->hashed_id); } catch (\Exception $exception) { return $this->processUnsuccessfulAuthorization($exception); } } /** * Payment view for SEPA. * * @param array $data * @return View */ public function paymentView(array $data): View { $data['gateway'] = $this->go_cardless; $data['amount'] = $this->go_cardless->convertToGoCardlessAmount($data['total']['amount_with_fee'], $this->go_cardless->client->currency()->precision); $data['currency'] = $this->go_cardless->client->getCurrencyCode(); return render('gateways.gocardless.sepa.pay', $data); } /** * Handle the payment page for SEPA. * * @param PaymentResponseRequest $request * @return RedirectResponse|App\PaymentDrivers\GoCardless\never|void */ public function paymentResponse(PaymentResponseRequest $request) { $token = ClientGatewayToken::find( $this->decodePrimaryKey($request->source) )->firstOrFail(); $this->go_cardless->ensureMandateIsReady($token); try { $payment = $this->go_cardless->gateway->payments()->create([ 'params' => [ 'amount' => $request->amount, 'currency' => $request->currency, 'metadata' => [ 'payment_hash' => $this->go_cardless->payment_hash->hash, ], 'links' => [ 'mandate' => $token->token, ], ], ]); if ($payment->status === 'pending_submission') { return $this->processPendingPayment($payment, ['token' => $token->hashed_id]); } return $this->processUnsuccessfulPayment($payment); } catch (\Exception $exception) { throw new PaymentFailed($exception->getMessage(), $exception->getCode()); } } /** * Handle pending payments for Direct Debit. * * @param ResourcesPayment $payment * @param array $data * @return RedirectResponse */ public function processPendingPayment(\GoCardlessPro\Resources\Payment $payment, array $data = []) { $data = [ 'payment_method' => $data['token'], 'payment_type' => PaymentType::SEPA, 'amount' => $this->go_cardless->payment_hash->data->amount_with_fee, 'transaction_reference' => $payment->id, 'gateway_type_id' => GatewayType::SEPA, ]; $payment = $this->go_cardless->createPayment($data, Payment::STATUS_PENDING); SystemLogger::dispatch( ['response' => $payment, 'data' => $data], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_GOCARDLESS, $this->go_cardless->client, $this->go_cardless->client->company, ); return redirect()->route('client.payments.show', ['payment' => $this->go_cardless->encodePrimaryKey($payment->id)]); } /** * Process unsuccessful payments for Direct Debit. * * @param ResourcesPayment $payment * @return never */ public function processUnsuccessfulPayment(\GoCardlessPro\Resources\Payment $payment) { $this->go_cardless->sendFailureMail( $payment->status ); $message = [ 'server_response' => $payment, 'data' => $this->go_cardless->payment_hash->data, ]; SystemLogger::dispatch( $message, SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_GOCARDLESS, $this->go_cardless->client, $this->go_cardless->client->company, ); throw new PaymentFailed('Failed to process the payment.', 500); } }