OrdersController.php 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  1. <?php
  2. namespace App\Http\Controllers\Wap\Pay;
  3. use App\Modules\Book\Services\ChapterService;
  4. use App\Modules\Subscribe\Services\SubstituteOrderService;
  5. use App\Modules\User\Services\ReadRecordService;
  6. use Illuminate\Routing\Controller;
  7. use App\Libs\Pay\WechatPay;
  8. use Illuminate\Http\Request;
  9. use App\Modules\Subscribe\Services\YearOrderService;
  10. use App\Modules\Subscribe\Services\BookOrderService;
  11. use App\Modules\Subscribe\Services\OrderService;
  12. use App\Modules\Product\Services\ProductService;
  13. use App\Modules\Book\Services\BookConfigService;
  14. use App\Modules\User\Services\UserService;
  15. use App\Modules\Channel\Services\ChannelService;
  16. use Log;
  17. use DB;
  18. use Cookie;
  19. use Redis;
  20. use Hashids;
  21. use EasyWeChat\Foundation\Application;
  22. use EasyWeChat\Support\XML;
  23. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  24. use App\Jobs\SendTexts;
  25. use App\Jobs\SendStatisticsList;
  26. use App\Libs\AliSMS;
  27. use App\Modules\Activity\Services\ActivityService;
  28. use App\Modules\SendOrder\Services\SendOrderService;
  29. use GuzzleHttp\Client;
  30. class OrdersController extends Controller
  31. {
  32. /**
  33. * @apiDefine pay 支付
  34. */
  35. /**
  36. * 支付唤起入口
  37. * 功能:授权、请求支付参数、并跳转微信支付
  38. * TODO 暂时不创建订单
  39. * tail -f /var/www/ydy_wap_backend/storage/logs/laravel-2017-12-01.log
  40. * zwap/goToPay?distribution_channel_id=3&price=1&uid=3
  41. * @apiVersion 1.0.0
  42. * @apiDescription 支付
  43. * @api {get} http://pay.aizhuishu.com/goToPay 支付
  44. * @apiGroup pay
  45. * @apiName wxindex
  46. * @apiParam {Int} product_id product_id
  47. * @apiParam {Int} uid uid
  48. * @apiParam {Int} distribution_channel_id distribution_channel_id
  49. * @apiParam {String} pay_redirect_url pay_redirect_url
  50. * @apiSuccess {int} code 状态码
  51. * @apiSuccessExample {json} Success-Response:
  52. * HTTP/1.1 200 OK
  53. * {
  54. * code: 0,
  55. * msg: "",
  56. * data: {
  57. * }
  58. */
  59. function wxindex(Request $request)
  60. {
  61. $product_id = $request->has('product_id') ? $request->get('product_id') : '';
  62. $uid = $request->has('uid') ? $request->get('uid') : '';
  63. $distribution_channel_id = $request->has('distribution_channel_id') ? $request->get('distribution_channel_id') : '';
  64. $pay_redirect_url = $request->has('pay_redirect_url') ? $request->get('pay_redirect_url') : '';
  65. $send_order_id = $request->has('send_order_id') ? $request->get('send_order_id') : 0;
  66. $activity_id = $request->has('activity_id') ? $request->get('activity_id') : 0;
  67. $n = $request->has('n') ? $request->get('n') : 0;
  68. if ($send_order_id && strlen($send_order_id) > 50) {
  69. try {
  70. $send_order_id = decrypt($send_order_id);
  71. } catch (\Exception $e) {
  72. $send_order_id = 0;
  73. }
  74. }
  75. if ($send_order_id && strlen($send_order_id) > 50) {
  76. $send_order_id = 0;
  77. }
  78. //活动判断
  79. if ($activity_id) {
  80. $limit = $request->get('limit', 0);
  81. if ($limit && $limit == 1) {
  82. $order = OrderService::userIsParticipateActivity($uid, $activity_id);
  83. if ($order) {
  84. $activity = ActivityService::getById($activity_id);
  85. if ($activity) {
  86. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com' . $activity->activity_page;
  87. return redirect()->to($url);
  88. }
  89. return '';
  90. }
  91. }
  92. }
  93. $suid= $request->get('suid',0);
  94. if ($suid) {
  95. $user_info = UserService::getById($uid);
  96. $distribution_channel_id = $user_info->distribution_channel_id;
  97. $pay_redirect_url = 1;
  98. }
  99. if (empty($product_id) || empty($uid) || empty($distribution_channel_id) || empty($pay_redirect_url)) {
  100. return response()->error('WAP_PARAM_ERROR');
  101. }
  102. if($suid && $suid == $uid){
  103. return back();
  104. }
  105. $openid = $request->has('openid') ? $request->get('openid') : '';
  106. $bid = $request->has('bid') ? $request->get('bid') : 0;
  107. $hash_bid = $bid;
  108. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from');
  109. if ($fromtype && strlen($fromtype) > 50) {
  110. try {
  111. $fromtype = decrypt($fromtype);
  112. } catch (\Exception $e) {
  113. $fromtype = 'main';
  114. }
  115. }
  116. if ($fromtype && strlen($fromtype) > 50) {
  117. $fromtype = 'main';
  118. }
  119. //根据分校id获取支付配置id
  120. Log::info($request->all());
  121. $channel = ChannelService::getById($distribution_channel_id);
  122. if (!$channel || !$channel->pay_merchant_id) {
  123. return response()->error('WAP_PARAM_ERROR');
  124. }
  125. //获取支付类型
  126. $pay_merchant = DB::table('pay_merchants')->select('id','appid', 'source', 'config_info')->where('id', $channel->pay_merchant_id)->where('is_enabled', 1)->first();
  127. if (!$pay_merchant || !$pay_merchant->appid || !$pay_merchant->source) return response()->error('WAP_PARAM_ERROR');
  128. if($pay_merchant->source == 'PALMPAYV2'){
  129. return redirect()->to($this->paympay($request,$pay_merchant));
  130. }
  131. //重定向 获取用户信息的次数
  132. $n++;
  133. if ($n >= 5) {
  134. //重定向次数过多,则授权公众号出问题
  135. $this->payAlert($channel->pay_merchant_id, '', '', $n);
  136. $back_url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . '/pay';
  137. return redirect()->to($back_url);
  138. }
  139. if (empty($openid)) {
  140. $trade_no = date("YmdHis") . hexdec(uniqid());
  141. $params = compact('uid', 'product_id', 'distribution_channel_id', 'send_order_id', 'bid', 'trade_no', 'pay_redirect_url', 'fromtype', 'activity_id', 'n','suid');
  142. $redirect_url = env('CREATE_PAY_URL') . '?' . http_build_query($params);
  143. //$redirect_url = env('CREATE_PAY_URL').'?uid='.$uid.'&product_id='.$product_id.'&distribution_channel_id='
  144. // .$distribution_channel_id.'&send_order_id='.$send_order_id.'&bid='.$bid.'&pay_redirect_url='.urlencode($pay_redirect_url)
  145. $auth_url = env('AUTH_URL') . '?gzh_app_id=' . $pay_merchant->appid . '&redirect_url=' . urlencode($redirect_url);
  146. Log::info('redirect_auth_url:' . $auth_url);
  147. header("Location:" . $auth_url);
  148. exit();
  149. }
  150. $trade_no = $request->input('trade_no');
  151. $order_info = OrderService::getByTradeNo($trade_no);
  152. if ($order_info) return response()->error('WAP_SYS_ERROR');
  153. $cid = $request->has('cid') ? $request->get('cid') : '';
  154. $product_info = ProductService::getProductSingle($product_id,false);
  155. //新用户只能冲一次
  156. //Log::info('新用户只能冲一次:' .$product_info->type);
  157. if($this->isNewUserSecondCharge($product_info->type,$product_id,$uid)){
  158. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  159. return redirect()->to($url);
  160. }
  161. $price = $product_info->price * 100;
  162. if ($uid < 32) {
  163. $price = 1;
  164. }
  165. if (!$send_order_id) {
  166. try {
  167. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  168. } catch (\Exception $e) {
  169. }
  170. }
  171. $this->updateUserSendOrderId($uid,$send_order_id);
  172. if (in_array($uid, explode(',', env('TEST_UID')))) {
  173. $price = 1;
  174. }
  175. if ($bid) {
  176. try {
  177. $bid = Hashids::decode($bid)[0];
  178. } catch (\Exception $e) {
  179. $bid = 0;
  180. }
  181. }
  182. try {
  183. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  184. Redis::hset($key, $uid, time());
  185. $date = date('Y-m-d');
  186. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  187. if ($send_order_id) {
  188. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  189. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  190. }
  191. } catch (\Exception $e) {
  192. }
  193. $data = [];
  194. $data['price'] = $price;
  195. $data['create_ip'] = $request->getClientIp();
  196. $data['openid'] = $openid;//
  197. $data['body'] = 'novel read';
  198. $official_name = $this->getSubscribeOfficialName($uid);
  199. if ($official_name) {
  200. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  201. }
  202. $data['detail'] = 'novel read';
  203. $data['trade_no'] = $trade_no;
  204. $data['remark'] = json_encode(
  205. [
  206. 'uid' => $uid,
  207. 'dcd' => $distribution_channel_id,
  208. 'bid' => $bid,
  209. 'cp' => $request->getClientIp(),
  210. 'pms' => $pay_merchant->source,
  211. 'pmi' => $channel->pay_merchant_id,
  212. 'pd' => $product_id,
  213. 'soi' => $send_order_id,
  214. ]
  215. );
  216. if ($product_info->type == 'YEAR_ORDER') {
  217. $order_type = 'YEAR';
  218. } elseif ($product_info->type == 'BOOK_ORDER') {
  219. $order_type = 'BOOK';
  220. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  221. $order_type = 'RECHARGE';
  222. } elseif($product_info->type == 'NEW_USER'){
  223. $order_type = 'RECHARGE';
  224. }else{
  225. $order_type = 'UNKNOWN';
  226. }
  227. /*
  228. $this->createOrderTotal([
  229. 'distribution_channel_id'=>$distribution_channel_id,
  230. 'uid'=>$uid,
  231. 'product_id'=>$product_id,
  232. 'price'=>$price/100,
  233. 'pay_type'=>1,
  234. 'trade_no'=>$data['trade_no'],
  235. 'pay_merchant_source'=>$pay_merchant->source,
  236. 'pay_merchant_id'=>$channel->pay_merchant_id,
  237. 'create_ip'=>$request->getClientIp(),
  238. 'send_order_id'=>$send_order_id,
  239. 'order_type'=>$order_type,
  240. 'from_bid'=>$bid
  241. ]);*/
  242. $send_order_name = '';
  243. if ($send_order_id) {
  244. $send_order_info = SendOrderService::getById($send_order_id);
  245. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  246. $send_order_name = $send_order_info->name;
  247. }
  248. }
  249. if($suid){
  250. $user_info = UserService::getById($uid);
  251. $distribution_channel_id = $user_info->distribution_channel_id;
  252. }
  253. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  254. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  255. $init_order = [
  256. 'distribution_channel_id' => $distribution_channel_id,
  257. 'uid' => $uid,
  258. 'product_id' => $product_id,
  259. 'price' => $price / 100,
  260. 'pay_type' => 1,
  261. 'trade_no' => $data['trade_no'],
  262. 'pay_merchant_source' => $pay_merchant->source,
  263. 'pay_merchant_id' => $channel->pay_merchant_id,
  264. 'create_ip' => $request->getClientIp(),
  265. 'send_order_id' => $send_order_id,
  266. 'send_order_name' => $send_order_name,
  267. 'order_type' => $order_type,
  268. 'from_bid' => $from_bid,
  269. 'from_type' => $fromtype,
  270. 'activity_id' => $activity_id,
  271. 'inner_send_order_id'=>$inner_send_order_id
  272. ];
  273. $result = $this->createUnPayOrder($init_order);
  274. //订单创建成功后增加统计
  275. if($result)
  276. {
  277. $this->orderCreated($init_order);
  278. }
  279. if ($request->get('suid')) {
  280. $this->createSubstituteOrder($result->id, $uid, $request->get('suid'));
  281. }
  282. Log::info($data);
  283. $config = [];
  284. if ($pay_merchant->config_info) {
  285. $config = json_decode($pay_merchant->config_info, true);
  286. }
  287. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  288. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  289. $pay_info = $wechatPay->send($data);
  290. if (!$pay_info) {
  291. $pay_info = $wechatPay->send($data);
  292. }
  293. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  294. //支付异常
  295. $this->payAlert($channel->pay_merchant_id, $trade_no, $pay_info);
  296. }
  297. $pay_info['pay_redirect_url'] = urldecode($pay_redirect_url);
  298. if ($request->has('cid')) {
  299. $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&cid=' . $request->input('cid');
  300. }
  301. // if ($hash_bid) {
  302. // $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&bid=' . $hash_bid;
  303. // }
  304. if ($suid) {
  305. $prize_fee = (int)(($product_info->price*100)*0.1);
  306. $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  307. $help_pay_page_channel_id = $distribution_channel_id;
  308. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  309. $pay_info['pay_redirect_url'] = $pay_redirect_url = sprintf(
  310. $url_format,
  311. env('PROTOCOL'),
  312. encodeDistributionChannelId($help_pay_page_channel_id),
  313. env('CUSTOM_HOST'),
  314. $prize_fee,
  315. $uid
  316. );
  317. }
  318. $pay_url_info = parse_url($pay_redirect_url);
  319. $pay_info['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/wait?order=' . $data['trade_no'] . '&redirect=' . urlencode($pay_info['pay_redirect_url']);
  320. //$h5_scheme = env('H5_SCHEME','https');
  321. //$jsSdkSign = $this->jsSdkSign($pay_info,str_replace('http',$h5_scheme,url()->current()));
  322. Log::info('$pay_info is');
  323. $jsSdkSign = 0;
  324. Log::info($pay_info);
  325. Log::info('jsSdkSign---- :' . $jsSdkSign);
  326. $pay_order = $trade_no;
  327. return view('pay.order.index', compact('pay_info', 'referer', 'jsSdkSign', 'pay_order'));
  328. }
  329. private function paympay(Request $request,$pay_merchant){
  330. $product_id = $request->get('product_id');
  331. $uid = $request->get('uid');
  332. $distribution_channel_id = $request->get('distribution_channel_id');
  333. $pay_redirect_url = $request->get('pay_redirect_url');
  334. $send_order_id = $request->get('send_order_id',0) ;
  335. $activity_id = $request->get('activity_id',0) ;
  336. $suid= $request->get('suid',0);
  337. $bid = $request->has('bid') ? $request->get('bid') : 0;
  338. $hash_bid = $bid;
  339. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from','main');
  340. //根据分校id获取支付配置id
  341. Log::info($request->all());
  342. $trade_no =$trade_no = date("YmdHis") . hexdec(uniqid());;
  343. $order_info = OrderService::getByTradeNo($trade_no);
  344. if ($order_info) return response()->error('WAP_SYS_ERROR');
  345. $cid = $request->has('cid') ? $request->get('cid') : '';
  346. $product_info = ProductService::getProductSingle($product_id,false);
  347. //新用户只能冲一次
  348. //Log::info('新用户只能冲一次:' .$product_info->type);
  349. if($this->isNewUserSecondCharge($product_info->type,$product_id,$uid)){
  350. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  351. return redirect()->to($url);
  352. }
  353. $price = $product_info->price * 100;
  354. if ($uid < 32) {
  355. $price = 1;
  356. }
  357. if (!$send_order_id) {
  358. try {
  359. $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
  360. } catch (\Exception $e) {}
  361. }
  362. $this->updateUserSendOrderId($uid,$send_order_id);
  363. if (in_array($uid, explode(',', env('TEST_UID')))) {
  364. $price = 1;
  365. }
  366. try {
  367. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  368. Redis::hset($key, $uid, time());
  369. $date = date('Y-m-d');
  370. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  371. if ($send_order_id) {
  372. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  373. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  374. }
  375. } catch (\Exception $e) {
  376. }
  377. $data = [];
  378. $data['price'] = $price;
  379. $data['create_ip'] = $request->getClientIp();
  380. $data['body'] = 'novel read';
  381. $official_name = $this->getSubscribeOfficialName($uid);
  382. if ($official_name) {
  383. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  384. }
  385. $data['detail'] = 'novel read';
  386. $data['trade_no'] = $trade_no;
  387. if ($product_info->type == 'YEAR_ORDER') {
  388. $order_type = 'YEAR';
  389. } elseif ($product_info->type == 'BOOK_ORDER') {
  390. $order_type = 'BOOK';
  391. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  392. $order_type = 'RECHARGE';
  393. } elseif($product_info->type == 'NEW_USER'){
  394. $order_type = 'RECHARGE';
  395. }else{
  396. $order_type = 'UNKNOWN';
  397. }
  398. $send_order_name = '';
  399. if ($send_order_id) {
  400. $send_order_info = SendOrderService::getById($send_order_id);
  401. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  402. $send_order_name = $send_order_info->name;
  403. }
  404. }
  405. if($suid){
  406. $user_info = UserService::getById($uid);
  407. $distribution_channel_id = $user_info->distribution_channel_id;
  408. }
  409. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  410. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  411. $init_order = [
  412. 'distribution_channel_id' => $distribution_channel_id,
  413. 'uid' => $uid,
  414. 'product_id' => $product_id,
  415. 'price' => $price / 100,
  416. 'pay_type' => 1,
  417. 'trade_no' => $data['trade_no'],
  418. 'pay_merchant_source' => 'PALMPAY',
  419. 'pay_merchant_id' => $pay_merchant->id,
  420. 'create_ip' => $request->getClientIp(),
  421. 'send_order_id' => $send_order_id,
  422. 'send_order_name' => $send_order_name,
  423. 'order_type' => $order_type,
  424. 'from_bid' => $from_bid,
  425. 'from_type' => $fromtype,
  426. 'activity_id' => $activity_id,
  427. 'inner_send_order_id'=>$inner_send_order_id
  428. ];
  429. $result = $this->createUnPayOrder($init_order);
  430. //订单创建成功后增加统计
  431. if($result)
  432. {
  433. $this->orderCreated($init_order);
  434. }
  435. if ($suid) {
  436. $this->createSubstituteOrder($result->id, $uid, $suid);
  437. }
  438. if ($suid) {
  439. $prize_fee = (int)(($product_info->price*100)*0.1);
  440. //$help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  441. $help_pay_page_channel_id = $distribution_channel_id;
  442. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  443. $pay_redirect_url = sprintf(
  444. $url_format,
  445. env('PROTOCOL'),
  446. encodeDistributionChannelId($help_pay_page_channel_id),
  447. env('CUSTOM_HOST'),
  448. $prize_fee,
  449. $uid
  450. );
  451. }
  452. $de_pay_redirect_url = urldecode($pay_redirect_url);
  453. /*if ($request->has('cid')) {
  454. $de_pay_redirect_url = $de_pay_redirect_url . '&cid=' . $request->get('cid');
  455. }*/
  456. $pay_url_info = parse_url($de_pay_redirect_url);
  457. $data['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/waitv2?order=' . $data['trade_no'] . '&redirect=' . urlencode($de_pay_redirect_url);
  458. Log::info($data);
  459. $config = [];
  460. if ($pay_merchant->config_info) {
  461. $config = json_decode($pay_merchant->config_info, true);
  462. }
  463. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  464. \Log::info('$wechatPay is');
  465. //\Log::info($wechatPay);
  466. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  467. \Log::info('$wechatPay ok-------------------');
  468. $pay_info = $wechatPay->send($data);
  469. \Log::info('send ok-------------------');
  470. return $pay_info;
  471. }
  472. public function waitPageV2(Request $request)
  473. {
  474. $order = $request->get('order');
  475. $order = (string)$order;
  476. $url = urldecode($request->get('redirect'));
  477. if(substr_count($url,'?') >1){
  478. $url = str_replace_last('?','&',$url);
  479. }
  480. $url_info = parse_url($url);
  481. if (isset($url_info['query']) && !empty($url_info['query'])) {
  482. parse_str($url_info['query'], $path);
  483. if (isset($path['success']) && $path['success'] == 0) {
  484. if (isset($url_info['path']) && str_contains($url_info['path'], 'reader') && isset($path['cid']) && !empty($path['cid'])) {
  485. $chapter_info = ChapterService::getChapterNameByIdNoCheck($path['cid']);
  486. $path['cid'] = $chapter_info->prev_cid;
  487. $url = sprintf('%s://%s/reader?%s', $url_info['scheme'], $url_info['host'], http_build_query($path));
  488. }
  489. return redirect($url);
  490. }
  491. }
  492. return view('pay.order.wait', compact('order', 'url'));
  493. }
  494. private function isNewUserSecondCharge($charge_type,$product_id,$uid){
  495. //Log::info('新用户只能冲一次:' .$charge_type);
  496. if($charge_type == 'NEW_USER'){
  497. $result = OrderService::getUserOrderByProductId($uid,$product_id);
  498. //Log::info('isNewUserSecondCharge-------------------------');
  499. //Log::info($result);
  500. if($result){
  501. return true;
  502. }
  503. }
  504. return false;
  505. }
  506. //订单创建后统计
  507. private function orderCreated($init_order)
  508. {
  509. try {
  510. if(isset($init_order['pay_merchant_id']))
  511. {
  512. $key = 'pay_merchant:'.$init_order['pay_merchant_id'];
  513. Redis::hincrby($key,'unpaid_num',1);
  514. Redis::hset($key,'last_create_time', time());
  515. }
  516. }catch (\Exception $e)
  517. {
  518. }
  519. }
  520. //订单回调后统计
  521. private function orderPaid($init_order)
  522. {
  523. try {
  524. if(isset($init_order['pay_merchant_id']))
  525. {
  526. $key = 'pay_merchant:'.$init_order['pay_merchant_id'];
  527. Redis::hset($key,'unpaid_num',0);
  528. }
  529. }catch (\Exception $e)
  530. {
  531. }
  532. }
  533. public function reportError(Request $request)
  534. {
  535. $data = $request->post('data');
  536. DB::table('pay_page_error')->insert([
  537. 'msg' => $data,
  538. 'created_at' => date('Y-m-d H:i:s'),
  539. 'updated_at' => date('Y-m-d H:i:s')
  540. ]);
  541. return response()->success();
  542. }
  543. private function jsSdkSign($pay_info, $url)
  544. {
  545. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  546. return 0;
  547. }
  548. $appid = $pay_info['appId'];
  549. $jsapi_ticket = Redis::hget($appid, 'jsapi_ticket');
  550. //如果能获取到jsapi_ticket 则直接签名
  551. if ($jsapi_ticket) {
  552. $sign_arr = [
  553. 'noncestr' => $pay_info['nonceStr'],
  554. 'jsapi_ticket' => $jsapi_ticket,
  555. 'timestamp' => $pay_info['timeStamp'],
  556. 'url' => $url
  557. ];
  558. return sha1($this->arr_to_url($sign_arr));
  559. }
  560. //获取不到jsapi_ticket
  561. $app_secret_info = DB::table('official_setting')->select('secret')->first();
  562. if ($app_secret_info && !empty($app_secret_info->secret)) {
  563. $client = new Client(['timeout' => 5]);
  564. try {
  565. //获取access_token
  566. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $app_secret_info->secret;
  567. $res = $client->request('get', $url)->getBody()->getContents();
  568. if (!$res) {
  569. return 0;
  570. }
  571. $res = json_decode($res, 1);
  572. if (!isset($res['access_token']) || empty($res['access_token'])) {
  573. return 0;
  574. }
  575. Redis::hset($appid, 'access_token', $res['access_token']);
  576. //获取jsapi_ticket
  577. $res = null;
  578. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $res['access_token'] . '&type=jsapi';
  579. $res = $client->request('get', $url)->getBody()->getContents();
  580. if (!$res) {
  581. return 0;
  582. }
  583. $res = json_decode($res, 1);
  584. if (!isset($res['ticket']) || empty($res['ticket'])) {
  585. return 0;
  586. }
  587. Redis::hset($appid, 'jsapi_ticket', $res['ticket']);
  588. Redis::EXPIRE($appid, 7000);
  589. $sign_arr = [
  590. 'noncestr' => $pay_info['nonceStr'],
  591. 'jsapi_ticket' => $res['ticket'],
  592. 'timestamp' => $pay_info['timeStamp'],
  593. 'url' => $url
  594. ];
  595. return sha1($this->arr_to_url($sign_arr));
  596. } catch (\Exception $e) {
  597. return 0;
  598. }
  599. }
  600. return 0;
  601. }
  602. public function waitPage(Request $request)
  603. {
  604. $order = $request->input('order');
  605. //$order = "201712081711401585932843356442";
  606. $order = (string)$order;
  607. $url = urldecode($request->input('redirect'));
  608. return view('pay.order.wait', compact('order', 'url'));
  609. //return view('pay.order.wait');
  610. }
  611. /**
  612. * 官方微信回调
  613. * @param Request $request
  614. * @return \Symfony\Component\HttpFoundation\Response
  615. */
  616. function wcback_official(Request $request)
  617. {
  618. $xml = XML::parse(strval($request->getContent()));
  619. Log::info('xml is');
  620. Log::info($xml);
  621. if (!$xml || !is_array($xml)) return 'fail';
  622. $remarks = json_decode($xml['attach'], true);
  623. $pay_merchant_id = $remarks['pmi'];
  624. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  625. $application = WechatPay::instance('OFFICIALPAY', json_decode($pay_merchant->config_info, true));
  626. //$app = new Application($options);
  627. $app = $application->app;
  628. $response = $app->payment->handleNotify(function ($notify, $successful) {
  629. if (!$successful) return 'fail';
  630. $trade_no = $notify->out_trade_no;
  631. $order = OrderService::getByTradeNo($trade_no);
  632. if (isset($order->status) && $order->status == 'PAID') {
  633. Log::info('has_pay:' . $trade_no);
  634. return true;
  635. }
  636. DB::beginTransaction();
  637. try {
  638. $transaction_id = $notify->transaction_id;
  639. $remarks = json_decode($notify->attach, true);
  640. $uid = $remarks['uid'];
  641. $distribution_channel_id = $remarks['dcd'];
  642. $product_id = $remarks['pd'];
  643. $product = ProductService::getProductSingle($product_id);
  644. $pay_merchant_source = $remarks['pms'];
  645. $pay_merchant_id = $remarks['pmi'];
  646. $send_order_id = $remarks['soi'];
  647. $create_ip = $remarks['cp'];
  648. $price = $product->price;
  649. $bid = $remarks['bid'];
  650. //$this->updateOrderTotal($trade_no,$transaction_id);
  651. $prize_fee = (int)(($product->price*100)*0.1);
  652. $this->substituteOrderPrice($order->id,$prize_fee);
  653. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  654. // 更新其他定制Order表
  655. if ($product->type == 'YEAR_ORDER') {
  656. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  657. $order_type = 'YEAR';
  658. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  659. $order->order_type = $order_type;
  660. $order->status = 'PAID';
  661. $order->pay_end_at = date('Y-m-d H:i:s');
  662. $order->transaction_id = $transaction_id;
  663. $order->save();
  664. //$price = $price/100;
  665. /*
  666. $this->createOrder(
  667. compact(
  668. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  669. 'pay_merchant_id','create_ip','bid','transaction_id'
  670. )
  671. );*/
  672. } elseif ($product->type == 'BOOK_ORDER') {
  673. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  674. $order_type = 'BOOK';
  675. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  676. $order->order_type = $order_type;
  677. $order->status = 'PAID';
  678. $order->pay_end_at = date('Y-m-d H:i:s');
  679. $order->transaction_id = $transaction_id;
  680. $order->save();
  681. //$price = $price/100;
  682. /*
  683. $this->createOrder(
  684. compact(
  685. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  686. 'pay_merchant_id','create_ip','bid','transaction_id'
  687. )
  688. );*/
  689. } elseif ($product->type == 'TICKET_RECHARGE') {
  690. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  691. $order_type = 'RECHARGE';
  692. $this->userCharge($product, $uid);
  693. $order->order_type = $order_type;
  694. $order->status = 'PAID';
  695. $order->pay_end_at = date('Y-m-d H:i:s');
  696. $order->transaction_id = $transaction_id;
  697. $order->save();
  698. //$price = $price/100;
  699. /*
  700. $this->createOrder(
  701. compact(
  702. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  703. 'pay_merchant_id','create_ip','bid','transaction_id'
  704. ));*/
  705. } elseif ($product->type == 'FOREVER') {
  706. Log::info('FOREVER------------------------FOREVER');
  707. $order_type = 'FOREVER';
  708. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  709. $order->order_type = $order_type;
  710. $order->status = 'PAID';
  711. $order->pay_end_at = date('Y-m-d H:i:s');
  712. $order->transaction_id = $transaction_id;
  713. $order->save();
  714. } else {
  715. DB::rollback();
  716. return 'Order not exist.';
  717. }
  718. $this->successPayPushMsg($uid, $product, $order->id);
  719. $this->orderStatistical($order);
  720. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  721. Redis::hdel($key, $uid);
  722. DB::commit();
  723. return true;
  724. } catch (\Exception $e) {
  725. DB::rollback();
  726. return 'fail';
  727. }
  728. });
  729. return $response;
  730. }
  731. /**
  732. * 通联支付回调
  733. * @param Request $request
  734. */
  735. function wcback_allinpay(Request $request)
  736. {
  737. Log::info('wcback_allinpay_request');
  738. try {
  739. $data = $request->all();
  740. Log::info($request->all());
  741. if (!$data['trxreserved']) return 'fail';
  742. $pay_merchant_id = json_decode($data['trxreserved'], true)['pmi'];
  743. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  744. unset($data['_url']);
  745. if (!$pay_merchant->config_info) return 'fail';
  746. $wechatPay = WechatPay::instance('ALLINPAY', json_decode($pay_merchant->config_info, true));
  747. if ($wechatPay->NotifyValidSign($data, $wechatPay->appkey)) {
  748. Log::info('allinpay_notify_sign_success');
  749. /**
  750. * 只有0000表示交易成功或下单成功,其他为失败
  751. * 0000:交易成功
  752. * 3045,3088:交易超时
  753. * 3008:余额不足
  754. * 3999:交易失败
  755. * 2008:交易处理中
  756. * 3050:交易已撤销
  757. */
  758. if ($data['trxstatus'] == '0000') {
  759. // 修改表比较多,开启事务
  760. DB::beginTransaction();
  761. $trade_no = $data['cusorderid'];
  762. $transaction_id = $data['chnltrxid'];
  763. //$openid = $data['acct'];
  764. $remarks = $data['trxreserved'];
  765. // $remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
  766. $remarks = json_decode($remarks, true);
  767. Log::info('$remarks');
  768. Log::info($remarks);
  769. // 更新Order
  770. $order = OrderService::getByTradeNo($trade_no);
  771. // 防止重复推送
  772. if (isset($order->status) && $order->status == 'PAID') {
  773. Log::info('has_pay:' . $trade_no);
  774. echo 'success';
  775. exit();
  776. }
  777. $uid = $remarks['uid'];
  778. $distribution_channel_id = $remarks['dcd'];
  779. $product_id = $remarks['pd'];
  780. $product = ProductService::getProductSingle($product_id);
  781. $pay_merchant_source = $remarks['pms'];
  782. $pay_merchant_id = $remarks['pmi'];
  783. $send_order_id = $remarks['soi'];
  784. $create_ip = $remarks['cp'];
  785. $price = $product->price;
  786. $bid = $remarks['bid'];
  787. Log::info('save_order_end');
  788. //$this->updateOrderTotal($trade_no,$transaction_id);
  789. Log::info($product);
  790. Log::info('product_type:' . $product->type);
  791. $prize_fee = (int)(($product->price*100)*0.1);
  792. $this->substituteOrderPrice($order->id,$prize_fee);
  793. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  794. // 更新其他定制Order表
  795. if ($product->type == 'YEAR_ORDER') {
  796. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  797. $order_type = 'YEAR';
  798. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  799. $order->order_type = $order_type;
  800. $order->status = 'PAID';
  801. $order->pay_end_at = date('Y-m-d H:i:s');
  802. $order->transaction_id = $transaction_id;
  803. $order->save();
  804. //$price = $price/100;
  805. /*
  806. $this->createOrder(
  807. compact(
  808. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  809. 'pay_merchant_id','create_ip','bid','transaction_id'
  810. )
  811. );*/
  812. } elseif ($product->type == 'BOOK_ORDER') {
  813. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  814. $order_type = 'BOOK';
  815. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  816. $order->order_type = $order_type;
  817. $order->status = 'PAID';
  818. $order->pay_end_at = date('Y-m-d H:i:s');
  819. $order->transaction_id = $transaction_id;
  820. $order->save();
  821. //$price = $price/100;
  822. /*
  823. $this->createOrder(
  824. compact(
  825. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  826. 'pay_merchant_id','create_ip','bid','transaction_id'
  827. )
  828. );*/
  829. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type=='NEW_USER') {
  830. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  831. $order_type = 'RECHARGE';
  832. $this->userCharge($product, $uid);
  833. $order->order_type = $order_type;
  834. $order->status = 'PAID';
  835. $order->pay_end_at = date('Y-m-d H:i:s');
  836. $order->transaction_id = $transaction_id;
  837. $order->save();
  838. //$price = $price/100;
  839. /*
  840. $this->createOrder(
  841. compact(
  842. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  843. 'pay_merchant_id','create_ip','bid','transaction_id'
  844. ));*/
  845. } elseif ($product->type == 'FOREVER') {
  846. Log::info('FOREVER------------------------FOREVER');
  847. $order_type = 'FOREVER';
  848. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  849. $order->order_type = $order_type;
  850. $order->status = 'PAID';
  851. $order->pay_end_at = date('Y-m-d H:i:s');
  852. $order->transaction_id = $transaction_id;
  853. $order->save();
  854. }
  855. $this->successPayPushMsg($uid, $product, $order->id);
  856. $this->orderStatistical($order);
  857. DB::commit();
  858. //redis 删除未支付的uid
  859. try {
  860. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  861. Redis::hdel($key, $uid);
  862. //Redis::srem($key,$uid);
  863. } catch (\Exception $e) {
  864. Log::info('redis remote error-----------------------');
  865. Log::info($e);
  866. }
  867. }
  868. } else {
  869. Log::info('allinpay_notify_sign_fail');
  870. }
  871. } catch (\Exception $e) {
  872. DB::rollBack();
  873. Log::info('receive_allinpay_ept:' . $e->getMessage());
  874. Log::info('pay_callback_end fail');
  875. echo 'fail';
  876. return ;
  877. }
  878. Log::info('pay_callback_end');
  879. echo "success";
  880. }
  881. public function wcback_lianlianpay(Request $request)
  882. {
  883. $data = $request->getContent();
  884. Log::info('wcback_lianlianpay call back enter');
  885. Log::info($request->getContent());
  886. $data = json_decode($data, true);
  887. if ($data['result_pay'] == 'SUCCESS') {
  888. $trade_no = $data['no_order'];
  889. $order = OrderService::getByTradeNo($trade_no);
  890. if (!$order) return response()->json(['ret_code' => '-1']);
  891. $pay_merchant_id = $order->pay_merchant_id;
  892. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  893. unset($data['_url']);
  894. if (!$pay_merchant->config_info) response()->json(['ret_code' => '-1']);
  895. $wechatPay = WechatPay::instance('LIANLIANPAY', json_decode($pay_merchant->config_info, true));
  896. $uid = $order->uid;
  897. $distribution_channel_id = $order->distribution_channel_id;
  898. $price = $order->price;
  899. $send_order_id = $order->send_order_id;
  900. $product_id = $order->product_id;
  901. if (!$wechatPay->rsaCheck($data)) {
  902. Log::info('sign check error');
  903. //return response()->json(['ret_code'=>'-1']);
  904. }
  905. // 防止重复推送
  906. if (isset($order->status) && $order->status == 'PAID') {
  907. Log::info('has_pay:' . $trade_no);
  908. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  909. }
  910. DB::beginTransaction();
  911. try {
  912. $product = ProductService::getProductSingle($order->product_id);
  913. $transaction_id = $data['oid_paybill'];
  914. $prize_fee = (int)(($product->price*100));
  915. $this->substituteOrderPrice($order->id,$prize_fee);
  916. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  917. // 更新其他定制Order表
  918. if ($product->type == 'YEAR_ORDER') {
  919. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  920. $order_type = 'YEAR';
  921. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  922. $order->order_type = $order_type;
  923. $order->status = 'PAID';
  924. $order->pay_end_at = date('Y-m-d H:i:s');
  925. $order->transaction_id = $transaction_id;
  926. $order->save();
  927. } elseif ($product->type == 'BOOK_ORDER') {
  928. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  929. $order_type = 'BOOK';
  930. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  931. $order->order_type = $order_type;
  932. $order->status = 'PAID';
  933. $order->pay_end_at = date('Y-m-d H:i:s');
  934. $order->transaction_id = $transaction_id;
  935. $order->save();
  936. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type=='NEW_USER') {
  937. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  938. $order_type = 'RECHARGE';
  939. $this->userCharge($product, $uid);
  940. $order->order_type = $order_type;
  941. $order->status = 'PAID';
  942. $order->pay_end_at = date('Y-m-d H:i:s');
  943. $order->transaction_id = $transaction_id;
  944. $order->save();
  945. } elseif ($product->type == 'FOREVER') {
  946. Log::info('FOREVER------------------------FOREVER');
  947. $order_type = 'FOREVER';
  948. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  949. $order->order_type = $order_type;
  950. $order->status = 'PAID';
  951. $order->pay_end_at = date('Y-m-d H:i:s');
  952. $order->transaction_id = $transaction_id;
  953. $order->save();
  954. }
  955. $this->successPayPushMsg($uid, $product, $order->id);
  956. $this->orderStatistical($order);
  957. DB::commit();
  958. //redis 删除未支付的uid
  959. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
  960. Redis::hdel($key, $uid);
  961. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  962. } catch (\Exception $e) {
  963. DB::rollBack();
  964. Log::info('lianlian:' . $e->getMessage());
  965. echo 'fail';
  966. return ;
  967. }
  968. }
  969. echo 'success';
  970. }
  971. /**
  972. * 微众支付回调
  973. * @param Request $request
  974. */
  975. function wcback_palmpay(Request $request)
  976. {
  977. Log::info('wcback_palmpay_request');
  978. try {
  979. $data = $request->except('_url');
  980. Log::info($request->all());
  981. //订单
  982. $trade_no = $data['outTradeNo'];
  983. $order = OrderService::getByTradeNo($trade_no);
  984. if (!$order) die('failed');
  985. // 防止重复推送
  986. if (isset($order->status) && $order->status == 'PAID') {
  987. Log::info('has_pay:' . $trade_no);
  988. echo 'success';
  989. exit();
  990. }
  991. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  992. //校验签名
  993. $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  994. if ($sign != $data['sign']) die('failed');
  995. $transaction_id = $data['chorderid'];
  996. if ($data['status'] == 'success') {
  997. Log::info('wcback_palmpay_notify_sign_success');
  998. if ($data['status'] == 'success') {
  999. // 修改表比较多,开启事务
  1000. DB::beginTransaction();
  1001. // 更新Order
  1002. $order = OrderService::getByTradeNo($trade_no);
  1003. $uid = $order->uid;
  1004. $distribution_channel_id = $order->distribution_channel_id;
  1005. $product_id = $order->product_id;
  1006. $product = ProductService::getProductSingle($product_id);
  1007. $send_order_id = $order->send_order_id;
  1008. $price = $product->price;
  1009. Log::info('save_order_end');
  1010. Log::info($product);
  1011. Log::info('product_type:' . $product->type);
  1012. $prize_fee = (int)(($product->price*100)*0.1);
  1013. $this->substituteOrderPrice($order->id,$prize_fee);
  1014. $this->smartPushTestBookPaidUv($order->from_bid,$order->uid,$order->price);
  1015. // 更新其他定制Order表
  1016. if ($product->type == 'YEAR_ORDER') {
  1017. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1018. $order_type = 'YEAR';
  1019. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1020. $order->order_type = $order_type;
  1021. $order->status = 'PAID';
  1022. $order->pay_end_at = date('Y-m-d H:i:s');
  1023. $order->transaction_id = $transaction_id;
  1024. $order->save();
  1025. } elseif ($product->type == 'BOOK_ORDER') {
  1026. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1027. $order_type = 'BOOK';
  1028. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1029. $order->order_type = $order_type;
  1030. $order->status = 'PAID';
  1031. $order->pay_end_at = date('Y-m-d H:i:s');
  1032. $order->transaction_id = $transaction_id;
  1033. $order->save();
  1034. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type=='NEW_USER') {
  1035. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1036. $order_type = 'RECHARGE';
  1037. $this->userCharge($product, $uid);
  1038. $order->order_type = $order_type;
  1039. $order->status = 'PAID';
  1040. $order->pay_end_at = date('Y-m-d H:i:s');
  1041. $order->transaction_id = $transaction_id;
  1042. $order->save();
  1043. } elseif ($product->type == 'FOREVER') {
  1044. Log::info('FOREVER------------------------FOREVER');
  1045. $order_type = 'FOREVER';
  1046. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1047. $order->order_type = $order_type;
  1048. $order->status = 'PAID';
  1049. $order->pay_end_at = date('Y-m-d H:i:s');
  1050. $order->transaction_id = $transaction_id;
  1051. $order->save();
  1052. }
  1053. $this->successPayPushMsg($uid, $product, $order->id);
  1054. $this->orderStatistical($order);
  1055. DB::commit();
  1056. //redis 删除未支付的uid
  1057. try {
  1058. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1059. Redis::hdel($key, $uid);
  1060. } catch (\Exception $e) {
  1061. Log::info('redis remote error-----------------------');
  1062. Log::info($e);
  1063. }
  1064. }
  1065. } else {
  1066. Log::info('wcback_palmpay_notify_sign_fail');
  1067. }
  1068. } catch (\Exception $e) {
  1069. var_dump($e->getMessage());
  1070. DB::rollBack();
  1071. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  1072. echo 'fail';
  1073. Log::info('pay_callback_end error');
  1074. return;
  1075. }
  1076. Log::info('pay_callback_end');
  1077. echo "success";
  1078. }
  1079. /**
  1080. * 公众号签名@华灯初上
  1081. * @param $params
  1082. * @return string
  1083. */
  1084. protected function getSign($params, $key)
  1085. {
  1086. $url = $this->arr_to_url($params, false);
  1087. $url = $url . '&key=' . $key;
  1088. $sign = md5($url);
  1089. return $sign;
  1090. }
  1091. /**
  1092. * 公众号签名@华灯初上
  1093. * @param $array
  1094. * @param bool $has_sign
  1095. * @return string
  1096. */
  1097. protected function arr_to_url($array, $has_sign = false)
  1098. {
  1099. ksort($array);
  1100. reset($array);
  1101. $arg = "";
  1102. while (list ($name, $val) = each($array)) {
  1103. if ($name == 'sign' && !$has_sign) continue;
  1104. if (strpos($name, "_") === 0)
  1105. continue;
  1106. if (is_array($val))
  1107. $val = join(',', $val);
  1108. if ($val === "")
  1109. continue;
  1110. $arg .= $name . "=" . $val . "&";
  1111. }
  1112. $arg = substr($arg, 0, count($arg) - 2);
  1113. return $arg;
  1114. }
  1115. /**
  1116. * 单本充值会掉
  1117. * @param $product_id
  1118. * @param $uid
  1119. * @param $send_order_id
  1120. * @param $fee
  1121. */
  1122. protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  1123. {
  1124. $book_conf = BookConfigService::getBookByProduct($product_id);
  1125. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  1126. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  1127. $insert_data['uid'] = $uid;
  1128. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1129. $insert_data['fee'] = $fee;
  1130. $insert_data['send_order_id'] = $send_order_id;
  1131. $insert_data['charge_balance'] = 0;
  1132. $insert_data['reward_balance'] = 0;
  1133. Log::info('start_save_book_order');
  1134. Log::info($insert_data);
  1135. return BookOrderService::save_book_order($insert_data);
  1136. }
  1137. /**
  1138. * 包年
  1139. * @param $uid
  1140. * @param $distribution_channel_id
  1141. * @param $fee
  1142. * @param $send_order_id
  1143. * @return mixed
  1144. */
  1145. protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1146. {
  1147. Log::info('start_save_year_order');
  1148. $insert_data['uid'] = $uid;
  1149. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1150. $insert_data['fee'] = $fee;
  1151. $insert_data['send_order_id'] = $send_order_id;
  1152. Log::info($insert_data);
  1153. return YearOrderService::save_year_order($insert_data);
  1154. }
  1155. private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1156. {
  1157. Log::info('start_save_year_order');
  1158. ('start_forever_Free_Order');
  1159. $insert_data['uid'] = $uid;
  1160. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1161. $insert_data['fee'] = $fee;
  1162. $insert_data['send_order_id'] = $send_order_id;
  1163. Log::info($insert_data);
  1164. return YearOrderService::foreverFree($insert_data);
  1165. }
  1166. /**
  1167. * 用户充值
  1168. * @param $product
  1169. * @param $uid\
  1170. */
  1171. protected function userCharge($product, $uid)
  1172. {
  1173. $total = $product->price * 100 + $product->given;
  1174. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  1175. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  1176. }
  1177. /**
  1178. * 添加订单
  1179. * @param array $data
  1180. * @return mixed
  1181. */
  1182. protected function createOrder(array $data)
  1183. {
  1184. $insert_data = array();
  1185. $insert_data['uid'] = $data['uid'];
  1186. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  1187. $insert_data['product_id'] = $data['product_id'];
  1188. $insert_data['price'] = $data['price'];
  1189. $insert_data['status'] = 'PAID';
  1190. $insert_data['pay_type'] = 1;
  1191. $insert_data['trade_no'] = $data['trade_no'];
  1192. $insert_data['send_order_id'] = $data['send_order_id'];
  1193. $insert_data['order_type'] = $data['order_type'];
  1194. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  1195. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  1196. $insert_data['transaction_id'] = $data['transaction_id'];
  1197. $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
  1198. $insert_data['create_ip'] = $data['create_ip'];
  1199. $insert_data['from_bid'] = $data['bid'];
  1200. Log::info('createOrder');
  1201. Log::info($insert_data);
  1202. return OrderService::save_order($insert_data);
  1203. }
  1204. /**
  1205. * 添加位置付订单
  1206. * @param $data
  1207. * @return mixed
  1208. */
  1209. protected function createUnPayOrder($data)
  1210. {
  1211. $data['status'] = 'UNPAID';
  1212. $data['transaction_id'] = '';
  1213. $data['pay_end_at'] = '0000-00-00 00:00:00';
  1214. return OrderService::save_order($data);
  1215. }
  1216. /**
  1217. * order total
  1218. * @param $data
  1219. *
  1220. */
  1221. protected function createOrderTotal($data)
  1222. {
  1223. $data['status'] = 'UNPAID';
  1224. $data['created_at'] = date('Y-m-d H:i:s');
  1225. $data['updated_at'] = date('Y-m-d H:i:s');
  1226. DB::table('orders_total')->insert($data);
  1227. }
  1228. protected function updateOrderTotal($trade_no, $transaction_id)
  1229. {
  1230. $data['status'] = 'PAID';
  1231. $data['transaction_id'] = $transaction_id;
  1232. $data['pay_end_at'] = date('Y-m-d H:i:s');
  1233. //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
  1234. }
  1235. protected function successPayPushMsg($uid, $product_info, $order_id)
  1236. {
  1237. try {
  1238. $force_sub_info = $this->getSubscribeV2($uid);
  1239. $data = UserService::getById($uid);
  1240. if (!in_array($data->distribution_channel_id, [5, 123])) {
  1241. //return false;
  1242. }
  1243. if ($force_sub_info) {
  1244. //$content_format = "您好,你已经成功充值\r\n\r\n会员:%s\r\n会员ID:%s\r\n充值金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
  1245. if (SubstituteOrderService::getByOrderId($order_id)) {
  1246. $content_format = "代充值客服消息:\r\n\r\n你好,您的好友为你代付成功\r\n会员:%s\r\n会员ID:%s\r\n代付金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
  1247. } else {
  1248. $content_format = "您好,你已经成功充值\r\n\r\n会员:%s\r\n会员ID:%s\r\n充值金额:%s\r\n充值状态:充值成功\r\n如有疑问,请点击用户中心-联系客服\r\n\r\n><a href='%s'>点击继续阅读上次阅读</a>";
  1249. }
  1250. if ($product_info->type == 'YEAR_ORDER') {
  1251. $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
  1252. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  1253. if ($product_info->given) {
  1254. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
  1255. } else {
  1256. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
  1257. }
  1258. } else {
  1259. $money_text = '未知';
  1260. }
  1261. $delay = 0;
  1262. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  1263. $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
  1264. foreach ($force_sub_info as $item){
  1265. $res['openid'] = $item->openid;
  1266. $res['appid'] = $item->appid;
  1267. $res['content'] = $content;
  1268. $res['type'] = 'one_task';
  1269. $res['send_time'] = date("Y-m-d H:i:s");
  1270. $res['task_id'] = md5('pay_success_push');
  1271. $send_data = array(
  1272. 'send_time' => date("Y-m-d H:i:s"),
  1273. 'data' => $res
  1274. );
  1275. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  1276. }
  1277. }
  1278. } catch (\Exception $e) {
  1279. Log::info('pay_success_push error');
  1280. Log::info($e);
  1281. }
  1282. return true;
  1283. }
  1284. private function orderStatistical($order)
  1285. {
  1286. try {
  1287. $send_data = array(
  1288. 'type' => 'order_info',
  1289. 'data' => $order->toArray()
  1290. );
  1291. $send = new SendStatisticsList($send_data);
  1292. $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
  1293. dispatch($job);
  1294. } catch (\Exception $e) {
  1295. Log::info($e);
  1296. }
  1297. //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
  1298. try{
  1299. if($order->from_type){
  1300. $pv = Redis::hget('push:distribution_channel_id:allpv' ,$order->from_type);
  1301. if($pv){
  1302. Redis::sadd('push:all:paidnum:from:' . $order->from_type,$order->uid);
  1303. $old = Redis::hget('push:all:paidamount' ,$order->from_type);
  1304. if($old){
  1305. Redis::hset('push:all:paidamount' ,$order->from_type,$old+$order->price);
  1306. }else{
  1307. Redis::hset('push:all:paidamount' ,$order->from_type,$order->price);
  1308. }
  1309. }
  1310. }
  1311. if(isset($order->pay_merchant_id) && $order->pay_merchant_id)//临时统计队列清0 用于报警
  1312. {
  1313. $key = 'pay_merchant:'.$order->pay_merchant_id;
  1314. Redis::hset($key,'unpaid_num',0);
  1315. }
  1316. }catch (\Exception $e){}
  1317. }
  1318. private function getSubscribe($uid)
  1319. {
  1320. return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  1321. }
  1322. private function getSubscribeV2($uid)
  1323. {
  1324. return DB::table('force_subscribe_users')->where('uid',$uid)->where('is_subscribed', 1)->get();
  1325. }
  1326. public function getSubscribeOfficialName($uid)
  1327. {
  1328. $subscribe = $this->getSubscribe($uid);
  1329. if ($subscribe && isset($subscribe->appid)) {
  1330. $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
  1331. if ($official && isset($official->nickname)) {
  1332. return $official->nickname;
  1333. }
  1334. }
  1335. return '';
  1336. }
  1337. private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
  1338. {
  1339. $change_pay_id = 9;
  1340. if ($pay_merchant_id == 9) {
  1341. //$change_pay_id = 12;
  1342. }
  1343. try {
  1344. $time = (int)date('G');
  1345. $change = false;
  1346. if ($time <= 8 || $time >= 23) {
  1347. if (!$pay_info && $pay_merchant_id != $change_pay_id) {
  1348. DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
  1349. $change = true;
  1350. }
  1351. }
  1352. //$number,$template_type,$param=array()
  1353. $phone_arr = ['15868100210', '18072713392', '15088790066', '13858057394', '18668029091', '18668420256'];
  1354. //$phone_arr = ['18668029091'];
  1355. $param = ['pay_id'=>$pay_merchant_id];
  1356. if ($n) {
  1357. $template_type = 'pay_channel_remind';
  1358. //$content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
  1359. } else {
  1360. $template_type = 'pay_channel_remind';
  1361. //$content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
  1362. }
  1363. if ($change) {
  1364. $template_type = 'pay_channel_change';
  1365. $param = ['pay_id'=>$pay_merchant_id,'new_pay_id'=>$change_pay_id];
  1366. }
  1367. foreach ($phone_arr as $phone) {
  1368. AliSMS::send($phone, $template_type,$param);
  1369. }
  1370. } catch (\Exception $e) {
  1371. }
  1372. }
  1373. private function createSubstituteOrder($order_id, $uid, $pay_uid)
  1374. {
  1375. SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
  1376. }
  1377. private function substituteOrderPrice($order_id,$prize_fee=500)
  1378. {
  1379. SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
  1380. }
  1381. private function updateUserSendOrderId(int $uid,$send_order_id){
  1382. if($send_order_id && $uid){
  1383. $user = UserService::getById($uid);
  1384. if($user && !$user->send_order_id){
  1385. UserService::updateInfo($uid,compact('send_order_id'));
  1386. }
  1387. }
  1388. }
  1389. /**
  1390. * 测书智能推送统计 付费用户数、充值金额
  1391. * @param $bid
  1392. * @param $uid
  1393. * @param $price
  1394. */
  1395. private function smartPushTestBookPaidUv($bid,$uid,$price){
  1396. $smart_bid = ReadRecordService::getSmartPush($uid);
  1397. if( $smart_bid && in_array($bid,$smart_bid) ){
  1398. try{
  1399. $key = 'smartPushTestBookPaidUv:bid:%s';
  1400. Redis::sadd(sprintf($key,$bid),$uid);
  1401. $now_amount = Redis::hget('smartPushTestBookPaidAmount',$bid);
  1402. if(!$now_amount){
  1403. $now_amount = 0;
  1404. }
  1405. Redis::hset('smartPushTestBookPaidAmount',$bid,$now_amount+$price);
  1406. }catch (\Exception $e){
  1407. }
  1408. }
  1409. }
  1410. }