yingzi vor 2 Jahren
Ursprung
Commit
d2d59bfb87
2 geänderte Dateien mit 155 neuen und 32 gelöschten Zeilen
  1. 131 0
      app/api/controller/Pay.php
  2. 24 32
      library/utils/WxpayV2.php

+ 131 - 0
app/api/controller/Pay.php

@@ -0,0 +1,131 @@
+<?php
+declare (strict_types=1);
+namespace app\api\controller\v1;
+
+use app\Request;
+use app\BaseController;
+use app\model\api\PayTrade;
+use library\services\UtilService;
+use library\utils\weixinPay as wxpayApi;
+use WeChatPay\Transformer;
+use app\lib\OrderLib;
+use think\Exception;
+
+class Pay extends BaseController{
+    /**
+     * 微信支付V2异步反馈
+     * @param Request $request
+     * @return boolean
+     */
+    public function wxpayNotify(Request $request){
+        $xmlData = $request->getInput();
+        if(empty($xmlData)){
+            return false;
+        }
+        $logfile = app()->getRootPath().'public/log/wxnotify.log';
+        file_put_contents($logfile, $xmlData.PHP_EOL,FILE_APPEND | LOCK_EX);
+        
+        
+        
+        
+        
+        
+        
+//
+//        if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
+//            return false;
+//        }
+//        if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
+//            return false;
+//        }
+//        if(empty($post["resource"])){
+//            return false;
+//        }
+//        $wxpay = new wxpayApi();
+//        $result = $wxpay->aesGcmDecrypt([
+//            "associated_data"=>$post["resource"]["associated_data"],
+//            "nonce"=>$post["resource"]["nonce"],
+//            "ciphertext"=>$post["resource"]["ciphertext"],
+//        ]);
+//        
+//        if(!$result){
+//            return false;
+//        }
+//        //商家内部订单号
+//        $out_trade_no   = $result["out_trade_no"];
+//        $res = $wxpay->searchOrder($out_trade_no);
+//        if(!$res){
+//            return false;
+//        }
+//        $data = json_decode($res,true);
+//        //支付成功
+//        if($data["trade_state"]=="SUCCESS"){
+//            $payDataInfo = [
+//                    'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"]))      ?"未返回":$data["amount"]["total"]/100,
+//                    'payMoney'   =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
+//                    'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
+//                    'outTradeNo' =>empty($data["out_trade_no"])   ? "未返回" : $data["out_trade_no"],
+//                    "tradeStatus"=>empty($data["trade_state"])    ? "未返回" : $data["trade_state"],
+//                    "payTime"    =>empty($data["success_time"])   ? "未返回" : $data["success_time"],
+//            ];
+//            $pay_json = json_encode($payDataInfo);
+//            (new OrderLib)->orderPay($out_trade_no, $pay_json);
+//        }
+    }
+     /**
+     * 微信支付V3异步反馈
+     * @param Request $request
+     * @return boolean
+     */
+    public function wxpayNotifyV3(Request $request){
+        $post = UtilService::getMore([
+            ['id', ''],
+            ['create_time', ''],
+            ['event_type',''],
+            ['resource_type', ''],
+            ['resource',[]],
+            ['summary', ''],
+        ], $request);
+        if(empty($post["event_type"]) || $post["event_type"] != "TRANSACTION.SUCCESS"){
+            return false;
+        }
+        if(empty($post["resource_type"]) || $post["resource_type"]!="encrypt-resource"){
+            return false;
+        }
+        if(empty($post["resource"])){
+            return false;
+        }
+        $wxpay = new wxpayApi();
+        $result = $wxpay->aesGcmDecrypt([
+            "associated_data"=>$post["resource"]["associated_data"],
+            "nonce"=>$post["resource"]["nonce"],
+            "ciphertext"=>$post["resource"]["ciphertext"],
+        ]);
+        
+        if(!$result){
+            return false;
+        }
+        //商家内部订单号
+        $out_trade_no   = $result["out_trade_no"];
+        $res = $wxpay->searchOrder($out_trade_no);
+        if(!$res){
+            return false;
+        }
+        $data = json_decode($res,true);
+        //支付成功
+        if($data["trade_state"]=="SUCCESS"){
+            $payDataInfo = [
+                    'totalMoney' =>(empty($data["amount"]) || empty($data["amount"]["total"]))      ?"未返回":$data["amount"]["total"]/100,
+                    'payMoney'   =>(empty($data["amount"]) || empty($data["amount"]["payer_total"]))?"未返回":$data["amount"]["payer_total"]/100,
+                    'payTradeNo' =>empty($data["transaction_id"]) ? "未返回" : $data["transaction_id"],
+                    'outTradeNo' =>empty($data["out_trade_no"])   ? "未返回" : $data["out_trade_no"],
+                    "tradeStatus"=>empty($data["trade_state"])    ? "未返回" : $data["trade_state"],
+                    "payTime"    =>empty($data["success_time"])   ? "未返回" : $data["success_time"],
+            ];
+            $pay_json = json_encode($payDataInfo);
+            (new OrderLib)->orderPay($out_trade_no, $pay_json);
+        }
+    }
+
+}
+

+ 24 - 32
library/utils/WxpayV2.php

@@ -14,6 +14,7 @@ use WeChatPay\Builder;
 use WeChatPay\Formatter;
 use WeChatPay\Crypto\Rsa;
 use WeChatPay\Crypto\AesGcm;
+use WeChatPay\Crypto\AesEcb;
 use WeChatPay\Crypto\Hash;
 use WeChatPay\Util\PemUtil;
 use WeChatPay\Transformer;
@@ -43,9 +44,6 @@ class WxpayV2{
             ],
         ]);
     }
-    public function wxNotify(){
-        var_dump(Transformer::toArray($request->getInput()));
-    }
     public function wxmpPay($post=[]){
         $apiUrl = "v2/pay/unifiedorder";
         //sign_type默认MD5不用传
@@ -242,35 +240,29 @@ class WxpayV2{
             return false;
         }
     }
-    
-    //回调验签
-    public function cesiCheckSign(){
-        $inBody = '';// 请根据实际情况获取,例如: file_get_contents('php://input');
-
-$apiv2Key = '';// 在商户平台上设置的APIv2密钥
-
-$inBodyArray = Transformer::toArray($inBody);
-
-// 部分通知体无`sign_type`,部分`sign_type`默认为`MD5`,部分`sign_type`默认为`HMAC-SHA256`
-// 部分通知无`sign`字典
-// 请根据官方开发文档确定
-['sign_type' => $signType, 'sign' => $sign] = $inBodyArray;
-
-$calculated = Hash::sign(
-    $signType ?? Hash::ALGO_MD5,// 如没获取到`sign_type`,假定默认为`MD5`
-    Formatter::queryStringLike(Formatter::ksort($inBodyArray)),
-    $apiv2Key
-);
-
-$signatureStatus = Hash::equals($calculated, $sign);
-
-if ($signatureStatus) {
-    // 如需要解密的
-    ['req_info' => $reqInfo] = $inBodyArray;
-    $inBodyReqInfoXml = AesEcb::decrypt($reqInfo, Hash::md5($apiv2Key));
-    $inBodyReqInfoArray = Transformer::toArray($inBodyReqInfoXml);
-    // print_r($inBodyReqInfoArray);// 打印解密后的结果
-}
+    /**
+     * 回调验签
+     * @param type $inBody
+     * @return bool
+     */
+    public function notifyCheckSign($inBody){
+        $apiv2Key = '';// 在商户平台上设置的APIv2密钥
+        $inBodyArray = Transformer::toArray($inBody);
+        ['sign_type' => $signType, 'sign' => $sign] = $inBodyArray;
+        $calculated = Hash::sign(
+            $signType ?? Hash::ALGO_MD5,// 如没获取到`sign_type`,假定默认为`MD5`
+            Formatter::queryStringLike(Formatter::ksort($inBodyArray)),
+            $apiv2Key
+        );
+        $signatureStatus = Hash::equals($calculated, $sign);
+        if ($signatureStatus) {
+            // 如需要解密的
+            ['req_info' => $reqInfo] = $inBodyArray;
+            $inBodyReqInfoXml = AesEcb::decrypt($reqInfo, Hash::md5($apiv2Key));
+            $inBodyReqInfoArray = Transformer::toArray($inBodyReqInfoXml);
+            return $inBodyReqInfoArray;
+        }
+        return false;
     }
 }