fly 5 vuotta sitten
vanhempi
commit
82b2cecd48

+ 127 - 64
app/Http/Controllers/Wap/Pay/OrdersController.php

@@ -2,10 +2,10 @@
 
 namespace App\Http\Controllers\Wap\Pay;
 
-use App\Client\Pay\BookOrderPaySuccess;
-use App\Client\Pay\ForeverOrderPaySuccess;
-use App\Client\Pay\RechargeOrderPaySuccess;
-use App\Client\Pay\YearOrderPaySuccess;
+use App\Modules\Trade\Pay\BookOrderPaySuccess;
+use App\Modules\Trade\Pay\ForeverOrderPaySuccess;
+use App\Modules\Trade\Pay\RechargeOrderPaySuccess;
+use App\Modules\Trade\Pay\YearOrderPaySuccess;
 use App\Modules\Book\Services\BookGiftsService;
 use App\Modules\Book\Services\ChapterService;
 use App\Modules\OfficialAccount\Services\OfficialAccountService;
@@ -41,6 +41,7 @@ use App\Libs\Pay\Merchants\UnionPay;
 use App\Modules\Activity\Services\ActivityService;
 use App\Modules\SendOrder\Services\SendOrderService;
 use App\Modules\Subscribe\Models\Order;
+use App\Modules\Trade\Models\PayMerchant;
 use GuzzleHttp\Client;
 
 class OrdersController extends Controller
@@ -1114,66 +1115,107 @@ class OrdersController extends Controller
      */
     function wcback_union(Request $request)
     {
-        return UnionPay::notify($request, function ($order) {
-            return $this->orderSuccess($order);
-        });
-    }
-
-    /**
-     * 订单成功
-     * @param Order $order
-     */
-    function orderSuccess(Order $order)
-    {
-
-        if (isset($order->status) && $order->status == 'PAID') {
-            Log::info('has_pay:' . $order->trade_no);
-            return true;
-        }
-        DB::beginTransaction();
-        try {
-
-            $uid = $order->uid;
-            $distribution_channel_id = $order->distribution_channel_id;
-            $product_id = $order->product_id;
-            $product = ProductService::getProductSingle($product_id);
-            switch ($product->type) {
-                case 'YEAR_ORDER':
-                    $instance = new YearOrderPaySuccess($order);
-                    break;
-                case 'BOOK_ORDER':
-                    $instance = new BookOrderPaySuccess($order);
-                    break;
-                case 'TICKET_RECHARGE':
-                    $instance = new RechargeOrderPaySuccess($order);
-                    break;
-                case 'FOREVER':
-                    $instance = new ForeverOrderPaySuccess($order);
-                    break;
+        $data = $request->all();
+        \Log::info('union_pay.notify: ');
+        \Log::info($data);
+        if (isset($data['merOrderId'])) {
+            $trade_no = $data['merOrderId'];
+            if ($trade_no) {
+                $order = OrderService::getByTradeNo($trade_no);
+                $transaction_id = $data['targetOrderId'];
+                if ($order) {
+                    if (isset($order->status) && $order->status == 'PAID') {
+                        Log::info('has_pay:' . $order->trade_no);
+                        return 'SUCCESS';
+                    }
+                    $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')
+                        ->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
+                    $instance =  WechatPay::Union(json_decode($pay_merchant->config, true));
+                    if ($instance->notify($data)) {
+                        // 修改表比较多,开启事务
+                        DB::beginTransaction();
+
+                        $uid = $order->uid;
+                        $distribution_channel_id = $order->distribution_channel_id;
+                        $product_id = $order->product_id;
+                        $product = ProductService::getProductSingle($product_id);
+                        $send_order_id = $order->send_order_id;
+                        $price = $product->price;
+                        Log::info('save_order_end');
+
+                        Log::info($product);
+
+                        Log::info('product_type:' . $product->type);
+                        $prize_fee = (int) (($product->price * 100) * 0.1);
+                        $this->substituteOrderPrice($order->id, $prize_fee);
+                        $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
+                        //获取用户充值次数
+                        $order->pay_type = $this->getChargeTimes($order->uid);
+                        // 更新其他定制Order表
+                        if ($product->type == 'YEAR_ORDER') {
+                            Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
+                            $order_type = 'YEAR';
+                            $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
+                            $order->order_type = $order_type;
+                            $order->status = 'PAID';
+                            $order->pay_end_at = date('Y-m-d H:i:s');
+                            $order->transaction_id = $transaction_id;
+                            $order->save();
+                        } elseif ($product->type == 'BOOK_ORDER') {
+                            Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
+                            $order_type = 'BOOK';
+                            $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
+                            $order->order_type = $order_type;
+                            $order->status = 'PAID';
+                            $order->pay_end_at = date('Y-m-d H:i:s');
+                            $order->transaction_id = $transaction_id;
+                            $order->save();
+                        } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
+                            Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
+                            $order_type = 'RECHARGE';
+                            $this->userCharge($product, $uid);
+                            $order->order_type = $order_type;
+                            $order->status = 'PAID';
+                            $order->pay_end_at = date('Y-m-d H:i:s');
+                            $order->transaction_id = $transaction_id;
+                            $order->save();
+                        } elseif ($product->type == 'FOREVER') {
+                            Log::info('FOREVER------------------------FOREVER');
+                            $order_type = 'FOREVER';
+                            $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
+                            $order->order_type = $order_type;
+                            $order->status = 'PAID';
+                            $order->pay_end_at = date('Y-m-d H:i:s');
+                            $order->transaction_id = $transaction_id;
+                            $order->save();
+                        }
+                        $this->successPayPushMsg($uid, $product, $order->id);
+                        $this->orderStatistical($order);
+                        DB::commit();
+                        //redis 删除未支付的uid
+                        try {
+                            $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
+                            Redis::hdel($key, $uid);
+                        } catch (\Exception $e) {
+                            Log::info('redis remote error-----------------------');
+                            Log::info($e);
+                        }
+
+                        // 注册动作-》回调订单
+                        $action_type = 'CallBackOrder';
+                        $param = [
+                            'order_sn' => isset($trade_no) ? $trade_no : '0',
+                            'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
+                        ];
+                        UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
+
+                        $this->userProperty($uid, $price, $product->type, $order->activity_id);
+                        //$this->huaweiActivity($order->activity_id,$uid,$product_id);
+                        //交换订单时间
+                        $this->exchangeUpdateAndCreated($order->id);
+                    }
+                }
             }
-            $instance->success();
-            $price = $product->price;
-            $prize_fee = (int) (($product->price * 100) * 0.1);
-            $this->substituteOrderPrice($order->id, $prize_fee);
-            $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
-            $this->successPayPushMsg($uid, $product, $order->id);
-            $this->orderStatistical($order);
-            $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
-            Redis::hdel($key, $uid);
-            // 注册动作-》回调订单
-            $action_type = 'CallBackOrder';
-            $param = [
-                'order_sn' => isset($order->trade_no) ? $order->trade_no : '0',
-                'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
-            ];
-            UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
-            //用户属性
-            $this->userProperty($uid, $price, $product->type, $order->activity_id);
-            //交换订单时间
-            $this->exchangeUpdateAndCreated($order->id);
-            return true;
-        } catch (\Exception $e) {
-            return false;
         }
     }
 
@@ -2009,7 +2051,7 @@ class OrdersController extends Controller
      * @param $data
      *
      */
-    protected function createOrderTotal($data)
+    protected function createOrderTaotal($data)
     {
         $data['status'] = 'UNPAID';
         $data['created_at'] = date('Y-m-d H:i:s');
@@ -2472,4 +2514,25 @@ class OrdersController extends Controller
             } catch (\Exception $e) { }
         }
     }
+
+    public function query()
+    {
+        $pay_merchant = PayMerchant::find(48);
+        $instance = WechatPay::Union($pay_merchant->source, json_decode($pay_merchant->config_info, true));
+        if ($instance->query('319420190918172727164500482012')) {
+            \Log::info('success');
+        }
+    }
+    public function refund()
+    {
+        $pay_merchant = PayMerchant::find(48);
+        $instance = WechatPay::Union($pay_merchant->source, json_decode($pay_merchant->config_info, true));
+        $data = [
+            'trade_no' => '319420190918172727164500482012',
+            'price' => 1
+        ];
+        if ($instance->refund($data)) {
+            \Log::info('success');
+        }
+    }
 }

+ 3 - 1
app/Http/Routes/Wap/WapRoutes.php

@@ -10,7 +10,7 @@
 Route::group(['domain'=>env('PAY_WAP_DOMAIN'),'namespace'=>'App\Http\Controllers\Wap'],function(){
     //支付
     Route::get('/goToPay','Pay\OrdersController@wxindex');
-    //支付会掉
+    //支付回调
     Route::any('pay/wcback_allinpay','Pay\OrdersController@wcback_allinpay');
     Route::any('pay/wcback_official','Pay\OrdersController@wcback_official');
     Route::any('pay/wcback_lianlianpay','Pay\OrdersController@wcback_lianlianpay');
@@ -19,6 +19,8 @@ Route::group(['domain'=>env('PAY_WAP_DOMAIN'),'namespace'=>'App\Http\Controllers
     Route::any('pay/wcback_smkpay','Pay\OrdersController@wcback_smkpay');
     Route::any('pay/wcback_union','Pay\OrdersController@wcback_union');
     Route::post('pay/reportError','Pay\OrdersController@reportError');
+    Route::any('pay/query','Pay\OrdersController@query');
+    Route::any('pay/refund','Pay\OrdersController@refund');
     //包月
     Route::get('monthpay/getRedirectUrl','Pay\getH5RedirectUrl@getRedirectUrl');
     Route::get('monthpay/getH5RedirectUrl','Pay\MonthOrderController@getH5RedirectUrl');

+ 9 - 3
app/Libs/Pay/Merchants/PayMerchantInterface.php

@@ -13,16 +13,22 @@ interface PayMerchantInterface
     public function send(array $data);
     /**
      * 支付回调
-     * @param lluminate\Http\Request $request
-     * @param string $callback 回调函数
+     * @param array $data 
      */
-    public static function notify(Request $request, callable $callback);
+    public function notify(array $data);
     /**
      * 订单查询
      * @param string $trade_no 商户订单号
      * @return bool 
      */
     public function query(string $trade_no);
+
+    /**
+     * 退款申请
+     * @param array $data 
+     */
+    public function refund(array $data);
+
     /**
      * 生成签名
      * @param array $data 

+ 53 - 47
app/Libs/Pay/Merchants/UnionPay.php

@@ -2,11 +2,8 @@
 
 namespace App\Libs\Pay\Merchants;
 
-use App\Modules\Subscribe\Services\OrderService;
 use Exception;
 use GuzzleHttp\Client;
-use Illuminate\Http\Request;
-use DB;
 
 /**
  * 银联支付
@@ -35,16 +32,6 @@ class UnionPay implements PayMerchantInterface
         $this->key = $config['key'];
     }
 
-    private function setConfig(array $config)
-    {
-        $this->mid = $config['mid'];
-        $this->tid = $config['tid'];
-        $this->instMid = $config['instMid'];
-        $this->msgSrc = $config['msgSrc'];
-        $this->msgSrcId = $config['msgSrcId'];
-        $this->key = $config['key'];
-    }
-
     public function send(array $data)
     {
         $result = [
@@ -69,55 +56,59 @@ class UnionPay implements PayMerchantInterface
     public function query(string $trade_no)
     {
         $query_params = [
-            'msgType' => 'query',
-            'requestTimestamp' => date('Y-m-d H:i:s'),
             'mid' => $this->mid,
             'tid' => $this->tid,
             'instMid' => $this->instMid,
             'msgSrc' => $this->msgSrc,
             'msgSrcId' => $this->msgSrcId,
             'signType' => $this->signType,
+            'msgType' => 'query',
+            'requestTimestamp' => date('Y-m-d H:i:s'),
             'merOrderId' => $trade_no,
         ];
-        $query_params['sign'] = $this->makeSign($query_params);
-        try {
-            $client = new Client(['base_uri' => $this->query_order_url, 'timeout' => 3]);
-            $result = $client->request('POST', '/mct1/payorder', ['form_params' => $query_params])->getBody()->getContents();
-            if ($result) {
-                $result = json_decode($result);
-                if ($result->errCode === 'SUCCESS' && $result->status === 'TRADE_SUCCESS') {
-                    return true;
-                }
-            }
-        } catch (Exception $e) { }
-
+        $result = $this->request($query_params);
+        if ($result->errCode === 'SUCCESS' && $result->status === 'TRADE_SUCCESS') {
+            return true;
+        } else {
+            \Log::info('union_pay.query: ');
+            \Log::info(object_to_array($result));
+        }
         return false;
     }
 
-    public static function notify(Request $request, callable $callback)
+    public function notify(array $data)
     {
-        $data = $request->all();
-        \Log::info('union_pay.notify: ');
-        \Log::info($data);
         if ($data['status'] === 'TRADE_SUCCESS') {
-            $trade_no = $data['merOrderId'];
-            if ($trade_no) {
-                $order = OrderService::getByTradeNo($trade_no);
-                $order->transaction_id = $data['targetOrderId'];
-                if ($order) {
-                    $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')
-                        ->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
-                    $instance = new UnionPay(json_decode($pay_merchant->config_info, true));
-                    $sign = $instance->makeSign($data);
-                    if ($sign === $data['sign']) {
-                        if (call_user_func_array($callback, [$order])) {
-                            return 'SUCCESS';
-                        }
-                    }
-                }
+            $sign = $this->makeSign($data);
+            if ($sign === $data['sign']) {
+                return true;
             }
         }
-        return 'FAILED';
+        return false;
+    }
+
+    public function refund(array $data)
+    {
+        $refund_params = [
+            'mid' => $this->mid,
+            'tid' => $this->tid,
+            'instMid' => $this->instMid,
+            'msgSrc' => $this->msgSrc,
+            'msgSrcId' => $this->msgSrcId,
+            'signType' => $this->signType,
+            'msgType' => 'refund',
+            'requestTimestamp' => date('Y-m-d H:i:s'),
+            'merOrderId' => $data['trade_no'],
+            'refundAmount' => $data['price'],
+        ];
+        $result = $this->request($refund_params);
+        if ($result->errCode === 'SUCCESS' && $result->refundStatus === 'SUCCESS' && $result->status === 'TRADE_SUCCESS') {
+            return true;
+        } else {
+            \Log::info('union_pay.refund: ');
+            \Log::info(object_to_array($result));
+        }
+        return false;
     }
 
     /**
@@ -147,4 +138,19 @@ class UnionPay implements PayMerchantInterface
         $result = strtoupper($string);
         return $result;
     }
+
+    private function request(array $data)
+    {
+        try {
+            $data['sign'] = $this->makeSign($data);
+            $client = new Client(['timeout' => 3]);
+            $result = $client->request('post',  $this->query_order_url, ['json' => $data])->getBody()->getContents();
+            if ($result) {
+                $result = json_decode($result);
+            }
+            return $result;
+        } catch (Exception $e) {
+            \Log::error('union_pay.request: ' . $e->getMessage());
+        }
+    }
 }

+ 1 - 1
app/Client/Pay/BookOrderPaySuccess.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace App\Client\Pay;
+namespace App\Modules\Trade\Pay;
 
 use App\Modules\Book\Services\BookConfigService;
 use App\Modules\Subscribe\Models\BookOrder;

+ 1 - 1
app/Client/Pay/ForeverOrderPaySuccess.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace App\Client\Pay;
+namespace App\Modules\Trade\Pay;
 
 use App\Modules\Subscribe\Models\Order;
 use App\Modules\Subscribe\Models\YearOrder;

+ 4 - 4
app/Client/Pay/PaySuccessAbstract.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace App\Client\Pay;
+namespace App\Modules\Trade\Pay;
 
 use App\Modules\Subscribe\Models\Order;
 use DB;
@@ -46,7 +46,7 @@ abstract class PaySuccessAbstract
     */
    protected function setOrderSuccess()
    {
-      $this->order->pay_type = $this->getChargeTimes($this->order->uid);
+      $this->order->pay_type = $this->getChargeTimes();
       $this->order->status = 'PAID';
       $this->order->pay_end_at = date('Y-m-d H:i:s');
       $this->order->save();
@@ -57,9 +57,9 @@ abstract class PaySuccessAbstract
     * @param $uid
     * @return int
     */
-   private function getChargeTimes($uid)
+   private function getChargeTimes()
    {
-      $count = Order::getUserChargeTimes($uid);
+      $count = Order::where('uid', $this->order->uid)->where('status', 'PAID')->count('id');
       return $count + 1;
    }
 }

+ 1 - 1
app/Client/Pay/RechargeOrderPaySuccess.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace App\Client\Pay;
+namespace App\Modules\Trade\Pay;
 
 use App\Modules\Product\Services\ProductService;
 use App\Modules\Subscribe\Models\Order;

+ 1 - 1
app/Client/Pay/YearOrderPaySuccess.php

@@ -1,6 +1,6 @@
 <?php
 
-namespace App\Client\Pay;
+namespace App\Modules\Trade\Pay;
 
 use App\Modules\Subscribe\Models\Order;
 use App\Modules\Subscribe\Models\YearOrder;