Paypal CC

This commit is contained in:
David Bomba 2023-10-16 15:48:45 +11:00
parent 0033fee578
commit 55059d1227
2 changed files with 94 additions and 4 deletions

View File

@ -41,7 +41,7 @@ class PayPalPPCPPaymentDriver extends BaseDriver
private string $paypal_payment_method = '';
protected ?string $access_token = null;
protected mixed $access_token = null;
protected ?Carbon $token_expiry = null;
@ -61,6 +61,12 @@ class PayPalPPCPPaymentDriver extends BaseDriver
// 7 => 'sofort'
];
/**
* Return an array of
* enabled gateway payment methods
*
* @return array
*/
public function gatewayTypes(): array
{
@ -141,16 +147,27 @@ class PayPalPPCPPaymentDriver extends BaseDriver
$this->init();
$data['gateway'] = $this;
$this->payment_hash->data = array_merge((array) $this->payment_hash->data, ['amount' => $data['total']['amount_with_fee']]);
$this->payment_hash->save();
$data['client_id'] = $this->company_gateway->getConfigField('clientId');
$data['token'] = $this->access_token;
$data['token'] = $this->getClientToken();
$data['order_id'] = $this->createOrder($data);
$data['funding_options'] = $this->paypal_payment_method;
return render('gateways.paypal.pay', $data);
return render('gateways.paypal.credit_card.pay', $data);
}
private function getClientToken(): string
{
$r = $this->gatewayRequest('/v1/identity/generate-token', 'post', ['body' => '']);
if($r->successful())
return $r->json()['client_token'];
throw new PaymentFailed('Unable to gain client token from Paypal. Check your configuration', 401);
}
@ -286,6 +303,8 @@ class PayPalPPCPPaymentDriver extends BaseDriver
$r = $this->gatewayRequest('/v2/checkout/orders', 'post', $order);
nlog($r->json());
return $r->json()['id'];
}

View File

@ -0,0 +1,71 @@
@extends('portal.ninja2020.layout.payments', ['gateway_title' => ctrans('texts.payment_type_credit_card'), 'card_title' => 'PayPal'])
@section('gateway_head')
<link
rel="stylesheet"
type="text/css"
href="https://www.paypalobjects.com/webstatic/en_US/developer/docs/css/cardfields.css"
/>
@endsection
@section('gateway_content')
<form action="{{ route('client.payments.response') }}" method="post" id="server_response">
@csrf
<input type="hidden" name="payment_hash" value="{{ $payment_hash }}">
<input type="hidden" name="company_gateway_id" value="{{ $gateway->company_gateway->id }}">
<input type="hidden" name="gateway_response" id="gateway_response">
<input type="hidden" name="amount_with_fee" id="amount_with_fee" value="{{ $total['amount_with_fee'] }}"/>
</form>
<div class="alert alert-failure mb-4" hidden id="errors"></div>
<div id="paypal-button-container" class="paypal-button-container"></div>
@endsection
@section('gateway_footer')
@endsection
@push('footer')
<!-- Replace "test" with your own sandbox Business account app client ID -->
<script src="https://www.paypal.com/sdk/js?client-id={!! $client_id !!}&components=buttons,funding-eligibility&intent=capture" data-client-token="{!! $token !!}"></script>
<div id="paypal-button-container"></div>
<script>
paypal.Buttons({
env: "{{ $gateway->company_gateway->getConfigField('testMode') ? 'sandbox' : 'production' }}",
fundingSource: "card",
client: {
@if($gateway->company_gateway->getConfigField('testMode'))
sandbox: "{{ $gateway->company_gateway->getConfigField('clientId') }}"
@else
production: "{{ $gateway->company_gateway->getConfigField('clientId') }}"
@endif
},
// Order is created on the server and the order id is returned
createOrder: function(data, actions) {
return "{!! $order_id !!}"
},
// Finalize the transaction on the server after payer approval
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
document.getElementById("gateway_response").value =JSON.stringify( details );
document.getElementById("server_response").submit();
});
},
onError: function(err) {
console.log(err);
}
}).render('#paypal-button-container');
</script>
@endpush