This commit is contained in:
David Bomba 2024-06-13 19:59:29 +10:00
parent d23866932b
commit 50398360b9
4 changed files with 100 additions and 5 deletions

View File

@ -180,6 +180,38 @@ class PayPalBasePaymentDriver extends BaseDriver
}
public function getClientHash()
{
nlog($this->client->present()->name());
/** @var \App\Models\ClientGatewayToken $cgt */
$cgt = ClientGatewayToken::where('company_gateway_id', $this->company_gateway->id)
->where('client_id', $this->client->id)
->first();
if(!$cgt)
return '';
$client_reference = $cgt->gateway_customer_reference;
$secret = $this->company_gateway->getConfigField('secret');
$client_id = $this->company_gateway->getConfigField('clientId');
$response = Http::withBasicAuth($client_id, $secret)
->withHeaders(['Content-Type' => 'application/x-www-form-urlencoded'])
->withQueryParameters(['grant_type' => 'client_credentials','response_type' => 'id_token', 'target_customer_id' => $client_reference])
->post("{$this->api_endpoint_url}/v1/oauth2/token");
if($response->successful()) {
$data =$response->json();
return $data['id_token'] ?? '';
}
return '';
}
public function handleDuplicateInvoiceId(string $orderID)
{

View File

@ -100,6 +100,8 @@ class PayPalPPCPPaymentDriver extends PayPalBasePaymentDriver
$data['merchantId'] = $this->company_gateway->getConfigField('merchantId');
$data['currency'] = $this->client->currency()->code;
$data['guid'] = $this->risk_guid;
$data['identifier'] = "s:INN_".$this->company_gateway->getConfigField('merchantId')."_CHCK";
$data['pp_client_reference'] = $this->getClientHash();
if($this->gateway_type_id == 29)
return render('gateways.paypal.ppcp.card', $data);
@ -249,7 +251,6 @@ class PayPalPPCPPaymentDriver extends PayPalBasePaymentDriver
})->implode("\n");
$order = [
"intent" => "CAPTURE",
"payment_source" => $this->getPaymentSource(),
"purchase_units" => [

View File

@ -46,6 +46,12 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver
$data['gateway_type_id'] = $this->gateway_type_id;
$data['currency'] = $this->client->currency()->code;
$data['guid'] = $this->risk_guid;
$data['identifier'] = "s:INN_ACDC_CHCK";
$data['pp_client_reference'] = $this->getClientHash();
nlog($data['guid']);
nlog($data['order_id']);
if($this->gateway_type_id == 29)
return render('gateways.paypal.ppcp.card', $data);
@ -270,6 +276,23 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver
nlog($r->json());
$response = $r->json();
if($r->status() == 422) {
//handle conditions where the client may need to try again.
$_invoice = collect($this->payment_hash->data->invoices)->first();
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($_invoice->invoice_id));
$new_invoice_number = $invoice->number."_".Str::random(5);
$order['purchase_units'][0]['invoice_id'] = $new_invoice_number;
$r = $this->gatewayRequest('/v2/checkout/orders', 'post', $order);
nlog($r->json());
$response = $r->json();
}
if(!isset($response['id']))
$this->handleProcessingFailure($response);
@ -304,6 +327,9 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver
$orderId = $response['orderID'];
$r = $this->gatewayRequest("/v1/checkout/orders/{$orderId}/", 'delete', ['body' => '']);
nlog("token payyy");
nlog($r->body());
$data['amount_with_fee'] = $this->payment_hash->data->amount_with_fee;
$data["payment_source"] = [
"card" => [
@ -318,8 +344,38 @@ class PayPalRestPaymentDriver extends PayPalBasePaymentDriver
$orderId = $this->createOrder($data);
$r = $this->gatewayRequest("/v2/checkout/orders/{$orderId}", 'get', ['body' => '']);
// $r = $this->gatewayRequest("/v2/checkout/orders/{$orderId}", 'get', ['body' => '']);
try {
$r = $this->gatewayRequest("/v2/checkout/orders/{$orderId}", 'get', ['body' => '']);
if($r->status() == 422) {
//handle conditions where the client may need to try again.
nlog("hit 422");
$r = $this->handleDuplicateInvoiceId($orderId);
}
} catch(\Exception $e) {
//Rescue for duplicate invoice_id
if(stripos($e->getMessage(), 'DUPLICATE_INVOICE_ID') !== false) {
nlog("hit 422 in exception");
$r = $this->handleDuplicateInvoiceId($orderId);
}
}
$response = $r->json();
$data = [

View File

@ -78,22 +78,28 @@
<script type="application/json" fncls="fnparams-dede7cc5-15fd-4c75-a9f4-36c430ee3a99">
{
"f":"{{ $guid }}",
"s":"paypal.card" // unique ID for each web page
"s":"{{ $identifier }}" // unique ID for each web page
}
</script>
<script type="text/javascript" src="https://c.paypal.com/da/r/fb.js"></script>
@if(isset($merchantId))
<script src="https://www.paypal.com/sdk/js?client-id={!! $client_id !!}&merchantId={!! $merchantId !!}&components=card-fields" data-partner-attribution-id="invoiceninja_SP_PPCP"></script>
<script src="https://www.paypal.com/sdk/js?client-id={!! $client_id !!}&merchantId={!! $merchantId !!}&components=card-fields,buttons" data-user-id-token="{!! $pp_client_reference !!}" data-partner-attribution-id="invoiceninja_SP_PPCP"></script>
@else
<script src="https://www.paypal.com/sdk/js?client-id={!! $client_id !!}&components=card-fields" data-partner-attribution-id="invoiceninja_SP_PPCP"></script>
<script src="https://www.paypal.com/sdk/js?client-id={!! $client_id !!}&components=card-fields,buttons" data-user-id-token="{!! $pp_client_reference !!}" data-partner-attribution-id="invoiceninja_SP_PPCP"></script>
@endif
<script>
const clientId = "{{ $client_id }}";
const orderId = "{!! $order_id !!}";
const buttons = paypal.Buttons();
@if(strlen($pp_client_reference) > 1)
buttons.render('#paypal-button-container');
@endif
const cardField = paypal.CardFields({
client: clientId,
createOrder: function(data, actions) {