浏览代码

apple支付修改

llp 4 天之前
父节点
当前提交
835d764ac9

+ 8 - 1
packages/Longyi/Pay/Applepay/src/Http/Controllers/ApplepayController.php

@@ -22,7 +22,14 @@ class ApplepayController extends Controller
 
 
     public function hook()
     public function hook()
     {
     {
-        request()->all();
+        $header = request()->header();
+        $rawBody = request()->getContent();
+        $event = json_decode($rawBody, true);
+        $signatureResult = $this->applepay->webhookSignature($header, $event);
+        if (!$signatureResult) {
+            @header("HTTP/1.0 400 Bad Request");
+            exit;
+        }
         $orderId = $event['resource']['invoice_id'];
         $orderId = $event['resource']['invoice_id'];
         $transactionId = $event['resource']['id'];
         $transactionId = $event['resource']['id'];
         $type = $event['event_type'];
         $type = $event['event_type'];

+ 32 - 2
packages/Longyi/Pay/Applepay/src/Payment/Applepay.php

@@ -17,8 +17,8 @@ class Applepay extends Payment
      */
      */
     protected $code  = 'applepay';
     protected $code  = 'applepay';
     protected $clientId;
     protected $clientId;
-    protected $apikey;
     protected $secret;
     protected $secret;
+    protected $webhookId;
     protected $currentOrder;
     protected $currentOrder;
     protected $prefix = 'QQS';
     protected $prefix = 'QQS';
     public $createOrderApi = 'https://api-m.sandbox.paypal.com/v2/checkout/orders';
     public $createOrderApi = 'https://api-m.sandbox.paypal.com/v2/checkout/orders';
@@ -45,7 +45,7 @@ class Applepay extends Payment
         }
         }
         $this->clientId = $this->getConfigData('client_id');
         $this->clientId = $this->getConfigData('client_id');
         $this->apikey = $this->getConfigData('secret_id');
         $this->apikey = $this->getConfigData('secret_id');
-        $this->secret = $this->getConfigData('webhook_id');
+        $this->webhookId = $this->getConfigData('webhook_id');
     }
     }
     /**
     /**
      * Get redirect url.
      * Get redirect url.
@@ -155,6 +155,36 @@ class Applepay extends Payment
         return $lineItems;
         return $lineItems;
     }
     }
 
 
+    public function webhookSignature($header, $data)
+    {
+        $data = [
+            'transmission_id' => $header['Paypal-Transmission-Id'],
+            'transmission_time' => $header['Paypal-Transmission-Time'],
+            'cert_url' => $header['Paypal-Cert-Url'],
+            'auth_algo' => $header['Paypal-Auth-Algo'],
+            'transmission_sig' => $header['Paypal-Transmission-Sig'],
+            'webhook_id' => $this->webhookId,
+            'webhook_event' => $data
+        ];
+        $client = new Client();
+        $response = $client->request('POST', $this->sigApi, [
+            'headers' => [
+                'Accept' => 'application/json',
+                'Content-Type' => 'application/json',
+                'Authorization' =>  'Basic '. base64_encode("{$this->apikey}:{$this->secret}")
+            ],
+            'json' => $data,
+            'timeout' => 30,
+            'verify' => false
+        ]);
+        $resultObject = json_decode($response->getBody()->getContents());
+        if ($resultObject->verification_status == 'SUCCESS') {
+            return true;
+        } else {
+            return false;
+        }
+    }
+
     //成功修改状态
     //成功修改状态
     public function paymentSucceeded($orderId, $transactionId)
     public function paymentSucceeded($orderId, $transactionId)
     {
     {