mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 14:04:38 -04:00
Refactor for GoCardless Billing Flow
This commit is contained in:
parent
d7ff984818
commit
6e9ebd5779
@ -19,6 +19,12 @@ use App\Utils\Ninja;
|
|||||||
|
|
||||||
class SubscriptionController extends Controller
|
class SubscriptionController extends Controller
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* This function is used to display the subscription page.
|
||||||
|
*
|
||||||
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory
|
||||||
|
*/
|
||||||
|
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
// if (Ninja::isHosted()) {
|
// if (Ninja::isHosted()) {
|
||||||
@ -44,7 +50,6 @@ class SubscriptionController extends Controller
|
|||||||
*
|
*
|
||||||
* @param ShowRecurringInvoiceRequest $request
|
* @param ShowRecurringInvoiceRequest $request
|
||||||
* @param RecurringInvoice $recurring_invoice
|
* @param RecurringInvoice $recurring_invoice
|
||||||
* @return Factory|View
|
|
||||||
*/
|
*/
|
||||||
public function show(ShowRecurringInvoiceRequest $request, RecurringInvoice $recurring_invoice)
|
public function show(ShowRecurringInvoiceRequest $request, RecurringInvoice $recurring_invoice)
|
||||||
{
|
{
|
||||||
|
@ -172,11 +172,13 @@ class ACH implements MethodInterface
|
|||||||
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
|
$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 {
|
try {
|
||||||
$payment = $this->go_cardless->gateway->payments()->create([
|
$payment = $this->go_cardless->gateway->payments()->create([
|
||||||
'params' => [
|
'params' => [
|
||||||
// 'amount' => $request->amount,
|
// 'amount' => $request->amount,
|
||||||
'amount' => (int)rtrim(round($request->amount),0),
|
'amount' => $amount,
|
||||||
'currency' => $request->currency,
|
'currency' => $request->currency,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
|
@ -49,37 +49,97 @@ class DirectDebit implements MethodInterface
|
|||||||
* @return Redirector|RedirectResponse|void
|
* @return Redirector|RedirectResponse|void
|
||||||
*/
|
*/
|
||||||
public function authorizeView(array $data)
|
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();
|
$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 {
|
try {
|
||||||
$redirect = $this->go_cardless->gateway->redirectFlows()->create([
|
$brf = $this->go_cardless->gateway->billingRequestFlows()->create([
|
||||||
'params' => [
|
"params" => [
|
||||||
'session_token' => $session_token,
|
"redirect_uri" => route('client.payment_methods.confirm', [
|
||||||
'success_redirect_url' => route('client.payment_methods.confirm', [
|
'method' => GatewayType::DIRECT_DEBIT,
|
||||||
'method' => GatewayType::DIRECT_DEBIT,
|
'session_token' => $session_token,
|
||||||
'session_token' => $session_token,
|
'billing_request' => $response->id,
|
||||||
]),
|
]),
|
||||||
'prefilled_customer' => [
|
"exit_uri" => $exit_uri,
|
||||||
'given_name' => auth()->guard('contact')->user()->first_name ?: '',
|
"links" => [
|
||||||
'family_name' => auth()->guard('contact')->user()->last_name ?: '',
|
"billing_request" => $response->id
|
||||||
'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,
|
|
||||||
],
|
],
|
||||||
],
|
"show_redirect_buttons" => true,
|
||||||
|
"show_success_redirect_button" => true,
|
||||||
|
]
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return redirect(
|
return redirect($brf->authorisation_url);
|
||||||
$redirect->redirect_url
|
|
||||||
);
|
|
||||||
} catch (\Exception $exception) {
|
} catch (\Exception $exception) {
|
||||||
|
nlog($exception->getMessage());
|
||||||
return $this->processUnsuccessfulAuthorization($exception);
|
return $this->processUnsuccessfulAuthorization($exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handle unsuccessful authorization.
|
* Handle unsuccessful authorization.
|
||||||
*
|
*
|
||||||
@ -109,31 +169,61 @@ class DirectDebit implements MethodInterface
|
|||||||
*/
|
*/
|
||||||
public function authorizeResponse(Request $request)
|
public function authorizeResponse(Request $request)
|
||||||
{
|
{
|
||||||
try {
|
|
||||||
$redirect_flow = $this->go_cardless->gateway->redirectFlows()->complete(
|
try{
|
||||||
$request->redirect_flow_id,
|
|
||||||
['params' => [
|
$billing_request = $this->go_cardless->gateway->billingRequests()->get($request->billing_request);
|
||||||
'session_token' => $request->session_token,
|
|
||||||
]],
|
|
||||||
);
|
|
||||||
|
|
||||||
$payment_meta = new \stdClass;
|
$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->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 = [
|
$data = [
|
||||||
'payment_meta' => $payment_meta,
|
'payment_meta' => $payment_meta,
|
||||||
'token' => $redirect_flow->links->mandate,
|
'token' => $billing_request->mandate_request->links->mandate,
|
||||||
'payment_method_id' => $this->resolveScheme($redirect_flow->scheme),
|
'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);
|
return redirect()->route('client.payment_methods.show', $payment_method->hashed_id);
|
||||||
} catch (\Exception $exception) {
|
|
||||||
|
}
|
||||||
|
catch (\Exception $exception) {
|
||||||
return $this->processUnsuccessfulAuthorization($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
|
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()}";
|
$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 {
|
try {
|
||||||
$payment = $this->go_cardless->gateway->payments()->create([
|
$payment = $this->go_cardless->gateway->payments()->create([
|
||||||
'params' => [
|
'params' => [
|
||||||
// 'amount' => $request->amount,
|
// 'amount' => $request->amount,
|
||||||
'amount' => (int)rtrim(round($request->amount),0),
|
'amount' => $amount,
|
||||||
'currency' => $request->currency,
|
'currency' => $request->currency,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
|
@ -172,10 +172,12 @@ class SEPA implements MethodInterface
|
|||||||
$description = "Amount {$request->amount} from client {$this->go_cardless->client->present()->name()}";
|
$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 {
|
try {
|
||||||
$payment = $this->go_cardless->gateway->payments()->create([
|
$payment = $this->go_cardless->gateway->payments()->create([
|
||||||
'params' => [
|
'params' => [
|
||||||
'amount' => (int)rtrim(round($request->amount),0),
|
'amount' => $amount),
|
||||||
'currency' => $request->currency,
|
'currency' => $request->currency,
|
||||||
'description' => $description,
|
'description' => $description,
|
||||||
'metadata' => [
|
'metadata' => [
|
||||||
|
@ -131,7 +131,7 @@ class GoCardlessPaymentDriver extends BaseDriver
|
|||||||
}
|
}
|
||||||
|
|
||||||
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
|
public function tokenBilling(ClientGatewayToken $cgt, PaymentHash $payment_hash)
|
||||||
{
|
{nlog("here");
|
||||||
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
|
$amount = array_sum(array_column($payment_hash->invoices(), 'amount')) + $payment_hash->fee_total;
|
||||||
$converted_amount = $this->convertToGoCardlessAmount($amount, $this->client->currency()->precision);
|
$converted_amount = $this->convertToGoCardlessAmount($amount, $this->client->currency()->precision);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user