fly 5 years ago
parent
commit
4f379a712e

+ 39 - 91
app/Http/Controllers/QuickApp/Order/OrdersController.php

@@ -10,6 +10,7 @@ use App\Modules\Xcx\Services\XcxOrderService as OrderService;
 use App\Http\Controllers\QuickApp\Order\Transformers\BookOrderTransformer;
 use App\Http\Controllers\QuickApp\Order\Transformers\ChapterOrderTransformer;
 use App\Http\Controllers\QuickApp\Order\Transformers\ChargeListTransformer;
+use App\Libs\Pay\WechatPay;
 use App\Modules\Subscribe\Services\YearOrderService;
 use App\Modules\User\Services\UserService;
 use DB;
@@ -19,6 +20,7 @@ use EasyWeChat\Foundation\Application;
 use EasyWeChat\Payment\Order as Wxorder;
 use App\Modules\Product\Services\ProductService;
 use App\Modules\Book\Services\BookConfigService;
+use App\Modules\Book\Services\BookService;
 use Log;
 
 class OrdersController extends BaseController
@@ -357,36 +359,23 @@ class OrdersController extends BaseController
      */
     function wxindex(Request $request)
     {
-        $product_id = $request->has('product_id') ? $request->get('product_id') : '';
-        $sign = $request->has('sign') ? $request->get('sign') : '';
-        $send_order_id = $request->has('send_order_id') ? $request->get('send_order_id') : 0;
-
+        $product_id = $request->get('product_id', 0);
+        $send_order_id = $request->get('send_order_id', 0);
         if (empty($product_id) || empty($sign)) {
             return response()->error('QAPP_PARAM_ERROR');
         }
-        $bid = $request->has('bid') ? $request->get('bid') : 0;
-        $openid = $this->openid;
-        //$openid = 'o0bsX0b3-kgWIb4JBdTAk7HVtnrk';
-        //根据分校id获取支付配置id
-
+        $bid = $request->get('bid', 0);
+        if ($bid) {
+            $bid = BookService::decodeBidStatic($bid);
+        }
         $trade_no = date("YmdHis") . hexdec(uniqid());
         $product_info = ProductService::getProductSingle($product_id);
         $uid = $this->uid;
         $distribution_channel_id = $this->distribution_channel_id;
         $price = $product_info->price * 100;
-        if ($uid < 32) { }
-
         if (in_array($uid, explode(',', env('TEST_UID')))) {
             $price = 1;
         }
-        if ($bid) {
-            try {
-                $bid = Hashids::decode($bid)[0];
-            } catch (\Exception $e) {
-                $bid = 0;
-            }
-        }
-        //$price = 1;
         if ($product_info->type == 'YEAR_ORDER') {
             $order_type = 'YEAR';
         } elseif ($product_info->type == 'BOOK_ORDER') {
@@ -404,7 +393,7 @@ class OrdersController extends BaseController
             'price' => $price / 100,
             'pay_type' => 1,
             'trade_no' => $trade_no,
-            'pay_merchant_source' => 'XIAOCHENGXU',
+            'pay_merchant_source' => 'QuickApp',
             'pay_merchant_id' => 0,
             'create_ip' => $request->getClientIp(),
             'send_order_id' => $send_order_id,
@@ -413,55 +402,39 @@ class OrdersController extends BaseController
             'from_type' => 'QuickApp',
             'activity_id' => 0
         ]);
-        Log::info('order is');
-        Log::info($res);
-        //微信下单
-        $options = [
-            'app_id' => 'wxa0c8331eba3b34d5',
-            'payment' => [
-                'merchant_id' => 1501944981,
-                'key' => 'ee245088b93ba88008279d95f6d30413',
-            ]
+        $config = [
+            // 微信支付参数
+            'app_id'     => 'wxf065f7364b078a73', // 应用ID
+            'mch_id'     => '1500977641', // 微信支付商户号
+            'mch_key'    => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
         ];
-        $app = new Application($options);
-        $payment = $app->payment;
-        $attributes = [
-            'trade_type' => 'JSAPI',
-            'body' => 'novel read',
-            'detail' => 'novel read',
-            'out_trade_no' => $trade_no,
-            'total_fee' => $price,
-            'notify_url' => env('QAPP_PAY_CALL_BACK_URL'),
-            'openid' => $openid,
-            'spbill_create_ip' => $request->getClientIp(),
-        ];
-
-        $order = new Wxorder($attributes);
-        Log::info('wexin order--------');
-        Log::info($order);
-        $result = $payment->prepare($order);
-        Log::info('wexin order--xcx--result-----');
-        Log::info($result);
-        if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
-            Log::info('wechat success in');
-            $data = [
-                'appId' => $result->appid,
-                'package' => 'prepay_id=' . $result->prepay_id,
-                'nonceStr' => $result->nonce_str,
-                'timeStamp' => (string) time(),
-                'signType' => 'MD5',
-            ];
-            $data['paySign'] = $this->MakeSign($data);
-            unset($data['appId']);
-            $data['order'] = $trade_no;
-            $data = [
-                'code' => 0,
-                'msg' => '',
-                'data' => $data
+        $pay = WechatPay::Official('OFFICIALPAY', $config);
+        try {
+            $payOrder = [
+                'out_trade_no'     => $trade_no, // 订单号
+                'total_fee'        => $price, // 订单金额,**单位:分**
+                'body'             => '阅读云阅 小说', // 订单描述
+                'spbill_create_ip' => _getIp(), // 支付人的 IP
+                'notify_url'       => env('WECHART_APP_PAY_CALL_BACK_URL'), // 定义通知URL
+                'attach'            => 'QuickApp'
             ];
-            return response()->json(($data));
+            $result = $pay->send($payOrder);
+            if ($result && isset($result['prepayid'])) {
+                $data['appid'] = $result['appid'];
+                $data['partnerid'] = $result['partnerid'];
+                $data['prepayid'] = $result['prepayid'];
+                $data['timestamp'] = $result['timestamp'];
+                $data['noncestr'] = $result['noncestr'];
+                $data['packagInfo'] = $result['package'];
+                $data['sign'] = $result['sign'];
+            } else {
+                Log::error("创建微信订单失败," . json_encode($result));
+                return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
+            }
+        } catch (Exception $e) {
+            Log::error("创建微信订单失败," . $e->getMessage());
+            return response()->error('APP_CREATE_WECHAT_ORDER_FAIL');
         }
-        return null;
     }
 
 
@@ -510,26 +483,6 @@ class OrdersController extends BaseController
         }
         return response()->error('QAPP_SYS_ERROR');
     }
-    protected function MakeSign($value)
-    {
-        $data = $value;
-        //签名步骤一:按字典序排序参数
-        ksort($data);
-        $buff = "";
-        foreach ($data as $k => $v) {
-            if ($k != "sign" && $v != "" && !is_array($v)) {
-                $buff .= $k . "=" . $v . "&";
-            }
-        }
-        $buff = trim($buff, "&");
-        //签名步骤二:在string后加入KEY
-        $string = $buff . "&key=" . 'ee245088b93ba88008279d95f6d30413';
-        //签名步骤三:MD5加密
-        $string = md5($string);
-        //签名步骤四:所有字符转为大写
-        $result = strtoupper($string);
-        return $result;
-    }
 
     /**
      * 官方微信回调
@@ -568,11 +521,8 @@ class OrdersController extends BaseController
                 $product = ProductService::getProductSingle($product_id);
                 $send_order_id = 0;
                 $price = $product->price;
-
-                //$this->updateOrderTotal($trade_no,$transaction_id);
                 // 更新其他定制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;
@@ -581,7 +531,6 @@ class OrdersController extends BaseController
                     $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;
@@ -590,7 +539,6 @@ class OrdersController extends BaseController
                     $order->transaction_id = $transaction_id;
                     $order->save();
                 } elseif ($product->type == 'TICKET_RECHARGE') {
-                    Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
                     $order_type = 'RECHARGE';
                     $this->userCharge($product, $uid);
                     $order->order_type = $order_type;

+ 0 - 1
app/Libs/Pay/Merchants/Official.php

@@ -8,7 +8,6 @@
 
 namespace App\Libs\Pay\Merchants;
 
-use GuzzleHttp\Client;
 use EasyWeChat\Foundation\Application;
 use EasyWeChat\Payment\Order as Wxorder;
 use Log;

+ 7 - 0
app/Modules/Book/Services/BookService.php

@@ -2,6 +2,7 @@
 
 namespace App\Modules\Book\Services;
 
+use App\Modules\BaseService;
 use App\Modules\Book\Models\Book;
 use App\Modules\Book\Models\Chapter;
 use Hashids;
@@ -9,8 +10,14 @@ use Redis;
 use App\Modules\Statistic\Services\WapVisitStatService;
 use DB;
 
+/**
+ * 
+ * @method static int decodeBidStatic(string $bid) 解析bid
+ */
 class BookService
 {
+    use BaseService;
+
     public function decodeBid(string $bid)
     {
         return Hashids::decode($bid)[0];

+ 1 - 1
app/Modules/User/Services/QappUserService.php

@@ -46,8 +46,8 @@ class QappUserService
 
     public function setGolableUser(int $uid)
     {
-        $qapp_user = app()->make('qapp_user');
         $user_info = $this->getQAppUserByUid($uid);
+        $qapp_user = app()->make('qapp_user');
         $qapp_user->id = $user_info->id;
         $qapp_user->uid = $user_info->uid;
         $qapp_user->device_no = $user_info->device_no;