fly 5 tahun lalu
induk
melakukan
07ab0e4e10

+ 76 - 66
app/Http/Controllers/QuickApp/Order/OrdersController.php

@@ -23,7 +23,9 @@ use App\Modules\Product\Services\ProductService;
 use App\Modules\Book\Services\BookConfigService;
 use App\Modules\Book\Services\BookService;
 use App\Modules\Channel\Services\PayTemplateService;
+use App\Modules\Trade\Pay\OrderArousePayFactory;
 use App\Modules\Trade\Pay\OrderPaySuccess;
+use Exception;
 use Log;
 
 class OrdersController extends BaseController
@@ -332,7 +334,6 @@ class OrdersController extends BaseController
         return response()->pagination(new ChapterOrderTransformer(), $chapter_order);
     }
 
-
     /**
      * @apiVersion 1.0.0
      * @apiDescription 充值记录
@@ -395,6 +396,50 @@ class OrdersController extends BaseController
     }
 
 
+    private function getPayParams(Request $request)
+    {
+        $product_id = $request->get('product_id', 0);
+        $send_order_id = $request->get('send_order_id', 0);
+        if (!$product_id) {
+            return false;
+        }
+        $bid = $request->get('bid', 0);
+        if ($bid) {
+            $from_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 (in_array($uid, explode(',', env('TEST_UID')))) {
+            $price = 1;
+        }
+        $from_type = 'QuickApp';
+        if ($product_info->type == 'YEAR_ORDER') {
+            $order_type = 'YEAR';
+        } elseif ($product_info->type == 'BOOK_ORDER') {
+            $order_type = 'BOOK';
+        } elseif ($product_info->type == 'TICKET_RECHARGE') {
+            $order_type = 'RECHARGE';
+        } else {
+            $order_type = '';
+        }
+        $create_ip = _getIp();
+        return compact(
+            'distribution_channel_id',
+            'uid',
+            'product_id',
+            'price',
+            'trade_no',
+            'create_ip',
+            'send_order_id',
+            'from_bid',
+            'from_type',
+            'order_type'
+        );
+    }
+
     /**
      * @apiVersion 1.0.0
      * @apiDescription 支付
@@ -425,78 +470,42 @@ class OrdersController extends BaseController
      */
     function wxindex(Request $request)
     {
-        $product_id = $request->get('product_id', 0);
-        $send_order_id = $request->get('send_order_id', 0);
-        if (!$product_id) {
+        if ($params = $this->getPayParams($request)) {
+            $params['pay_merchant_source'] = 'QuickAppWxPay';
+        } else {
             return response()->error('QAPP_PARAM_ERROR');
         }
-        $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 (in_array($uid, explode(',', env('TEST_UID')))) {
-            $price = 1;
-        }
-        if ($product_info->type == 'YEAR_ORDER') {
-            $order_type = 'YEAR';
-        } elseif ($product_info->type == 'BOOK_ORDER') {
-            $order_type = 'BOOK';
-        } elseif ($product_info->type == 'TICKET_RECHARGE') {
-            $order_type = 'RECHARGE';
+        $app = OrderArousePayFactory::wxApp($this->uid);
+        $result = $app->handle($params);
+        if ($result) {
+            return response()->success($result);
         } else {
-            $order_type = '';
-        }
-
-        $this->createUnPayOrder([
-            'distribution_channel_id' => $distribution_channel_id,
-            'uid' => $uid,
-            'product_id' => $product_id,
-            'price' => $price / 100,
-            'pay_type' => 1,
-            'trade_no' => $trade_no,
-            'pay_merchant_source' => 'QuickApp',
-            'pay_merchant_id' => 0,
-            'create_ip' => $request->getClientIp(),
-            'send_order_id' => $send_order_id,
-            'order_type' => $order_type,
-            'from_bid' => $bid,
-            'from_type' => 'QuickApp',
-            'activity_id' => 0
-        ]);
-        $config = [
-            // 微信支付参数
-            'appid'     => 'wxf065f7364b078a73', // 应用ID
-            'merchant_id'     => '1500977641', // 微信支付商户号
-            'key'    => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
-            'trade_type' => 'APP',
-        ];
-        $pay = WechatPay::Official('OFFICIALPAY', $config);
-        try {
-            $payOrder = [
-                'trade_no'     => $trade_no, // 订单号
-                'price'        => $price, // 订单金额,**单位:分**
-                'body'             => '快应用 小说', // 订单描述
-                'create_ip' => _getIp(), // 支付人的 IP
-                'remark'            => 'QuickApp'
-            ];
-            $result = $pay->send($payOrder);
-            if ($result) {
-                return response()->success($result);
-            } 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');
         }
     }
 
+    /**
+     * @apiVersion 1.0.0
+     * @apiDescription 支付宝支付
+     * @api {get} ali/goToPay 支付宝支付
+     * @apiGroup pay
+     * @apiName aliIndex
+     * @apiParam {Int}  product_id   product_id
+     * @apiParam {Int}  send_order_id   send_order_id
+     * @apiParam {String}  bid   bid
+     * @apiHeader {String} [Authorization]  token
+     * 
+     */
+    public function aliIndex(Request $request)
+    {
+        if ($params = $this->getPayParams($request)) {
+            $params['pay_merchant_source'] = 'QuickAppALiPay';
+        } else {
+            return response()->error('QAPP_PARAM_ERROR');
+        }
+        $app = OrderArousePayFactory::ali($this->uid);
+        return $app->handle($params);
+    }
 
     /**
      * @apiVersion 1.0.0
@@ -596,6 +605,7 @@ class OrdersController extends BaseController
      */
     private function createUnPayOrder($data)
     {
+        $data['price'] =  $data['price'] / 100;
         $data['status'] = 'UNPAID';
         $data['transaction_id'] = '';
         $data['pay_end_at'] = '0000-00-00 00:00:00';

+ 3 - 1
app/Http/Routes/QuickApp/QuickAppRoutes.php

@@ -4,6 +4,7 @@ Route::group(['domain' => env('QUICKAPP_DOMAIN'), 'namespace' => 'App\Http\Contr
 
     //支付回调
     Route::any('pay/wcback', 'Order\OrdersController@wxback');
+    Route::any('pay/aliback', 'Order\OrdersController@aliback');
 
     // Route::group(['prefix' => 'api', 'middleware' => 'checkSign'], function () {
     Route::group(['prefix' => 'api'], function () {
@@ -68,7 +69,8 @@ Route::group(['domain' => env('QUICKAPP_DOMAIN'), 'namespace' => 'App\Http\Contr
         //删除阅读记录
         Route::get('readrecord/delete', 'User\ReadRecordController@delReadRecord');
         //支付
-        Route::get('goToPay', 'Order\OrdersController@wxindex');
+        Route::get('goToPay', 'Order\OrdersController@wxIndex');
+        Route::get('goToAliPay', 'Order\OrdersController@aliIndex');
 
         Route::get('checkOrder', 'Order\OrdersController@checkOrder');
         //添加快捷方式

File diff ditekan karena terlalu besar
+ 6 - 7
app/Libs/Pay/Merchants/AliPay.php


+ 2 - 2
app/Libs/Pay/Merchants/Official.php

@@ -50,7 +50,7 @@ class Official implements PayMerchantInterface
         $this->app = new Application($options);
     }
 
-    public function send($data)
+    public function send(array $data)
     {
         $app =  $this->app;
         $payment = $app->payment;
@@ -97,7 +97,7 @@ class Official implements PayMerchantInterface
         return [];
     }
 
-    public function makeSign($value)
+    public function makeSign(array $value)
     {
         $data = $value;
         //签名步骤一:按字典序排序参数

+ 1 - 1
app/Libs/Pay/PayFactory.php

@@ -13,7 +13,7 @@ class PayFactory
 	public function __callStatic($name, $arguments)
 	{
 		$namesapce = "\\App\\Libs\\Pay\\Merchants";
-		$application = $namesapce . "\\" . $name;
+		$application = $namesapce . "\\" . ucfirst($name);
 		return new $application(...$arguments);
 	}
 }

+ 30 - 0
app/Modules/Trade/Pay/AliOrderArousePay.php

@@ -0,0 +1,30 @@
+<?php
+
+namespace App\Modules\Trade\Pay;
+
+use App\Libs\Pay\PayFactory;
+use Exception;
+use Log;
+
+class AliOrderArousePay extends OrderArousePayAbstract
+{
+    public function arouse(array $data)
+    {
+        $ali_param = [
+            'body' => '小说阅读',
+            'subject' => '追书云',
+            'out_trade_no' => $data['trade_no'],
+            'timeout_express' => '90m',
+            'total_amount' => $data['price'],
+            'product_code' => 'QUICK_WAP_WAY',
+            'return_url' => '',
+        ];
+        try {
+            $pay = PayFactory::aliPay();
+            $result = $pay->send($ali_param);
+            return $result;
+        } catch (Exception $e) {
+            Log::error("创建支付宝订单失败," . $e->getMessage());
+        }
+    }
+}

+ 16 - 13
app/Modules/Trade/Pay/BookOrderPaySuccess.php

@@ -2,32 +2,35 @@
 
 namespace App\Modules\Trade\Pay;
 
-use App\Modules\Book\Services\BookConfigService;
+use App\Modules\Book\Models\BookConfig;
 use App\Modules\Subscribe\Models\BookOrder;
 use App\Modules\Subscribe\Models\Order;
 
 
 class BookOrderPaySuccess extends PaySuccessAbstract
 {
-    private $book_order;
+    private $book_config;
 
     public function __construct(Order $order)
     {
         parent::__construct($order);
-        $book_conf = BookConfigService::getBookByProduct($order->product_id);
-        $this->book_order = [];
-        $this->book_order['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
-        $this->book_order['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
-        $this->book_order['uid'] = $order->uid;
-        $this->book_order['distribution_channel_id'] = $order->distribution_channel_id;
-        $this->book_order['fee'] = $order->price;
-        $this->book_order['send_order_id'] = $order->send_order_id;
-        $this->book_order['charge_balance'] = 0;
-        $this->book_order['reward_balance'] = 0;
+        $this->book_config = $this->getBookByProduct($order->product_id);
+    }
+
+    private function getBookByProduct($product_id)
+    {
+        return BookConfig::where('product_id', $product_id)->first();
     }
 
     protected function handlePayProcess()
     {
-       return BookOrder::firstOrCreate($this->book_order);
+        $book_order = [];
+        $book_order['bid'] = isset($this->book_config->bid) ? $this->book_config->bid : '';
+        $book_order['book_name'] = isset($this->book_config->book_name) ? $this->book_config->book_name : '';
+        $book_order['uid'] = $this->order->uid;
+        $book_order['distribution_channel_id'] = $this->order->distribution_channel_id;
+        $book_order['fee'] = $this->order->price;
+        $book_order['send_order_id'] = $this->order->send_order_id;
+        return BookOrder::firstOrCreate($book_order);
     }
 }

+ 53 - 0
app/Modules/Trade/Pay/OrderArousePayAbstract.php

@@ -0,0 +1,53 @@
+<?php
+
+namespace App\Modules\Trade\Pay;
+
+use App\Modules\Subscribe\Models\Order;
+
+abstract class OrderArousePayAbstract
+{
+    /**
+     * 唤起支付
+     */
+    abstract public function arouse(array $data);
+
+
+    protected $uid;
+
+    public function __construct(int $uid)
+    {
+        $this->uid = $uid;
+    }
+
+    public function handle(array $data)
+    {
+        $this->createOrder($data);
+        return $this->arouse($data);
+    }
+
+    public function createOrder(array $data)
+    {
+        $params = [
+            'uid' => $this->uid,
+            'product_id' => $data['product_id'],
+            'distribution_channel_id' => $data['distribution_channel_id'],
+            'trade_no' => $data['trade_no'],
+            'pay_merchant_source' => $data['pay_merchant_source'],
+            'order_type' => $data['order_type'],
+            'create_ip' => $data['create_ip'],
+            'send_order_id' => $data['send_order_id'],
+            'from_type' => $data['from_type'],
+            'price' =>  $data['price'] / 100,
+            'status' => 'UNPAID',
+            'transaction_id' => '',
+            'pay_end_at' => '0000-00-00 00:00:00',
+            'pay_merchant_id' => isset($data['pay_merchant_id']) ? $data['pay_merchant_id'] : 0,
+            'send_order_name' => isset($data['send_order_name']) ? $data['send_order_name'] : '',
+            'pay_type' => isset($data['pay_type']) ? $data['pay_type'] : 1,
+            'from_bid' => isset($data['from_bid']) ? $data['from_bid'] : 0,
+            'activity_id' => isset($data['activity_id']) ? $data['activity_id'] : 0,
+            'inner_send_order_id' => isset($data['inner_send_order_id']) ? $data['inner_send_order_id'] : '',
+        ];
+        Order::firstOrCreate($params);
+    }
+}

+ 19 - 0
app/Modules/Trade/Pay/OrderArousePayFactory.php

@@ -0,0 +1,19 @@
+<?php
+
+namespace App\Modules\Trade\Pay;
+
+/**
+ * 
+ * @method static \App\Modules\Trade\Pay\WxAppOrderArousePay wxApp(array $data)
+ * @method static \App\Modules\Trade\Pay\AliPayOrderArousePay ali(array $data)
+ */
+class OrderArousePayFactory
+{
+    public function __callStatic($name, $arguments)
+    {
+        $name =  ucfirst($name) . 'OrderArousePay';
+        $namesapce = "\\App\\Modules\\Trade\\Pay";
+        $application = $namesapce . "\\" . $name;
+        return new $application(...$arguments);
+    }
+}

+ 6 - 8
app/Modules/Trade/Pay/OrderPaySuccess.php

@@ -2,8 +2,7 @@
 
 namespace App\Modules\Trade\Pay;
 
-use App\Modules\Product\Services\ProductService;
-use App\Modules\Subscribe\Services\OrderService;
+use App\Modules\Subscribe\Models\Order;
 
 /**
  * 
@@ -18,20 +17,19 @@ class OrderPaySuccess
      */
     public static function handle(string $trade_no, string $transaction_id)
     {
-        $order = OrderService::getByTradeNo($trade_no);
+        $order = Order::where('trade_no', $trade_no)->first();
         $order->transaction_id = $transaction_id;
         if ($order) {
             if ($order->status == 'PAID') {
                 myLog('pay_notify')->info('has_pay:' . $trade_no);
                 return true;
             }
-            $product = ProductService::getProductSingle($order->product_id);
-            if ($product->type == 'YEAR_ORDER') {
+            if ($order->order_type == 'YEAR') {
                 $app = new YearOrderPaySuccess($order);
-            } elseif ($product->type == 'BOOK_ORDER') {
+            } elseif ($order->order_type == 'BOOK') {
                 $app = new BookOrderPaySuccess($order);
-            } elseif ($product->type == 'TICKET_RECHARGE') {
-                $app = new RechargeOrderPaySuccess($order, $product);
+            } elseif ($order->order_type == 'RECHARGE') {
+                $app = new RechargeOrderPaySuccess($order);
             }
             return $app->success();
         } else {

+ 2 - 2
app/Modules/Trade/Pay/PaySuccessAbstract.php

@@ -13,7 +13,7 @@ use Log;
  */
 abstract class PaySuccessAbstract
 {
-   private $order;
+   protected $order;
 
    /**
     * 处理支付成功的逻辑
@@ -38,7 +38,7 @@ abstract class PaySuccessAbstract
          return true;
       } catch (Exception $e) {
          DB::rollback();
-         Log::error('pay.success: ' . $e->getMessage());
+         Log::error('pay.success: ' . $e->getTraceAsString());
       }
    }
 

+ 13 - 6
app/Modules/Trade/Pay/RechargeOrderPaySuccess.php

@@ -3,9 +3,9 @@
 namespace App\Modules\Trade\Pay;
 
 use App\Modules\Product\Models\Product;
-use App\Modules\Product\Services\ProductService;
 use App\Modules\Subscribe\Models\Order;
-use App\Modules\User\Services\UserService;
+use App\Modules\User\Models\User;
+use DB;
 
 /**
  * 
@@ -16,16 +16,23 @@ class RechargeOrderPaySuccess extends PaySuccessAbstract
 
     private $product;
 
-    public function __construct(Order $order, Product $product)
+    public function __construct(Order $order)
     {
         parent::__construct($order);
-        $this->product = $product;
+        $this->product = Product::find($order->product_id);
     }
 
     protected function handlePayProcess()
     {
-        $total = $this->product->price * 100 + $this->product->given;
+        $charge = $this->product->price * 100;
+        $given = $this->product->given;
         $uid = $this->order->uid;
-        UserService::addBalance($uid, $total, $this->product->price * 100, $this->product->given);
+        User::where('id', $uid)->update(
+            [
+                'balance' => DB::raw('balance+' . ($charge + $given)),
+                'charge_balance' => DB::raw('charge_balance+' . $charge),
+                'reward_balance' => DB::raw('reward_balance+' . $given),
+            ]
+        );
     }
 }

+ 40 - 0
app/Modules/Trade/Pay/WxAppOrderArousePay.php

@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Modules\Trade\Pay;
+
+use App\Libs\Pay\PayFactory;
+use Log;
+
+class WxAppOrderArousePay extends OrderArousePayAbstract
+{
+    public function arouse(array $data)
+    {
+        $config = [
+            // 微信支付参数
+            'appid'     => 'wxf065f7364b078a73', // 应用ID
+            'merchant_id'     => '1500977641', // 微信支付商户号
+            'key'    => '0e7SfPt3EOS0HC1GxVa4fqmCUINcN71E', // 微信支付密钥
+            'trade_type' => 'APP',
+        ];
+        $pay = PayFactory::official($config);
+        try {
+            $payOrder = [
+                'trade_no'     => $data['trade_no'], // 订单号
+                'price'        => $data['price'], // 订单金额,**单位:分**
+                'body'             => '快应用 小说', // 订单描述
+                'create_ip' => _getIp(), // 支付人的 IP
+                'remark'            => 'QuickApp'
+            ];
+            $result = $pay->send($payOrder);
+            if ($result) {
+                return $result;
+            } else {
+                Log::error("创建微信订单失败," . json_encode($result));
+                return false;
+            }
+        } catch (Exception $e) {
+            Log::error("创建微信订单失败," . $e->getMessage());
+            return false;
+        }
+    }
+}