OrdersController.php 64 KB

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