diff --git a/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php b/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php index b9ecf3802dc7..932f7cdcc613 100644 --- a/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php +++ b/app/PaymentDrivers/PayPal/PayPalBasePaymentDriver.php @@ -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) { diff --git a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php index ad8c764f37b6..45d356c239ad 100644 --- a/app/PaymentDrivers/PayPalPPCPPaymentDriver.php +++ b/app/PaymentDrivers/PayPalPPCPPaymentDriver.php @@ -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" => [ diff --git a/app/PaymentDrivers/PayPalRestPaymentDriver.php b/app/PaymentDrivers/PayPalRestPaymentDriver.php index fc0198037ffa..d79d719579dc 100644 --- a/app/PaymentDrivers/PayPalRestPaymentDriver.php +++ b/app/PaymentDrivers/PayPalRestPaymentDriver.php @@ -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 = [ diff --git a/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php b/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php index 438ff574cc94..bae844c8b19f 100644 --- a/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php +++ b/resources/views/portal/ninja2020/gateways/paypal/ppcp/card.blade.php @@ -78,22 +78,28 @@ @if(isset($merchantId)) - + @else - + @endif