Refactor for GoCardless Billing Flow

This commit is contained in:
David Bomba 2023-03-22 15:04:04 +11:00
parent d7ff984818
commit 6e9ebd5779
5 changed files with 138 additions and 37 deletions

View File

@ -19,6 +19,12 @@ use App\Utils\Ninja;
class SubscriptionController extends Controller
{
/**
* This function is used to display the subscription page.
*
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
*/
public function index()
{
// if (Ninja::isHosted()) {
@ -44,7 +50,6 @@ class SubscriptionController extends Controller
*
* @param ShowRecurringInvoiceRequest $request
* @param RecurringInvoice $recurring_invoice
* @return Factory|View
*/
public function show(ShowRecurringInvoiceRequest $request, RecurringInvoice $recurring_invoice)
{

View File

@ -172,11 +172,13 @@ class ACH implements MethodInterface
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
// 'amount' => $request->amount,
'amount' => (int)rtrim(round($request->amount),0),
'amount' => $amount,
'currency' => $request->currency,
'description' => $description,
'metadata' => [

View File

@ -49,37 +49,97 @@ class DirectDebit implements MethodInterface
* @return Redirector|RedirectResponse|void
*/
public function authorizeView(array $data)
{
return $this->billingRequestFlows($data);
// $session_token = \Illuminate\Support\Str::uuid()->toString();
// try {
// $redirect = $this->go_cardless->gateway->redirectFlows()->create([
// 'params' => [
// 'session_token' => $session_token,
// 'success_redirect_url' => route('client.payment_methods.confirm', [
// 'method' => GatewayType::DIRECT_DEBIT,
// 'session_token' => $session_token,
// ]),
// 'prefilled_customer' => [
// 'given_name' => auth()->guard('contact')->user()->first_name ?: '',
// 'family_name' => auth()->guard('contact')->user()->last_name ?: '',
// 'email' => auth()->guard('contact')->user()->email ?: '',
// 'address_line1' => auth()->guard('contact')->user()->client->address1 ?: '',
// 'city' => auth()->guard('contact')->user()->client->city ?: '',
// 'postal_code' => auth()->guard('contact')->user()->client->postal_code ?: '',
// 'country_code' => auth()->guard('contact')->user()->client->country->iso_3166_2,
// ],
// ],
// ]);
// return redirect(
// $redirect->redirect_url
// );
// } catch (\Exception $exception) {
// return $this->processUnsuccessfulAuthorization($exception);
// }
}
/**
* Response
* {
* "billing_request_flows": {
* "authorisation_url": "https://pay.gocardless.com/flow/static/billing_request?id=<br_id>",
* "lock_customer_details": false,
* "lock_bank_account": false,
* "auto_fulfil": true,
* "created_at": "2021-03-30T16:23:10.679Z",
* "expires_at": "2021-04-06T16:23:10.679Z",
* "redirect_uri": "https://my-company.com/completed",
* "links": {
* "billing_request": "BRQ123"
* }
* }
* }
*
*
*/
public function billingRequestFlows(array $data)
{
$session_token = \Illuminate\Support\Str::uuid()->toString();
$exit_uri = route('client.payment_methods.index');
$response = $this->go_cardless->gateway->billingRequests()->create([
"params" => [
"mandate_request" => [
"currency" => auth()->guard('contact')->user()->client->currency()->code
]
]
]);
try {
$redirect = $this->go_cardless->gateway->redirectFlows()->create([
'params' => [
'session_token' => $session_token,
'success_redirect_url' => route('client.payment_methods.confirm', [
'method' => GatewayType::DIRECT_DEBIT,
'session_token' => $session_token,
]),
'prefilled_customer' => [
'given_name' => auth()->guard('contact')->user()->first_name ?: '',
'family_name' => auth()->guard('contact')->user()->last_name ?: '',
'email' => auth()->guard('contact')->user()->email ?: '',
'address_line1' => auth()->guard('contact')->user()->client->address1 ?: '',
'city' => auth()->guard('contact')->user()->client->city ?: '',
'postal_code' => auth()->guard('contact')->user()->client->postal_code ?: '',
'country_code' => auth()->guard('contact')->user()->client->country->iso_3166_2,
$brf = $this->go_cardless->gateway->billingRequestFlows()->create([
"params" => [
"redirect_uri" => route('client.payment_methods.confirm', [
'method' => GatewayType::DIRECT_DEBIT,
'session_token' => $session_token,
'billing_request' => $response->id,
]),
"exit_uri" => $exit_uri,
"links" => [
"billing_request" => $response->id
],
],
"show_redirect_buttons" => true,
"show_success_redirect_button" => true,
]
]);
return redirect(
$redirect->redirect_url
);
return redirect($brf->authorisation_url);
} catch (\Exception $exception) {
nlog($exception->getMessage());
return $this->processUnsuccessfulAuthorization($exception);
}
}
/**
* Handle unsuccessful authorization.
*
@ -109,31 +169,61 @@ class DirectDebit implements MethodInterface
*/
public function authorizeResponse(Request $request)
{
try {
$redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
$request->redirect_flow_id,
['params' => [
'session_token' => $request->session_token,
]],
);
try{
$billing_request = $this->go_cardless->gateway->billingRequests()->get($request->billing_request);
$payment_meta = new \stdClass;
$payment_meta->brand = ctrans('texts.payment_type_direct_debit');
$payment_meta->brand = $billing_request->resources->customer_bank_account->bank_name;
$payment_meta->type = GatewayType::DIRECT_DEBIT;
$payment_meta->state = 'authorized';
$payment_meta->state = 'pending';
$payment_meta->last4 = $billing_request->resources->customer_bank_account->account_number_ending;
$data = [
'payment_meta' => $payment_meta,
'token' => $redirect_flow->links->mandate,
'payment_method_id' => $this->resolveScheme($redirect_flow->scheme),
'token' => $billing_request->mandate_request->links->mandate,
'payment_method_id' => $this->resolveScheme($billing_request->mandate_request->scheme),
];
$payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $redirect_flow->links->customer]);
$payment_method = $this->go_cardless->storeGatewayToken($data, ['gateway_customer_reference' => $billing_request->resources->customer->id]);
$mandate = $this->go_cardless->gateway->mandates()->get($billing_request->mandate_request->links->mandate);
nlog($mandate);
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
} catch (\Exception $exception) {
}
catch (\Exception $exception) {
return $this->processUnsuccessfulAuthorization($exception);
}
// 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.payment_type_direct_debit');
// $payment_meta->type = GatewayType::DIRECT_DEBIT;
// $payment_meta->state = 'authorized';
// $data = [
// 'payment_meta' => $payment_meta,
// 'token' => $redirect_flow->links->mandate,
// 'payment_method_id' => $this->resolveScheme($redirect_flow->scheme),
// ];
// $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);
// }
}
private function resolveScheme(string $scheme): int
@ -176,11 +266,13 @@ class DirectDebit implements MethodInterface
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
// 'amount' => $request->amount,
'amount' => (int)rtrim(round($request->amount),0),
'amount' => $amount,
'currency' => $request->currency,
'description' => $description,
'metadata' => [

View File

@ -172,10 +172,12 @@ class SEPA implements MethodInterface
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
}
$amount = $this->go_cardless->convertToGoCardlessAmount($this->go_cardless->payment_hash?->amount_with_fee(), $this->go_cardless->client->currency()->precision);
try {
$payment = $this->go_cardless->gateway->payments()->create([
'params' => [
'amount' => (int)rtrim(round($request->amount),0),
'amount' => $amount),
'currency' => $request->currency,
'description' => $description,
'metadata' => [

View File

@ -131,7 +131,7 @@ class GoCardlessPaymentDriver extends BaseDriver
}
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
{
{nlog("here");
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
$converted_amount = $this->convertToGoCardlessAmount($amount, $this->client->currency()->precision);