OrdersController.php 111 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545
  1. <?php
  2. namespace App\Http\Controllers\Wap\Pay;
  3. use App\Modules\Trade\Pay\BookOrderPaySuccess;
  4. use App\Modules\Trade\Pay\ForeverOrderPaySuccess;
  5. use App\Modules\Trade\Pay\RechargeOrderPaySuccess;
  6. use App\Modules\Trade\Pay\YearOrderPaySuccess;
  7. use App\Modules\Book\Services\BookGiftsService;
  8. use App\Modules\Book\Services\ChapterService;
  9. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  10. use App\Modules\Subscribe\Services\OrderParamService;
  11. use App\Modules\Subscribe\Services\OrderParamWsoService;
  12. use App\Modules\Subscribe\Services\SubstituteOrderService;
  13. use App\Modules\User\Services\ReadRecordService;
  14. use App\Modules\User\Services\UserBindPhoneService;
  15. use App\Modules\User\Services\UserDivisionCpcPropertyService;
  16. use App\Modules\User\Services\UserGiftService;
  17. use Illuminate\Routing\Controller;
  18. use App\Libs\Pay\WechatPay;
  19. use Illuminate\Http\Request;
  20. use App\Modules\Subscribe\Services\YearOrderService;
  21. use App\Modules\Subscribe\Services\BookOrderService;
  22. use App\Modules\Subscribe\Services\OrderService;
  23. use App\Modules\Product\Services\ProductService;
  24. use App\Modules\Book\Services\BookConfigService;
  25. use App\Modules\User\Services\UserService;
  26. use App\Modules\Channel\Services\ChannelService;
  27. use Log;
  28. use DB;
  29. use Cookie;
  30. use Redis;
  31. use Hashids;
  32. use EasyWeChat\Foundation\Application;
  33. use EasyWeChat\Support\XML;
  34. use App\Modules\OfficialAccount\Services\ForceSubscribeService;
  35. use App\Jobs\SendTexts;
  36. use App\Jobs\SendStatisticsList;
  37. use App\Libs\AliSMS;
  38. use App\Libs\Pay\Merchants\UnionPay;
  39. use App\Modules\Activity\Services\ActivityService;
  40. use App\Modules\SendOrder\Services\SendOrderService;
  41. use App\Modules\Subscribe\Models\Order;
  42. use App\Modules\Trade\Models\PayMerchant;
  43. use GuzzleHttp\Client;
  44. class OrdersController extends Controller
  45. {
  46. /**
  47. * @apiDefine pay 支付
  48. */
  49. /**
  50. * 支付唤起入口
  51. * 功能:授权、请求支付参数、并跳转微信支付
  52. * TODO 暂时不创建订单
  53. * tail -f /var/www/ydy_wap_backend/storage/logs/laravel-2017-12-01.log
  54. * zwap/goToPay?distribution_channel_id=3&price=1&uid=3
  55. * @apiVersion 1.0.0
  56. * @apiDescription 支付
  57. * @api {get} http://pay.aizhuishu.com/goToPay 支付
  58. * @apiGroup pay
  59. * @apiName wxindex
  60. * @apiParam {Int} product_id product_id
  61. * @apiParam {Int} uid uid
  62. * @apiParam {Int} distribution_channel_id distribution_channel_id
  63. * @apiParam {String} pay_redirect_url pay_redirect_url
  64. * @apiSuccess {int} code 状态码
  65. * @apiSuccessExample {json} Success-Response:
  66. * HTTP/1.1 200 OK
  67. * {
  68. * code: 0,
  69. * msg: "",
  70. * data: {
  71. * }
  72. */
  73. function wxindex(Request $request)
  74. {
  75. $product_id = $request->has('product_id') ? $request->get('product_id') : '';
  76. $uid = $request->has('uid') ? $request->get('uid') : '';
  77. $distribution_channel_id = $request->has('distribution_channel_id') ? $request->get('distribution_channel_id') : '';
  78. $p_channel_id = $request->has('p_channel_id') ? $request->get('p_channel_id') : 0;
  79. $pay_redirect_url = $request->has('pay_redirect_url') ? $request->get('pay_redirect_url') : '';
  80. $send_order_id = $request->has('send_order_id') ? $request->get('send_order_id') : 0;
  81. $activity_id = $request->has('activity_id') ? $request->get('activity_id') : 0;
  82. $ip = $request->has('ip') ? $request->get('ip') : '';
  83. $n = $request->has('n') ? $request->get('n') : 0;
  84. $crm = $request->has('crm') ? $request->get('crm') : 0;
  85. if ($send_order_id && strlen($send_order_id) > 50) {
  86. try {
  87. $send_order_id = decrypt($send_order_id);
  88. } catch (\Exception $e) {
  89. $send_order_id = 0;
  90. }
  91. }
  92. if ($send_order_id && strlen($send_order_id) > 50) {
  93. $send_order_id = 0;
  94. }
  95. //活动判断
  96. if ($activity_id) {
  97. $limit = $request->get('limit', 0);
  98. if ($limit && $limit == 1) {
  99. $order = OrderService::userIsParticipateActivity($uid, $activity_id);
  100. if ($order) {
  101. $activity = ActivityService::getById($activity_id);
  102. if ($activity) {
  103. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com' . $activity->activity_page;
  104. return redirect()->to($url);
  105. }
  106. return '';
  107. }
  108. }
  109. }
  110. $suid = $request->get('suid', 0);
  111. if ($suid) {
  112. $user_info = UserService::getById($uid);
  113. $distribution_channel_id = $user_info->distribution_channel_id;
  114. $pay_redirect_url = 1;
  115. }
  116. if (empty($product_id) || empty($uid) || empty($distribution_channel_id) || empty($pay_redirect_url)) {
  117. return response()->error('WAP_PARAM_ERROR');
  118. }
  119. if ($suid && $suid == $uid) {
  120. return back();
  121. }
  122. $openid = $request->has('openid') ? $request->get('openid') : '';
  123. $bid = $request->has('bid') ? $request->get('bid') : 0;
  124. $hash_bid = $bid;
  125. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from', 'main');
  126. if ($fromtype && strlen($fromtype) > 50) {
  127. try {
  128. $fromtype = decrypt($fromtype);
  129. } catch (\Exception $e) {
  130. $fromtype = 'main';
  131. }
  132. }
  133. if ($fromtype && strlen($fromtype) > 50) {
  134. $fromtype = 'main';
  135. }
  136. if (!$openid && !$p_channel_id && !$suid) {
  137. $transfer_channel_id = $this->transfer($distribution_channel_id);
  138. if ($transfer_channel_id) {
  139. $p_channel_id = $distribution_channel_id;
  140. $distribution_channel_id = $transfer_channel_id;
  141. }
  142. }
  143. //根据分校id获取支付配置id
  144. Log::info($request->all());
  145. $channel = ChannelService::getById($distribution_channel_id);
  146. if (!$channel || !$channel->pay_merchant_id) {
  147. return response()->error('WAP_PARAM_ERROR');
  148. }
  149. //获取支付类型
  150. $pay_merchant = DB::table('pay_merchants')->select('id', 'appid', 'source', 'config_info')->where('id', $channel->pay_merchant_id)->where('is_enabled', 1)->first();
  151. if (!$pay_merchant || !$pay_merchant->appid || !$pay_merchant->source) return response()->error('WAP_PARAM_ERROR');
  152. if ($pay_merchant->source == 'PALMPAYV2') {
  153. return redirect()->to($this->paympay($request, $pay_merchant));
  154. } else if ($pay_merchant->source === 'UNIONPAY') {
  155. return redirect()->to($this->unionpay($request, $pay_merchant));
  156. }
  157. //重定向 获取用户信息的次数
  158. $n++;
  159. if ($n >= 5) {
  160. //重定向次数过多,则授权公众号出问题
  161. $this->payAlert($channel->pay_merchant_id, '', '', $n);
  162. $back_url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . '/pay';
  163. return redirect()->to($back_url);
  164. }
  165. if (empty($openid)) {
  166. $trade_no = date("YmdHis") . str_shuffle(hexdec(uniqid()));
  167. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
  168. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  169. } else {
  170. $ip = get_client_ip();
  171. }
  172. $params = compact('uid', 'product_id', 'distribution_channel_id', 'send_order_id', 'bid', 'trade_no', 'pay_redirect_url', 'fromtype', 'activity_id', 'n', 'suid', 'ip', 'p_channel_id', 'crm');
  173. $redirect_url = env('CREATE_PAY_URL') . '?' . http_build_query($params);
  174. //$redirect_url = env('CREATE_PAY_URL').'?uid='.$uid.'&product_id='.$product_id.'&distribution_channel_id='
  175. // .$distribution_channel_id.'&send_order_id='.$send_order_id.'&bid='.$bid.'&pay_redirect_url='.urlencode($pay_redirect_url)
  176. $auth_url = env('AUTH_URL') . '?gzh_app_id=' . $pay_merchant->appid . '&redirect_url=' . urlencode($redirect_url);
  177. Log::info('redirect_auth_url:' . $auth_url);
  178. header("Location:" . $auth_url);
  179. exit();
  180. }
  181. $trade_no = $request->input('trade_no');
  182. $order_info = OrderService::getByTradeNo($trade_no);
  183. if ($order_info) return response()->error('WAP_SYS_ERROR');
  184. $cid = $request->has('cid') ? $request->get('cid') : '';
  185. $product_info = ProductService::getProductSingle($product_id, false);
  186. //新用户只能冲一次
  187. //Log::info('新用户只能冲一次:' .$product_info->type);
  188. if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
  189. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  190. return redirect()->to($url);
  191. }
  192. $price = $product_info->price * 100;
  193. if ($uid < 32) {
  194. $price = 1;
  195. }
  196. try {
  197. $send_order_id = (int) Redis::hget('book_read:' . $uid, 'send_order_id');
  198. } catch (\Exception $e) { }
  199. $this->updateUserSendOrderId($uid, $send_order_id);
  200. if (in_array($uid, explode(',', env('TEST_UID')))) {
  201. $price = 1;
  202. }
  203. if ($bid) {
  204. try {
  205. $bid = Hashids::decode($bid)[0];
  206. } catch (\Exception $e) {
  207. $bid = 0;
  208. }
  209. }
  210. try {
  211. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  212. Redis::hset($key, $uid, time());
  213. $date = date('Y-m-d');
  214. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  215. if ($send_order_id) {
  216. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  217. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  218. }
  219. } catch (\Exception $e) { }
  220. $data = [];
  221. $data['price'] = $price;
  222. $data['create_ip'] = $ip;
  223. $data['openid'] = $openid; //
  224. $data['body'] = 'novel read';
  225. $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
  226. if ($official_name) {
  227. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  228. }
  229. $data['detail'] = 'novel read';
  230. $data['trade_no'] = $trade_no;
  231. $data['remark'] = json_encode(
  232. [
  233. 'uid' => $uid,
  234. 'dcd' => $distribution_channel_id,
  235. 'bid' => $bid,
  236. 'cp' => $ip,
  237. 'pms' => $pay_merchant->source,
  238. 'pmi' => $channel->pay_merchant_id,
  239. 'pd' => $product_id,
  240. 'soi' => $send_order_id,
  241. ]
  242. );
  243. if ($product_info->type == 'YEAR_ORDER') {
  244. $order_type = 'YEAR';
  245. } elseif ($product_info->type == 'BOOK_ORDER') {
  246. $order_type = 'BOOK';
  247. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  248. $order_type = 'RECHARGE';
  249. } elseif ($product_info->type == 'NEW_USER') {
  250. $order_type = 'RECHARGE';
  251. } else {
  252. $order_type = 'UNKNOWN';
  253. }
  254. /*
  255. $this->createOrderTotal([
  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. 'order_type'=>$order_type,
  267. 'from_bid'=>$bid
  268. ]);*/
  269. $send_order_name = '';
  270. if ($send_order_id) {
  271. $send_order_info = SendOrderService::getById($send_order_id);
  272. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  273. $send_order_name = $send_order_info->name;
  274. }
  275. if (redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0) == $distribution_channel_id) {
  276. $send_order_id = -$send_order_id;
  277. }
  278. }
  279. if ($suid) {
  280. $user_info = UserService::getById($uid);
  281. $distribution_channel_id = $user_info->distribution_channel_id;
  282. }
  283. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  284. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  285. $init_order = [
  286. 'distribution_channel_id' => $distribution_channel_id,
  287. 'uid' => $uid,
  288. 'product_id' => $product_id,
  289. 'price' => $price / 100,
  290. 'pay_type' => 1,
  291. 'trade_no' => $data['trade_no'],
  292. 'pay_merchant_source' => $pay_merchant->source,
  293. 'pay_merchant_id' => $channel->pay_merchant_id,
  294. 'create_ip' => $ip,
  295. 'send_order_id' => $send_order_id,
  296. 'send_order_name' => $send_order_name,
  297. 'order_type' => $order_type,
  298. 'from_bid' => $from_bid,
  299. 'from_type' => $fromtype,
  300. 'activity_id' => $activity_id,
  301. 'inner_send_order_id' => $inner_send_order_id
  302. ];
  303. $result = $this->createUnPayOrder($init_order);
  304. //订单创建成功后增加统计
  305. if ($result) {
  306. $this->orderCreated($init_order);
  307. $this->recordOtherParam($result->id, $uid);
  308. if ($p_channel_id) {
  309. //miwa 转换
  310. OrderParamService::createByParam([
  311. 'order_id' => $result->id, 'gxhp' => 'mw_transfer', 'appid' => '', 'bid' => $bid, 'data_hour_key' => '', 'times' => '', 'distribution_channel_id' => $p_channel_id
  312. ]);
  313. }
  314. if ($crm) {
  315. OrderParamService::createByParam([
  316. 'order_id' => $result->id, 'gxhp' => 'crm', 'appid' => '', 'bid' => $bid, 'data_hour_key' => $crm, 'times' => '', 'distribution_channel_id' => $distribution_channel_id
  317. ]);
  318. }
  319. $this->orderParamWso($uid, $result->id);
  320. }
  321. if ($request->get('suid')) {
  322. $this->createSubstituteOrder($result->id, $uid, $request->get('suid'));
  323. }
  324. Log::info($data);
  325. $config = [];
  326. if ($pay_merchant->config_info) {
  327. $config = json_decode($pay_merchant->config_info, true);
  328. }
  329. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  330. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  331. $pay_info = $wechatPay->send($data);
  332. if (!$pay_info) {
  333. $pay_info = $wechatPay->send($data);
  334. }
  335. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  336. //支付异常
  337. myLog('order')->info('pay error start---------------------------------');
  338. myLog('order')->info($pay_info);
  339. myLog('order')->info($request->all());
  340. myLog('order')->info('order info is');
  341. myLog('order')->info($init_order);
  342. myLog('order')->info('pay error end-----------------------------------');
  343. $this->payAlert($channel->pay_merchant_id, $trade_no, $pay_info);
  344. }
  345. $pay_info['pay_redirect_url'] = urldecode($pay_redirect_url);
  346. if ($request->has('cid')) {
  347. $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&cid=' . $request->input('cid');
  348. }
  349. // if ($hash_bid) {
  350. // $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&bid=' . $hash_bid;
  351. // }
  352. if ($suid) {
  353. $prize_fee = (int) (($product_info->price * 100) * 0.1);
  354. $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  355. $help_pay_page_channel_id = $distribution_channel_id;
  356. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  357. $pay_info['pay_redirect_url'] = $pay_redirect_url = sprintf(
  358. $url_format,
  359. env('PROTOCOL'),
  360. encodeDistributionChannelId($help_pay_page_channel_id),
  361. env('CUSTOM_HOST'),
  362. $prize_fee,
  363. $uid
  364. );
  365. }
  366. $pay_url_info = parse_url($pay_redirect_url);
  367. $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']);
  368. //$h5_scheme = env('H5_SCHEME','https');
  369. //$jsSdkSign = $this->jsSdkSign($pay_info,str_replace('http',$h5_scheme,url()->current()));
  370. Log::info('$pay_info is');
  371. $jsSdkSign = 0;
  372. Log::info($pay_info);
  373. Log::info('jsSdkSign---- :' . $jsSdkSign);
  374. $pay_order = $trade_no;
  375. // 注册动作-》创建订单
  376. $action_type = 'CreateOrder';
  377. $param = [
  378. 'openid' => isset($openid) ? $openid : '0',
  379. 'uid' => isset($uid) ? $uid : '0',
  380. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  381. 'amount' => isset($price) ? $price : '0',
  382. ];
  383. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  384. return view('pay.order.index', compact('pay_info', 'referer', 'jsSdkSign', 'pay_order'));
  385. }
  386. private function paympay(Request $request, $pay_merchant)
  387. {
  388. $product_id = $request->get('product_id');
  389. $uid = $request->get('uid');
  390. $distribution_channel_id = $request->get('distribution_channel_id');
  391. $pay_redirect_url = $request->get('pay_redirect_url');
  392. $send_order_id = $request->get('send_order_id', 0);
  393. $activity_id = $request->get('activity_id', 0);
  394. $suid = $request->get('suid', 0);
  395. $bid = $request->has('bid') ? $request->get('bid') : 0;
  396. $crm = $request->has('crm') ? $request->get('crm') : '';
  397. $hash_bid = $bid;
  398. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from', 'main');
  399. //根据分校id获取支付配置id
  400. Log::info($request->all());
  401. $trade_no = $trade_no = date("YmdHis") . str_shuffle(hexdec(uniqid()));
  402. $order_info = OrderService::getByTradeNo($trade_no);
  403. if ($order_info) return response()->error('WAP_SYS_ERROR');
  404. $cid = $request->has('cid') ? $request->get('cid') : '';
  405. $product_info = ProductService::getProductSingle($product_id, false);
  406. //新用户只能冲一次
  407. //Log::info('新用户只能冲一次:' .$product_info->type);
  408. if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
  409. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  410. return redirect()->to($url);
  411. }
  412. $price = $product_info->price * 100;
  413. if ($uid < 32) {
  414. $price = 1;
  415. }
  416. if (!$send_order_id) {
  417. try {
  418. $send_order_id = (int) Redis::hget('book_read:' . $uid, 'send_order_id');
  419. } catch (\Exception $e) { }
  420. }
  421. $this->updateUserSendOrderId($uid, $send_order_id);
  422. if (in_array($uid, explode(',', env('TEST_UID')))) {
  423. $price = 1;
  424. }
  425. try {
  426. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  427. Redis::hset($key, $uid, time());
  428. $date = date('Y-m-d');
  429. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  430. if ($send_order_id) {
  431. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  432. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  433. }
  434. } catch (\Exception $e) { }
  435. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
  436. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  437. } else {
  438. $ip = get_client_ip();
  439. }
  440. $data = [];
  441. $data['price'] = $price;
  442. $data['create_ip'] = $ip;
  443. $data['body'] = 'novel read';
  444. $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
  445. if ($official_name) {
  446. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  447. }
  448. $data['detail'] = 'novel read';
  449. $data['trade_no'] = $trade_no;
  450. if ($product_info->type == 'YEAR_ORDER') {
  451. $order_type = 'YEAR';
  452. } elseif ($product_info->type == 'BOOK_ORDER') {
  453. $order_type = 'BOOK';
  454. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  455. $order_type = 'RECHARGE';
  456. } elseif ($product_info->type == 'NEW_USER') {
  457. $order_type = 'RECHARGE';
  458. } else {
  459. $order_type = 'UNKNOWN';
  460. }
  461. $send_order_name = '';
  462. if ($send_order_id) {
  463. $send_order_info = SendOrderService::getById($send_order_id);
  464. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  465. $send_order_name = $send_order_info->name;
  466. }
  467. }
  468. if ($suid) {
  469. $user_info = UserService::getById($uid);
  470. $distribution_channel_id = $user_info->distribution_channel_id;
  471. }
  472. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  473. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  474. $init_order = [
  475. 'distribution_channel_id' => $distribution_channel_id,
  476. 'uid' => $uid,
  477. 'product_id' => $product_id,
  478. 'price' => $price / 100,
  479. 'pay_type' => 1,
  480. 'trade_no' => $data['trade_no'],
  481. 'pay_merchant_source' => 'PALMPAY',
  482. 'pay_merchant_id' => $pay_merchant->id,
  483. 'create_ip' => $ip,
  484. 'send_order_id' => $send_order_id,
  485. 'send_order_name' => $send_order_name,
  486. 'order_type' => $order_type,
  487. 'from_bid' => $from_bid,
  488. 'from_type' => $fromtype,
  489. 'activity_id' => $activity_id,
  490. 'inner_send_order_id' => $inner_send_order_id
  491. ];
  492. $result = $this->createUnPayOrder($init_order);
  493. //订单创建成功后增加统计
  494. if ($result) {
  495. $this->orderCreated($init_order);
  496. $this->recordOtherParam($result->id, $uid);
  497. if ($crm) {
  498. OrderParamService::createByParam([
  499. 'order_id' => $result->id, 'gxhp' => 'crm', 'appid' => '', 'bid' => $bid, 'data_hour_key' => $crm, 'times' => '', 'distribution_channel_id' => $distribution_channel_id
  500. ]);
  501. }
  502. $this->orderParamWso($uid, $result->id);
  503. }
  504. if ($suid) {
  505. $this->createSubstituteOrder($result->id, $uid, $suid);
  506. }
  507. if ($suid) {
  508. $prize_fee = (int) (($product_info->price * 100) * 0.1);
  509. //$help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  510. $help_pay_page_channel_id = $distribution_channel_id;
  511. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  512. $pay_redirect_url = sprintf(
  513. $url_format,
  514. env('PROTOCOL'),
  515. encodeDistributionChannelId($help_pay_page_channel_id),
  516. env('CUSTOM_HOST'),
  517. $prize_fee,
  518. $uid
  519. );
  520. }
  521. $de_pay_redirect_url = urldecode($pay_redirect_url);
  522. /*if ($request->has('cid')) {
  523. $de_pay_redirect_url = $de_pay_redirect_url . '&cid=' . $request->get('cid');
  524. }*/
  525. $pay_url_info = parse_url($de_pay_redirect_url);
  526. $data['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/waitv2?order=' . $data['trade_no'] . '&redirect=' . urlencode($de_pay_redirect_url);
  527. Log::info($data);
  528. $config = [];
  529. if ($pay_merchant->config_info) {
  530. $config = json_decode($pay_merchant->config_info, true);
  531. }
  532. $wechatPay = WechatPay::instance($pay_merchant->source, $config);
  533. \Log::info('$wechatPay is');
  534. //\Log::info($wechatPay);
  535. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  536. \Log::info('$wechatPay ok-------------------');
  537. $pay_info = $wechatPay->send($data);
  538. \Log::info('send ok-------------------');
  539. // 注册动作-》创建订单
  540. $action_type = 'CreateOrder';
  541. $param = [
  542. 'openid' => isset($openid) ? $openid : '0',
  543. 'uid' => isset($uid) ? $uid : '0',
  544. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  545. 'amount' => isset($price) ? $price : '0',
  546. ];
  547. \Log::info('FUCK_PALMPAYV2');
  548. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  549. return $pay_info;
  550. }
  551. /**
  552. * shit code
  553. */
  554. private function unionpay(Request $request, $pay_merchant)
  555. {
  556. $product_id = $request->get('product_id');
  557. $uid = $request->get('uid');
  558. $distribution_channel_id = $request->get('distribution_channel_id');
  559. $pay_redirect_url = $request->get('pay_redirect_url');
  560. $send_order_id = $request->get('send_order_id', 0);
  561. $activity_id = $request->get('activity_id', 0);
  562. $suid = $request->get('suid', 0);
  563. $bid = $request->has('bid') ? $request->get('bid') : 0;
  564. $crm = $request->has('crm') ? $request->get('crm') : '';
  565. $hash_bid = $bid;
  566. $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from', 'main');
  567. //根据分校id获取支付配置id
  568. Log::info($request->all());
  569. $config = [];
  570. if ($pay_merchant->config_info) {
  571. $config = json_decode($pay_merchant->config_info, true);
  572. }
  573. $wechatPay = WechatPay::Union($pay_merchant->source, $config);
  574. if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
  575. $trade_no = $wechatPay->generateTradeNo();
  576. $order_info = OrderService::getByTradeNo($trade_no);
  577. if ($order_info) return response()->error('WAP_SYS_ERROR');
  578. $cid = $request->has('cid') ? $request->get('cid') : '';
  579. $product_info = ProductService::getProductSingle($product_id, false);
  580. //新用户只能冲一次
  581. //Log::info('新用户只能冲一次:' .$product_info->type);
  582. if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
  583. $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
  584. return redirect()->to($url);
  585. }
  586. $price = $product_info->price * 100;
  587. if ($uid < 32) {
  588. $price = 1;
  589. }
  590. if (!$send_order_id) {
  591. try {
  592. $send_order_id = (int) Redis::hget('book_read:' . $uid, 'send_order_id');
  593. } catch (\Exception $e) { }
  594. }
  595. $this->updateUserSendOrderId($uid, $send_order_id);
  596. if (in_array($uid, explode(',', env('TEST_UID')))) {
  597. $price = 1;
  598. }
  599. try {
  600. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  601. Redis::hset($key, $uid, time());
  602. $date = date('Y-m-d');
  603. Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
  604. if ($send_order_id) {
  605. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
  606. Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
  607. }
  608. } catch (\Exception $e) { }
  609. if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
  610. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  611. } else {
  612. $ip = get_client_ip();
  613. }
  614. $data = [];
  615. $data['price'] = $price;
  616. $data['create_ip'] = $ip;
  617. $data['body'] = 'novel read';
  618. $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
  619. if ($official_name) {
  620. $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
  621. }
  622. $data['detail'] = 'novel read';
  623. $data['trade_no'] = $trade_no;
  624. if ($product_info->type == 'YEAR_ORDER') {
  625. $order_type = 'YEAR';
  626. } elseif ($product_info->type == 'BOOK_ORDER') {
  627. $order_type = 'BOOK';
  628. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  629. $order_type = 'RECHARGE';
  630. } elseif ($product_info->type == 'NEW_USER') {
  631. $order_type = 'RECHARGE';
  632. } else {
  633. $order_type = 'UNKNOWN';
  634. }
  635. $send_order_name = '';
  636. if ($send_order_id) {
  637. $send_order_info = SendOrderService::getById($send_order_id);
  638. if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
  639. $send_order_name = $send_order_info->name;
  640. }
  641. }
  642. if ($suid) {
  643. $user_info = UserService::getById($uid);
  644. $distribution_channel_id = $user_info->distribution_channel_id;
  645. }
  646. $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
  647. $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
  648. $init_order = [
  649. 'distribution_channel_id' => $distribution_channel_id,
  650. 'uid' => $uid,
  651. 'product_id' => $product_id,
  652. 'price' => $price / 100,
  653. 'pay_type' => 1,
  654. 'trade_no' => $data['trade_no'],
  655. 'pay_merchant_source' => $pay_merchant->source,
  656. 'pay_merchant_id' => $pay_merchant->id,
  657. 'create_ip' => $ip,
  658. 'send_order_id' => $send_order_id,
  659. 'send_order_name' => $send_order_name,
  660. 'order_type' => $order_type,
  661. 'from_bid' => $from_bid,
  662. 'from_type' => $fromtype,
  663. 'activity_id' => $activity_id,
  664. 'inner_send_order_id' => $inner_send_order_id
  665. ];
  666. $result = $this->createUnPayOrder($init_order);
  667. //订单创建成功后增加统计
  668. if ($result) {
  669. $this->orderCreated($init_order);
  670. $this->recordOtherParam($result->id, $uid);
  671. if ($crm) {
  672. OrderParamService::createByParam([
  673. 'order_id' => $result->id, 'gxhp' => 'crm', 'appid' => '', 'bid' => $bid, 'data_hour_key' => $crm, 'times' => '', 'distribution_channel_id' => $distribution_channel_id
  674. ]);
  675. }
  676. $this->orderParamWso($uid, $result->id);
  677. }
  678. if ($suid) {
  679. $this->createSubstituteOrder($result->id, $uid, $suid);
  680. }
  681. if ($suid) {
  682. $prize_fee = (int) (($product_info->price * 100) * 0.1);
  683. //$help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
  684. $help_pay_page_channel_id = $distribution_channel_id;
  685. $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
  686. $pay_redirect_url = sprintf(
  687. $url_format,
  688. env('PROTOCOL'),
  689. encodeDistributionChannelId($help_pay_page_channel_id),
  690. env('CUSTOM_HOST'),
  691. $prize_fee,
  692. $uid
  693. );
  694. }
  695. $de_pay_redirect_url = urldecode($pay_redirect_url);
  696. $pay_url_info = parse_url($de_pay_redirect_url);
  697. $data['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/wait_union?order=' . $data['trade_no'] . '&redirect=' . urlencode($de_pay_redirect_url);
  698. Log::info($data);
  699. $pay_info = $wechatPay->send($data);
  700. // 注册动作-》创建订单
  701. $action_type = 'CreateOrder';
  702. $param = [
  703. 'openid' => isset($openid) ? $openid : '0',
  704. 'uid' => isset($uid) ? $uid : '0',
  705. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  706. 'amount' => isset($price) ? $price : '0',
  707. ];
  708. \Log::info($pay_info);
  709. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  710. return $pay_info;
  711. }
  712. public function waitPageV2(Request $request)
  713. {
  714. $order = $request->get('order');
  715. $order = (string) $order;
  716. $url = urldecode($request->get('redirect'));
  717. if (substr_count($url, '?') > 1) {
  718. $url = str_replace_last('?', '&', $url);
  719. }
  720. $from = 'pay';
  721. $order_info = OrderService::getByTradeNo($order);
  722. $uid = $order_info->uid;
  723. //$bind_info = UserBindPhoneService::bindInfo($uid);
  724. $bind_info = true;
  725. $page = 'pay.order.wait';
  726. if (!$bind_info) {
  727. //$page = 'pay.order.bindPhone';
  728. }
  729. $url_info = parse_url($url);
  730. if (isset($url_info['query']) && !empty($url_info['query'])) {
  731. parse_str($url_info['query'], $path);
  732. if (isset($path['success']) && $path['success'] == 0) {
  733. if (isset($url_info['path']) && str_contains($url_info['path'], 'reader') && isset($path['cid']) && !empty($path['cid'])) {
  734. $chapter_info = ChapterService::getChapterNameByIdNoCheck($path['cid']);
  735. $path['cid'] = $chapter_info->prev_cid;
  736. $url = sprintf('%s://%s/reader?%s', $url_info['scheme'], $url_info['host'], http_build_query($path));
  737. }
  738. if (!$bind_info) {
  739. return view('pay.order.bindPhone', compact('order', 'url', 'from'));
  740. } else {
  741. return redirect($url);
  742. }
  743. }
  744. }
  745. if (isset($url_info['path']) && str_contains($url_info['path'], 'pay')) {
  746. $url = sprintf('%s://%s/recent', $url_info['scheme'], $url_info['host']);
  747. //return redirect($url);
  748. if (!$bind_info) {
  749. return view('pay.order.bindPhone', compact('order', 'url', 'from'));
  750. } else {
  751. return redirect($url);
  752. }
  753. }
  754. return view($page, compact('order', 'url', 'from'));
  755. }
  756. private function isNewUserSecondCharge($charge_type, $product_id, $uid)
  757. {
  758. //Log::info('新用户只能冲一次:' .$charge_type);
  759. if ($charge_type == 'NEW_USER') {
  760. $result = OrderService::getUserOrderByProductId($uid, $product_id);
  761. //Log::info('isNewUserSecondCharge-------------------------');
  762. //Log::info($result);
  763. if ($result) {
  764. return true;
  765. }
  766. }
  767. return false;
  768. }
  769. //订单创建后统计
  770. private function orderCreated($init_order)
  771. {
  772. try {
  773. if (isset($init_order['pay_merchant_id'])) {
  774. $key = 'pay_merchant:' . $init_order['pay_merchant_id'];
  775. Redis::hincrby($key, 'unpaid_num', 1);
  776. Redis::hset($key, 'last_create_time', time());
  777. }
  778. } catch (\Exception $e) { }
  779. }
  780. //订单回调后统计
  781. private function orderPaid($init_order)
  782. {
  783. try {
  784. if (isset($init_order['pay_merchant_id'])) {
  785. $key = 'pay_merchant:' . $init_order['pay_merchant_id'];
  786. Redis::hset($key, 'unpaid_num', 0);
  787. }
  788. } catch (\Exception $e) { }
  789. }
  790. public function reportError(Request $request)
  791. {
  792. $data = $request->post('data');
  793. DB::table('pay_page_error')->insert([
  794. 'msg' => $data,
  795. 'created_at' => date('Y-m-d H:i:s'),
  796. 'updated_at' => date('Y-m-d H:i:s')
  797. ]);
  798. return response()->success();
  799. }
  800. private function jsSdkSign($pay_info, $url)
  801. {
  802. if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
  803. return 0;
  804. }
  805. $appid = $pay_info['appId'];
  806. $jsapi_ticket = Redis::hget($appid, 'jsapi_ticket');
  807. //如果能获取到jsapi_ticket 则直接签名
  808. if ($jsapi_ticket) {
  809. $sign_arr = [
  810. 'noncestr' => $pay_info['nonceStr'],
  811. 'jsapi_ticket' => $jsapi_ticket,
  812. 'timestamp' => $pay_info['timeStamp'],
  813. 'url' => $url
  814. ];
  815. return sha1($this->arr_to_url($sign_arr));
  816. }
  817. //获取不到jsapi_ticket
  818. $app_secret_info = DB::table('official_setting')->select('secret')->first();
  819. if ($app_secret_info && !empty($app_secret_info->secret)) {
  820. $client = new Client(['timeout' => 5]);
  821. try {
  822. //获取access_token
  823. $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $app_secret_info->secret;
  824. $res = $client->request('get', $url)->getBody()->getContents();
  825. if (!$res) {
  826. return 0;
  827. }
  828. $res = json_decode($res, 1);
  829. if (!isset($res['access_token']) || empty($res['access_token'])) {
  830. return 0;
  831. }
  832. Redis::hset($appid, 'access_token', $res['access_token']);
  833. //获取jsapi_ticket
  834. $res = null;
  835. $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $res['access_token'] . '&type=jsapi';
  836. $res = $client->request('get', $url)->getBody()->getContents();
  837. if (!$res) {
  838. return 0;
  839. }
  840. $res = json_decode($res, 1);
  841. if (!isset($res['ticket']) || empty($res['ticket'])) {
  842. return 0;
  843. }
  844. Redis::hset($appid, 'jsapi_ticket', $res['ticket']);
  845. Redis::EXPIRE($appid, 7000);
  846. $sign_arr = [
  847. 'noncestr' => $pay_info['nonceStr'],
  848. 'jsapi_ticket' => $res['ticket'],
  849. 'timestamp' => $pay_info['timeStamp'],
  850. 'url' => $url
  851. ];
  852. return sha1($this->arr_to_url($sign_arr));
  853. } catch (\Exception $e) {
  854. return 0;
  855. }
  856. }
  857. return 0;
  858. }
  859. public function waitPage(Request $request)
  860. {
  861. $order = $request->input('order');
  862. $from = 'pay';
  863. //$order = "201712081711401585932843356442";
  864. $order = (string) $order;
  865. $order_info = OrderService::getByTradeNo($order);
  866. $uid = $order_info->uid;
  867. $bind_info = UserBindPhoneService::bindInfo($uid);
  868. //$bind_info = false;
  869. $page = 'pay.order.wait';
  870. if (!$bind_info) {
  871. $page = 'pay.order.bindPhone';
  872. }
  873. $url = urldecode($request->input('redirect'));
  874. return view($page, compact('order', 'url', 'from'));
  875. //return view('pay.order.wait');
  876. }
  877. public function waitPageUnion(Request $request)
  878. {
  879. $url = urldecode($request->get('redirect', ''));
  880. Log::info($request->except('_url'));
  881. if ($url) {
  882. $status = $request->get('status', '');
  883. $order = $request->get('merOrderId', '');
  884. $order_info = OrderService::getByTradeNo($order);
  885. $uid = $order_info->uid;
  886. if ($status && $status === 'TRADE_SUCCESS') {
  887. if ($order) {
  888. $from = 'pay';
  889. $order = (string) $order;
  890. $bind_info = UserBindPhoneService::bindInfo($uid);
  891. $page = 'pay.order.wait';
  892. if (!$bind_info) {
  893. $page = 'pay.order.bindPhone';
  894. }
  895. $url = urldecode($request->input('redirect'));
  896. return view($page, compact('order', 'url', 'from'));
  897. }
  898. } else {
  899. $url = urldecode($request->input('redirect'));
  900. if (strpos($url, '/reader') !== false) {
  901. preg_match('/bid=(\w+)/', $url, $matchs);
  902. $bid = Hashids::decode($matchs[1])[0];
  903. Log::info('redirect_url :' . $bid);
  904. $record = ReadRecordService::getRecordByUidBid($uid, $bid);
  905. if ($record) {
  906. $cid = explode('_', $record)[0];
  907. $url = preg_replace('/cid=(\d+)/', 'cid=' . $cid, $url);
  908. } else {
  909. $url = preg_replace(['/bid=(\w+)/', '/\/reader\?/'], ['', '/catalog?id=' . $bid . '&'], $url);
  910. }
  911. Log::info('reader_url :' . $url);
  912. }
  913. return redirect($url);
  914. }
  915. }
  916. }
  917. /**
  918. * 官方微信回调
  919. * @param Request $request
  920. * @return \Symfony\Component\HttpFoundation\Response
  921. */
  922. function wcback_official(Request $request)
  923. {
  924. libxml_disable_entity_loader(true);
  925. $xml = XML::parse(strval($request->getContent()));
  926. Log::info('xml is');
  927. Log::info($xml);
  928. if (!$xml || !is_array($xml)) return 'fail';
  929. $remarks = json_decode($xml['attach'], true);
  930. $pay_merchant_id = $remarks['pmi'];
  931. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  932. $application = WechatPay::Official($pay_merchant->source, json_decode($pay_merchant->config_info, true));
  933. //$app = new Application($options);
  934. $app = $application->app;
  935. $response = $app->payment->handleNotify(function ($notify, $successful) {
  936. if (!$successful) return 'fail';
  937. $trade_no = $notify->out_trade_no;
  938. $order = OrderService::getByTradeNo($trade_no);
  939. if (isset($order->status) && $order->status == 'PAID') {
  940. Log::info('has_pay:' . $trade_no);
  941. return true;
  942. }
  943. DB::beginTransaction();
  944. try {
  945. $transaction_id = $notify->transaction_id;
  946. $remarks = json_decode($notify->attach, true);
  947. $uid = $remarks['uid'];
  948. $distribution_channel_id = $remarks['dcd'];
  949. $product_id = $remarks['pd'];
  950. $product = ProductService::getProductSingle($product_id);
  951. $pay_merchant_source = $remarks['pms'];
  952. $pay_merchant_id = $remarks['pmi'];
  953. $send_order_id = $remarks['soi'];
  954. $create_ip = $remarks['cp'];
  955. $price = $product->price;
  956. $bid = $remarks['bid'];
  957. //$this->updateOrderTotal($trade_no,$transaction_id);
  958. $prize_fee = (int) (($product->price * 100) * 0.1);
  959. $this->substituteOrderPrice($order->id, $prize_fee);
  960. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  961. //获取用户充值次数
  962. $order->pay_type = $this->getChargeTimes($order->uid);
  963. // 更新其他定制Order表
  964. if ($product->type == 'YEAR_ORDER') {
  965. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  966. $order_type = 'YEAR';
  967. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  968. $order->order_type = $order_type;
  969. $order->status = 'PAID';
  970. $order->pay_end_at = date('Y-m-d H:i:s');
  971. $order->transaction_id = $transaction_id;
  972. $order->save();
  973. //$price = $price/100;
  974. /*
  975. $this->createOrder(
  976. compact(
  977. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  978. 'pay_merchant_id','create_ip','bid','transaction_id'
  979. )
  980. );*/
  981. } elseif ($product->type == 'BOOK_ORDER') {
  982. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  983. $order_type = 'BOOK';
  984. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  985. $order->order_type = $order_type;
  986. $order->status = 'PAID';
  987. $order->pay_end_at = date('Y-m-d H:i:s');
  988. $order->transaction_id = $transaction_id;
  989. $order->save();
  990. //$price = $price/100;
  991. /*
  992. $this->createOrder(
  993. compact(
  994. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  995. 'pay_merchant_id','create_ip','bid','transaction_id'
  996. )
  997. );*/
  998. } elseif ($product->type == 'TICKET_RECHARGE') {
  999. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1000. $order_type = 'RECHARGE';
  1001. $this->userCharge($product, $uid);
  1002. $order->order_type = $order_type;
  1003. $order->status = 'PAID';
  1004. $order->pay_end_at = date('Y-m-d H:i:s');
  1005. $order->transaction_id = $transaction_id;
  1006. $order->save();
  1007. //$price = $price/100;
  1008. /*
  1009. $this->createOrder(
  1010. compact(
  1011. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  1012. 'pay_merchant_id','create_ip','bid','transaction_id'
  1013. ));*/
  1014. } elseif ($product->type == 'FOREVER') {
  1015. Log::info('FOREVER------------------------FOREVER');
  1016. $order_type = 'FOREVER';
  1017. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1018. $order->order_type = $order_type;
  1019. $order->status = 'PAID';
  1020. $order->pay_end_at = date('Y-m-d H:i:s');
  1021. $order->transaction_id = $transaction_id;
  1022. $order->save();
  1023. } else {
  1024. DB::rollback();
  1025. return 'Order not exist.';
  1026. }
  1027. $this->successPayPushMsg($uid, $product, $order->id);
  1028. $this->orderStatistical($order);
  1029. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1030. Redis::hdel($key, $uid);
  1031. DB::commit();
  1032. //$this->huaweiActivity($order->activity_id,$uid,$product_id);
  1033. // 注册动作-》回调订单
  1034. $action_type = 'CallBackOrder';
  1035. $param = [
  1036. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1037. 'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
  1038. ];
  1039. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1040. //用户属性
  1041. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1042. //交换订单时间
  1043. $this->exchangeUpdateAndCreated($order->id);
  1044. return true;
  1045. } catch (\Exception $e) {
  1046. DB::rollback();
  1047. return 'fail';
  1048. }
  1049. });
  1050. return $response;
  1051. }
  1052. /**
  1053. * 银联支付回调
  1054. */
  1055. function wcback_union(Request $request)
  1056. {
  1057. $data = $request->except('_url');
  1058. \Log::info('union_pay.notify: ');
  1059. \Log::info($data);
  1060. try {
  1061. if (isset($data['merOrderId'])) {
  1062. $trade_no = $data['merOrderId'];
  1063. if ($trade_no) {
  1064. $order = OrderService::getByTradeNo($trade_no);
  1065. $transaction_id = $data['targetOrderId'];
  1066. if ($order) {
  1067. if (isset($order->status) && $order->status == 'PAID') {
  1068. Log::info('has_pay:' . $order->trade_no);
  1069. return 'SUCCESS';
  1070. }
  1071. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')
  1072. ->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  1073. $instance = WechatPay::Union($pay_merchant->source, json_decode($pay_merchant->config_info, true));
  1074. if ($instance->notify($data)) {
  1075. // 修改表比较多,开启事务
  1076. DB::beginTransaction();
  1077. $uid = $order->uid;
  1078. $distribution_channel_id = $order->distribution_channel_id;
  1079. $product_id = $order->product_id;
  1080. $product = ProductService::getProductSingle($product_id);
  1081. $send_order_id = $order->send_order_id;
  1082. $price = $product->price;
  1083. Log::info('save_order_end');
  1084. Log::info($product);
  1085. Log::info('product_type:' . $product->type);
  1086. $prize_fee = (int) (($product->price * 100) * 0.1);
  1087. $this->substituteOrderPrice($order->id, $prize_fee);
  1088. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1089. //获取用户充值次数
  1090. $order->pay_type = $this->getChargeTimes($order->uid);
  1091. // 更新其他定制Order表
  1092. if ($product->type == 'YEAR_ORDER') {
  1093. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1094. $order_type = 'YEAR';
  1095. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1096. $order->order_type = $order_type;
  1097. $order->status = 'PAID';
  1098. $order->pay_end_at = date('Y-m-d H:i:s');
  1099. $order->transaction_id = $transaction_id;
  1100. $order->save();
  1101. } elseif ($product->type == 'BOOK_ORDER') {
  1102. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1103. $order_type = 'BOOK';
  1104. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1105. $order->order_type = $order_type;
  1106. $order->status = 'PAID';
  1107. $order->pay_end_at = date('Y-m-d H:i:s');
  1108. $order->transaction_id = $transaction_id;
  1109. $order->save();
  1110. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1111. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1112. $order_type = 'RECHARGE';
  1113. $this->userCharge($product, $uid);
  1114. $order->order_type = $order_type;
  1115. $order->status = 'PAID';
  1116. $order->pay_end_at = date('Y-m-d H:i:s');
  1117. $order->transaction_id = $transaction_id;
  1118. $order->save();
  1119. } elseif ($product->type == 'FOREVER') {
  1120. Log::info('FOREVER------------------------FOREVER');
  1121. $order_type = 'FOREVER';
  1122. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1123. $order->order_type = $order_type;
  1124. $order->status = 'PAID';
  1125. $order->pay_end_at = date('Y-m-d H:i:s');
  1126. $order->transaction_id = $transaction_id;
  1127. $order->save();
  1128. }
  1129. $this->successPayPushMsg($uid, $product, $order->id);
  1130. $this->orderStatistical($order);
  1131. DB::commit();
  1132. //redis 删除未支付的uid
  1133. try {
  1134. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1135. Redis::hdel($key, $uid);
  1136. } catch (\Exception $e) {
  1137. Log::info('redis remote error-----------------------');
  1138. Log::info($e);
  1139. }
  1140. // 注册动作-》回调订单
  1141. $action_type = 'CallBackOrder';
  1142. $param = [
  1143. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1144. 'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
  1145. ];
  1146. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1147. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1148. //$this->huaweiActivity($order->activity_id,$uid,$product_id);
  1149. //交换订单时间
  1150. $this->exchangeUpdateAndCreated($order->id);
  1151. }
  1152. }
  1153. }
  1154. }
  1155. } catch (\Exception $e) {
  1156. \Log::error('union: ' . $e->getMessage());
  1157. }
  1158. return 'SUCCESS';
  1159. }
  1160. /**
  1161. * 通联支付回调
  1162. * @param Request $request
  1163. */
  1164. function wcback_allinpay(Request $request)
  1165. {
  1166. Log::info('wcback_allinpay_request');
  1167. try {
  1168. $data = $request->all();
  1169. Log::info($request->all());
  1170. if (!$data['trxreserved']) return 'fail';
  1171. $pay_merchant_id = json_decode($data['trxreserved'], true)['pmi'];
  1172. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  1173. unset($data['_url']);
  1174. if (!$pay_merchant->config_info) return 'fail';
  1175. $wechatPay = WechatPay::instance('ALLINPAY', json_decode($pay_merchant->config_info, true));
  1176. if ($wechatPay->NotifyValidSign($data, $wechatPay->appkey)) {
  1177. Log::info('allinpay_notify_sign_success');
  1178. /**
  1179. * 只有0000表示交易成功或下单成功,其他为失败
  1180. * 0000:交易成功
  1181. * 3045,3088:交易超时
  1182. * 3008:余额不足
  1183. * 3999:交易失败
  1184. * 2008:交易处理中
  1185. * 3050:交易已撤销
  1186. */
  1187. if ($data['trxstatus'] == '0000') {
  1188. // 修改表比较多,开启事务
  1189. DB::beginTransaction();
  1190. $trade_no = $data['cusorderid'];
  1191. $transaction_id = $data['chnltrxid'];
  1192. //$openid = $data['acct'];
  1193. $remarks = $data['trxreserved'];
  1194. // $remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
  1195. $remarks = json_decode($remarks, true);
  1196. Log::info('$remarks');
  1197. Log::info($remarks);
  1198. // 更新Order
  1199. $order = OrderService::getByTradeNo($trade_no);
  1200. // 防止重复推送
  1201. if (isset($order->status) && $order->status == 'PAID') {
  1202. Log::info('has_pay:' . $trade_no);
  1203. echo 'success';
  1204. exit();
  1205. }
  1206. $uid = $remarks['uid'];
  1207. $distribution_channel_id = $remarks['dcd'];
  1208. $product_id = $remarks['pd'];
  1209. $product = ProductService::getProductSingle($product_id);
  1210. $pay_merchant_source = $remarks['pms'];
  1211. $pay_merchant_id = $remarks['pmi'];
  1212. $send_order_id = $remarks['soi'];
  1213. $create_ip = $remarks['cp'];
  1214. $price = $product->price;
  1215. $bid = $remarks['bid'];
  1216. Log::info('save_order_end');
  1217. //$this->updateOrderTotal($trade_no,$transaction_id);
  1218. Log::info($product);
  1219. Log::info('product_type:' . $product->type);
  1220. $prize_fee = (int) (($product->price * 100) * 0.1);
  1221. $this->substituteOrderPrice($order->id, $prize_fee);
  1222. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1223. //获取用户充值次数
  1224. $order->pay_type = $this->getChargeTimes($order->uid);
  1225. // 更新其他定制Order表
  1226. if ($product->type == 'YEAR_ORDER') {
  1227. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1228. $order_type = 'YEAR';
  1229. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1230. $order->order_type = $order_type;
  1231. $order->status = 'PAID';
  1232. $order->pay_end_at = date('Y-m-d H:i:s');
  1233. $order->transaction_id = $transaction_id;
  1234. $order->save();
  1235. //$price = $price/100;
  1236. /*
  1237. $this->createOrder(
  1238. compact(
  1239. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  1240. 'pay_merchant_id','create_ip','bid','transaction_id'
  1241. )
  1242. );*/
  1243. } elseif ($product->type == 'BOOK_ORDER') {
  1244. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1245. $order_type = 'BOOK';
  1246. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1247. $order->order_type = $order_type;
  1248. $order->status = 'PAID';
  1249. $order->pay_end_at = date('Y-m-d H:i:s');
  1250. $order->transaction_id = $transaction_id;
  1251. $order->save();
  1252. //$price = $price/100;
  1253. /*
  1254. $this->createOrder(
  1255. compact(
  1256. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  1257. 'pay_merchant_id','create_ip','bid','transaction_id'
  1258. )
  1259. );*/
  1260. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1261. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1262. $order_type = 'RECHARGE';
  1263. $this->userCharge($product, $uid);
  1264. $order->order_type = $order_type;
  1265. $order->status = 'PAID';
  1266. $order->pay_end_at = date('Y-m-d H:i:s');
  1267. $order->transaction_id = $transaction_id;
  1268. $order->save();
  1269. //$price = $price/100;
  1270. /*
  1271. $this->createOrder(
  1272. compact(
  1273. 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
  1274. 'pay_merchant_id','create_ip','bid','transaction_id'
  1275. ));*/
  1276. } elseif ($product->type == 'FOREVER') {
  1277. Log::info('FOREVER------------------------FOREVER');
  1278. $order_type = 'FOREVER';
  1279. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1280. $order->order_type = $order_type;
  1281. $order->status = 'PAID';
  1282. $order->pay_end_at = date('Y-m-d H:i:s');
  1283. $order->transaction_id = $transaction_id;
  1284. $order->save();
  1285. }
  1286. $this->successPayPushMsg($uid, $product, $order->id);
  1287. $this->orderStatistical($order);
  1288. DB::commit();
  1289. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1290. //$this->huaweiActivity($order->activity_id,$uid,$product_id);
  1291. //交换订单时间
  1292. $this->exchangeUpdateAndCreated($order->id);
  1293. //redis 删除未支付的uid
  1294. try {
  1295. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1296. Redis::hdel($key, $uid);
  1297. //Redis::srem($key,$uid);
  1298. } catch (\Exception $e) {
  1299. Log::info('redis remote error-----------------------');
  1300. Log::info($e);
  1301. }
  1302. }
  1303. } else {
  1304. Log::info('allinpay_notify_sign_fail');
  1305. }
  1306. } catch (\Exception $e) {
  1307. DB::rollBack();
  1308. Log::info('receive_allinpay_ept:' . $e->getMessage());
  1309. Log::info('pay_callback_end fail');
  1310. echo 'fail';
  1311. return;
  1312. }
  1313. Log::info('pay_callback_end');
  1314. echo "success";
  1315. }
  1316. public function wcback_lianlianpay(Request $request)
  1317. {
  1318. $data = $request->getContent();
  1319. Log::info('wcback_lianlianpay call back enter');
  1320. Log::info($request->getContent());
  1321. $data = json_decode($data, true);
  1322. if ($data['result_pay'] == 'SUCCESS') {
  1323. $trade_no = $data['no_order'];
  1324. $order = OrderService::getByTradeNo($trade_no);
  1325. if (!$order) return response()->json(['ret_code' => '-1']);
  1326. $pay_merchant_id = $order->pay_merchant_id;
  1327. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
  1328. unset($data['_url']);
  1329. if (!$pay_merchant->config_info) response()->json(['ret_code' => '-1']);
  1330. $wechatPay = WechatPay::instance('LIANLIANPAY', json_decode($pay_merchant->config_info, true));
  1331. $uid = $order->uid;
  1332. $distribution_channel_id = $order->distribution_channel_id;
  1333. $price = $order->price;
  1334. $send_order_id = $order->send_order_id;
  1335. $product_id = $order->product_id;
  1336. if (!$wechatPay->rsaCheck($data)) {
  1337. Log::info('sign check error');
  1338. //return response()->json(['ret_code'=>'-1']);
  1339. }
  1340. // 防止重复推送
  1341. if (isset($order->status) && $order->status == 'PAID') {
  1342. Log::info('has_pay:' . $trade_no);
  1343. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  1344. }
  1345. DB::beginTransaction();
  1346. try {
  1347. $product = ProductService::getProductSingle($order->product_id);
  1348. $transaction_id = $data['oid_paybill'];
  1349. $prize_fee = (int) (($product->price * 100));
  1350. $this->substituteOrderPrice($order->id, $prize_fee);
  1351. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1352. //获取用户充值次数
  1353. $order->pay_type = $this->getChargeTimes($order->uid);
  1354. // 更新其他定制Order表
  1355. if ($product->type == 'YEAR_ORDER') {
  1356. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1357. $order_type = 'YEAR';
  1358. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1359. $order->order_type = $order_type;
  1360. $order->status = 'PAID';
  1361. $order->pay_end_at = date('Y-m-d H:i:s');
  1362. $order->transaction_id = $transaction_id;
  1363. $order->save();
  1364. } elseif ($product->type == 'BOOK_ORDER') {
  1365. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1366. $order_type = 'BOOK';
  1367. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1368. $order->order_type = $order_type;
  1369. $order->status = 'PAID';
  1370. $order->pay_end_at = date('Y-m-d H:i:s');
  1371. $order->transaction_id = $transaction_id;
  1372. $order->save();
  1373. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1374. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1375. $order_type = 'RECHARGE';
  1376. $this->userCharge($product, $uid);
  1377. $order->order_type = $order_type;
  1378. $order->status = 'PAID';
  1379. $order->pay_end_at = date('Y-m-d H:i:s');
  1380. $order->transaction_id = $transaction_id;
  1381. $order->save();
  1382. } elseif ($product->type == 'FOREVER') {
  1383. Log::info('FOREVER------------------------FOREVER');
  1384. $order_type = 'FOREVER';
  1385. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1386. $order->order_type = $order_type;
  1387. $order->status = 'PAID';
  1388. $order->pay_end_at = date('Y-m-d H:i:s');
  1389. $order->transaction_id = $transaction_id;
  1390. $order->save();
  1391. }
  1392. $this->successPayPushMsg($uid, $product, $order->id);
  1393. $this->orderStatistical($order);
  1394. DB::commit();
  1395. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1396. //$this->huaweiActivity($order->activity_id,$uid,$product_id);
  1397. //redis 删除未支付的uid
  1398. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
  1399. Redis::hdel($key, $uid);
  1400. //交换订单时间
  1401. $this->exchangeUpdateAndCreated($order->id);
  1402. return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
  1403. } catch (\Exception $e) {
  1404. DB::rollBack();
  1405. Log::info('lianlian:' . $e->getMessage());
  1406. echo 'fail';
  1407. return;
  1408. }
  1409. }
  1410. echo 'success';
  1411. }
  1412. /**
  1413. * 微众支付回调
  1414. * @param Request $request
  1415. */
  1416. function wcback_palmpay(Request $request)
  1417. {
  1418. Log::info('wcback_palmpay_request');
  1419. try {
  1420. $data = $request->except('_url');
  1421. Log::info($request->all());
  1422. //订单
  1423. $trade_no = $data['outTradeNo'];
  1424. $order = OrderService::getByTradeNo($trade_no);
  1425. if (!$order) die('failed');
  1426. // 防止重复推送
  1427. if (isset($order->status) && $order->status == 'PAID') {
  1428. if ($data['chorderid'] != $order->transaction_id) {
  1429. Log::info('palmpay order repeat');
  1430. $this->palmpaySpecial($data['chorderid'], $trade_no);
  1431. }
  1432. Log::info('has_pay:' . $trade_no);
  1433. echo 'success';
  1434. exit();
  1435. }
  1436. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
  1437. //校验签名
  1438. $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  1439. if ($sign != $data['sign']) die('failed');
  1440. $transaction_id = $data['chorderid'];
  1441. if ($data['status'] == 'success') {
  1442. Log::info('wcback_palmpay_notify_sign_success');
  1443. if ($data['status'] == 'success') {
  1444. // 修改表比较多,开启事务
  1445. DB::beginTransaction();
  1446. // 更新Order
  1447. $order = OrderService::getByTradeNo($trade_no);
  1448. $uid = $order->uid;
  1449. $distribution_channel_id = $order->distribution_channel_id;
  1450. $product_id = $order->product_id;
  1451. $product = ProductService::getProductSingle($product_id);
  1452. $send_order_id = $order->send_order_id;
  1453. $price = $product->price;
  1454. Log::info('save_order_end');
  1455. Log::info($product);
  1456. Log::info('product_type:' . $product->type);
  1457. $prize_fee = (int) (($product->price * 100) * 0.1);
  1458. $this->substituteOrderPrice($order->id, $prize_fee);
  1459. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1460. //获取用户充值次数
  1461. $order->pay_type = $this->getChargeTimes($order->uid);
  1462. // 更新其他定制Order表
  1463. if ($product->type == 'YEAR_ORDER') {
  1464. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1465. $order_type = 'YEAR';
  1466. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1467. $order->order_type = $order_type;
  1468. $order->status = 'PAID';
  1469. $order->pay_end_at = date('Y-m-d H:i:s');
  1470. $order->transaction_id = $transaction_id;
  1471. $order->save();
  1472. } elseif ($product->type == 'BOOK_ORDER') {
  1473. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1474. $order_type = 'BOOK';
  1475. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1476. $order->order_type = $order_type;
  1477. $order->status = 'PAID';
  1478. $order->pay_end_at = date('Y-m-d H:i:s');
  1479. $order->transaction_id = $transaction_id;
  1480. $order->save();
  1481. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1482. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1483. $order_type = 'RECHARGE';
  1484. $this->userCharge($product, $uid);
  1485. $order->order_type = $order_type;
  1486. $order->status = 'PAID';
  1487. $order->pay_end_at = date('Y-m-d H:i:s');
  1488. $order->transaction_id = $transaction_id;
  1489. $order->save();
  1490. } elseif ($product->type == 'FOREVER') {
  1491. Log::info('FOREVER------------------------FOREVER');
  1492. $order_type = 'FOREVER';
  1493. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1494. $order->order_type = $order_type;
  1495. $order->status = 'PAID';
  1496. $order->pay_end_at = date('Y-m-d H:i:s');
  1497. $order->transaction_id = $transaction_id;
  1498. $order->save();
  1499. }
  1500. $this->successPayPushMsg($uid, $product, $order->id);
  1501. $this->orderStatistical($order);
  1502. DB::commit();
  1503. //redis 删除未支付的uid
  1504. try {
  1505. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1506. Redis::hdel($key, $uid);
  1507. } catch (\Exception $e) {
  1508. Log::info('redis remote error-----------------------');
  1509. Log::info($e);
  1510. }
  1511. // 注册动作-》回调订单
  1512. $action_type = 'CallBackOrder';
  1513. $param = [
  1514. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1515. 'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
  1516. ];
  1517. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1518. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1519. //$this->huaweiActivity($order->activity_id,$uid,$product_id);
  1520. //交换订单时间
  1521. $this->exchangeUpdateAndCreated($order->id);
  1522. }
  1523. } else {
  1524. Log::info('wcback_palmpay_notify_sign_fail');
  1525. }
  1526. } catch (\Exception $e) {
  1527. var_dump($e->getMessage());
  1528. DB::rollBack();
  1529. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  1530. echo 'fail';
  1531. Log::info('pay_callback_end error');
  1532. return;
  1533. }
  1534. Log::info('pay_callback_end');
  1535. echo "success";
  1536. }
  1537. //平安银行支付回调
  1538. function wcback_originbank(Request $request)
  1539. {
  1540. Log::info($request->all());
  1541. Log::info('wcback_palmpay_request');
  1542. try {
  1543. $data = $request->except('_url');
  1544. unset($data['sign']);
  1545. //订单
  1546. $trade_no = $data['out_no'];
  1547. $order = OrderService::getByTradeNo($trade_no);
  1548. if (!$order) die('failed');
  1549. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->first();
  1550. // 防止重复推送
  1551. if (isset($order->status) && $order->status == 'PAID') {
  1552. Log::info('has_pay:' . $trade_no);
  1553. echo 'success';
  1554. exit();
  1555. }
  1556. //校验签名
  1557. // $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
  1558. // if ($sign != $data['sign']) die('failed');
  1559. $trade_result = json_decode($data['trade_result'], true);
  1560. $transaction_id = $trade_result['transaction_id'];
  1561. if ($trade_result['return_code'] == 'SUCCESS') {
  1562. Log::info('wcback_palmpay_notify_sign_success');
  1563. // 修改表比较多,开启事务
  1564. DB::beginTransaction();
  1565. $uid = $order->uid;
  1566. $distribution_channel_id = $order->distribution_channel_id;
  1567. $product_id = $order->product_id;
  1568. $product = ProductService::getProductSingle($product_id);
  1569. $send_order_id = $order->send_order_id;
  1570. $price = $product->price;
  1571. Log::info('save_order_end');
  1572. Log::info($product);
  1573. Log::info('product_type:' . $product->type);
  1574. $prize_fee = (int) (($product->price * 100) * 0.1);
  1575. $this->substituteOrderPrice($order->id, $prize_fee);
  1576. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1577. //获取用户充值次数
  1578. $order->pay_type = $this->getChargeTimes($order->uid);
  1579. // 更新其他定制Order表
  1580. if ($product->type == 'YEAR_ORDER') {
  1581. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1582. $order_type = 'YEAR';
  1583. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1584. $order->order_type = $order_type;
  1585. $order->status = 'PAID';
  1586. $order->pay_end_at = date('Y-m-d H:i:s');
  1587. $order->transaction_id = $transaction_id;
  1588. $order->save();
  1589. } elseif ($product->type == 'BOOK_ORDER') {
  1590. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1591. $order_type = 'BOOK';
  1592. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1593. $order->order_type = $order_type;
  1594. $order->status = 'PAID';
  1595. $order->pay_end_at = date('Y-m-d H:i:s');
  1596. $order->transaction_id = $transaction_id;
  1597. $order->save();
  1598. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1599. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1600. $order_type = 'RECHARGE';
  1601. $this->userCharge($product, $uid);
  1602. $order->order_type = $order_type;
  1603. $order->status = 'PAID';
  1604. $order->pay_end_at = date('Y-m-d H:i:s');
  1605. $order->transaction_id = $transaction_id;
  1606. $order->save();
  1607. } elseif ($product->type == 'FOREVER') {
  1608. Log::info('FOREVER------------------------FOREVER');
  1609. $order_type = 'FOREVER';
  1610. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1611. $order->order_type = $order_type;
  1612. $order->status = 'PAID';
  1613. $order->pay_end_at = date('Y-m-d H:i:s');
  1614. $order->transaction_id = $transaction_id;
  1615. $order->save();
  1616. }
  1617. $this->successPayPushMsg($uid, $product, $order->id);
  1618. $this->orderStatistical($order);
  1619. DB::commit();
  1620. //redis 删除未支付的uid
  1621. try {
  1622. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1623. Redis::hdel($key, $uid);
  1624. } catch (\Exception $e) {
  1625. Log::info('redis remote error-----------------------');
  1626. Log::info($e);
  1627. }
  1628. // 注册动作-》回调订单
  1629. $action_type = 'CallBackOrder';
  1630. $param = [
  1631. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1632. 'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
  1633. ];
  1634. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1635. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1636. //$this->huaweiActivity($order->activity_id,$uid,$product_id);
  1637. //交换订单时间
  1638. $this->exchangeUpdateAndCreated($order->id);
  1639. } else {
  1640. Log::info('wcback_palmpay_notify_sign_fail');
  1641. }
  1642. } catch (\Exception $e) {
  1643. var_dump($e->getMessage());
  1644. DB::rollBack();
  1645. Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
  1646. echo 'fail';
  1647. Log::info('pay_callback_end error');
  1648. return;
  1649. }
  1650. Log::info('pay_callback_end');
  1651. echo "success";
  1652. }
  1653. //市民卡支付回调
  1654. function wcback_smkpay(Request $request)
  1655. {
  1656. Log::info('wcback_smkpay start:');
  1657. $res_data = [];
  1658. try {
  1659. $data = $request->except('_url');
  1660. $source_sign = $data['sign'];
  1661. unset($data['sign']);
  1662. //订单
  1663. $trade_no = $data['orderNo'];
  1664. $order = OrderService::getByTradeNo($trade_no);
  1665. if (!$order) die('failed');
  1666. $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->first();
  1667. $pay_config_info = json_decode($pay_merchant->config_info, 1);
  1668. // 防止重复推送
  1669. if (isset($order->status) && $order->status == 'PAID') {
  1670. Log::info('has_pay:' . $trade_no);
  1671. $res_data['respCode'] = '00';
  1672. $res_data['respDesc'] = '成功';
  1673. $res_data['merCode'] = $pay_config_info['merCode'];
  1674. $res_data['reqSeq'] = $data['reqSeq'];
  1675. return response()->json($res_data);
  1676. }
  1677. //校验签名
  1678. $wechatPay = WechatPay::instance($pay_merchant->source, $pay_config_info);
  1679. if (!$wechatPay->checkSign($data, $source_sign)) {
  1680. Log::info('sign check error:');
  1681. Log::info($request->all());
  1682. die('failed');
  1683. }
  1684. $trade_code = $data['respCode'];
  1685. $trade_status = $data['status'];
  1686. $transaction_id = $data['serialNo'];
  1687. if ($trade_code == '00' && $trade_status == '01') {
  1688. Log::info('wcback_wcback_smkpay_notify_sign_success');
  1689. // 修改表比较多,开启事务
  1690. DB::beginTransaction();
  1691. $uid = $order->uid;
  1692. $distribution_channel_id = $order->distribution_channel_id;
  1693. $product_id = $order->product_id;
  1694. $product = ProductService::getProductSingle($product_id);
  1695. $send_order_id = $order->send_order_id;
  1696. $price = $product->price;
  1697. Log::info('save_order_end');
  1698. Log::info($product);
  1699. Log::info('product_type:' . $product->type);
  1700. $prize_fee = (int) (($product->price * 100) * 0.1);
  1701. $this->substituteOrderPrice($order->id, $prize_fee);
  1702. $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
  1703. //获取用户充值次数
  1704. $order->pay_type = $this->getChargeTimes($order->uid);
  1705. // 更新其他定制Order表
  1706. if ($product->type == 'YEAR_ORDER') {
  1707. Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
  1708. $order_type = 'YEAR';
  1709. $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1710. $order->order_type = $order_type;
  1711. $order->status = 'PAID';
  1712. $order->pay_end_at = date('Y-m-d H:i:s');
  1713. $order->transaction_id = $transaction_id;
  1714. $order->save();
  1715. } elseif ($product->type == 'BOOK_ORDER') {
  1716. Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
  1717. $order_type = 'BOOK';
  1718. $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
  1719. $order->order_type = $order_type;
  1720. $order->status = 'PAID';
  1721. $order->pay_end_at = date('Y-m-d H:i:s');
  1722. $order->transaction_id = $transaction_id;
  1723. $order->save();
  1724. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  1725. Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
  1726. $order_type = 'RECHARGE';
  1727. $this->userCharge($product, $uid);
  1728. $order->order_type = $order_type;
  1729. $order->status = 'PAID';
  1730. $order->pay_end_at = date('Y-m-d H:i:s');
  1731. $order->transaction_id = $transaction_id;
  1732. $order->save();
  1733. } elseif ($product->type == 'FOREVER') {
  1734. Log::info('FOREVER------------------------FOREVER');
  1735. $order_type = 'FOREVER';
  1736. $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
  1737. $order->order_type = $order_type;
  1738. $order->status = 'PAID';
  1739. $order->pay_end_at = date('Y-m-d H:i:s');
  1740. $order->transaction_id = $transaction_id;
  1741. $order->save();
  1742. }
  1743. $this->successPayPushMsg($uid, $product, $order->id);
  1744. $this->orderStatistical($order);
  1745. DB::commit();
  1746. //redis 删除未支付的uid
  1747. try {
  1748. $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
  1749. Redis::hdel($key, $uid);
  1750. } catch (\Exception $e) {
  1751. Log::info('redis remote error-----------------------');
  1752. Log::info($e);
  1753. }
  1754. // 注册动作-》回调订单
  1755. $action_type = 'CallBackOrder';
  1756. $param = [
  1757. 'order_sn' => isset($trade_no) ? $trade_no : '0',
  1758. 'openid' => isset($uid) ? $uid : '0', // 没有openid,用uid写log
  1759. ];
  1760. UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
  1761. $this->userProperty($uid, $price, $product->type, $order->activity_id);
  1762. } else {
  1763. Log::info('wcback_smkpay_notify_sign_fail');
  1764. }
  1765. $res_data['respCode'] = '00';
  1766. $res_data['respDesc'] = '成功';
  1767. $res_data['merCode'] = $pay_config_info['merCode'];
  1768. $res_data['reqSeq'] = $order->transaction_id;
  1769. return response()->json($res_data);
  1770. Log::info('pay_callback_end');
  1771. } catch (\Exception $e) {
  1772. DB::rollBack();
  1773. Log::info('receive_smkpay_palmpay_ept:' . $e->getMessage());
  1774. echo 'fail';
  1775. Log::info('pay_callback_end error');
  1776. return;
  1777. }
  1778. }
  1779. /**
  1780. * 公众号签名@华灯初上
  1781. * @param $params
  1782. * @return string
  1783. */
  1784. protected function getSign($params, $key)
  1785. {
  1786. $url = $this->arr_to_url($params, false);
  1787. $url = $url . '&key=' . $key;
  1788. $sign = md5($url);
  1789. return $sign;
  1790. }
  1791. /**
  1792. * 公众号签名@华灯初上
  1793. * @param $array
  1794. * @param bool $has_sign
  1795. * @return string
  1796. */
  1797. protected function arr_to_url($array, $has_sign = false)
  1798. {
  1799. ksort($array);
  1800. reset($array);
  1801. $arg = "";
  1802. while (list($name, $val) = each($array)) {
  1803. if ($name == 'sign' && !$has_sign) continue;
  1804. if (strpos($name, "_") === 0)
  1805. continue;
  1806. if (is_array($val))
  1807. $val = join(',', $val);
  1808. if ($val === "")
  1809. continue;
  1810. $arg .= $name . "=" . $val . "&";
  1811. }
  1812. $arg = substr($arg, 0, count($arg) - 2);
  1813. return $arg;
  1814. }
  1815. /**
  1816. * 单本充值会掉
  1817. * @param $product_id
  1818. * @param $uid
  1819. * @param $send_order_id
  1820. * @param $fee
  1821. */
  1822. protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
  1823. {
  1824. $book_conf = BookConfigService::getBookByProduct($product_id);
  1825. $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
  1826. $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
  1827. $insert_data['uid'] = $uid;
  1828. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1829. $insert_data['fee'] = $fee;
  1830. $insert_data['send_order_id'] = $send_order_id;
  1831. $insert_data['charge_balance'] = 0;
  1832. $insert_data['reward_balance'] = 0;
  1833. Log::info('start_save_book_order');
  1834. Log::info($insert_data);
  1835. return BookOrderService::save_book_order($insert_data);
  1836. }
  1837. /**
  1838. * 包年
  1839. * @param $uid
  1840. * @param $distribution_channel_id
  1841. * @param $fee
  1842. * @param $send_order_id
  1843. * @return mixed
  1844. */
  1845. protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1846. {
  1847. Log::info('start_save_year_order');
  1848. $insert_data['uid'] = $uid;
  1849. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1850. $insert_data['fee'] = $fee;
  1851. $insert_data['send_order_id'] = $send_order_id;
  1852. Log::info($insert_data);
  1853. return YearOrderService::save_year_order($insert_data);
  1854. }
  1855. private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
  1856. {
  1857. Log::info('start_save_year_order');
  1858. ('start_forever_Free_Order');
  1859. $insert_data['uid'] = $uid;
  1860. $insert_data['distribution_channel_id'] = $distribution_channel_id;
  1861. $insert_data['fee'] = $fee;
  1862. $insert_data['send_order_id'] = $send_order_id;
  1863. Log::info($insert_data);
  1864. return YearOrderService::foreverFree($insert_data);
  1865. }
  1866. /**
  1867. * 用户充值
  1868. * @param $product
  1869. * @param $uid\
  1870. */
  1871. protected function userCharge($product, $uid)
  1872. {
  1873. $total = $product->price * 100 + $product->given;
  1874. UserService::addBalance($uid, $total, $product->price * 100, $product->given);
  1875. Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
  1876. }
  1877. /**
  1878. * 添加订单
  1879. * @param array $data
  1880. * @return mixed
  1881. */
  1882. protected function createOrder(array $data)
  1883. {
  1884. $insert_data = array();
  1885. $insert_data['uid'] = $data['uid'];
  1886. $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
  1887. $insert_data['product_id'] = $data['product_id'];
  1888. $insert_data['price'] = $data['price'];
  1889. $insert_data['status'] = 'PAID';
  1890. $insert_data['pay_type'] = 1;
  1891. $insert_data['trade_no'] = $data['trade_no'];
  1892. $insert_data['send_order_id'] = $data['send_order_id'];
  1893. $insert_data['order_type'] = $data['order_type'];
  1894. $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
  1895. $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
  1896. $insert_data['transaction_id'] = $data['transaction_id'];
  1897. $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
  1898. $insert_data['create_ip'] = $data['create_ip'];
  1899. $insert_data['from_bid'] = $data['bid'];
  1900. Log::info('createOrder');
  1901. Log::info($insert_data);
  1902. return OrderService::save_order($insert_data);
  1903. }
  1904. /**
  1905. * 添加位置付订单
  1906. * @param $data
  1907. * @return mixed
  1908. */
  1909. protected function createUnPayOrder($data)
  1910. {
  1911. $data['status'] = 'UNPAID';
  1912. $data['transaction_id'] = '';
  1913. $data['pay_end_at'] = '0000-00-00 00:00:00';
  1914. return OrderService::save_order($data);
  1915. }
  1916. /**
  1917. * order total
  1918. * @param $data
  1919. *
  1920. */
  1921. protected function createOrderTaotal($data)
  1922. {
  1923. $data['status'] = 'UNPAID';
  1924. $data['created_at'] = date('Y-m-d H:i:s');
  1925. $data['updated_at'] = date('Y-m-d H:i:s');
  1926. DB::table('orders_total')->insert($data);
  1927. }
  1928. protected function updateOrderTotal($trade_no, $transaction_id)
  1929. {
  1930. $data['status'] = 'PAID';
  1931. $data['transaction_id'] = $transaction_id;
  1932. $data['pay_end_at'] = date('Y-m-d H:i:s');
  1933. //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
  1934. }
  1935. protected function successPayPushMsg($uid, $product_info, $order_id)
  1936. {
  1937. $force_sub_info = $this->getSubscribeV2($uid);
  1938. $data = UserService::getById($uid);
  1939. try {
  1940. if (!in_array($data->distribution_channel_id, [5, 123])) {
  1941. //return false;
  1942. }
  1943. if ($force_sub_info) {
  1944. //$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>";
  1945. if (SubstituteOrderService::getByOrderId($order_id)) {
  1946. $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>";
  1947. } else {
  1948. $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>";
  1949. }
  1950. if ($product_info->type == 'YEAR_ORDER') {
  1951. $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
  1952. } elseif ($product_info->type == 'TICKET_RECHARGE') {
  1953. if ($product_info->given) {
  1954. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
  1955. } else {
  1956. $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
  1957. }
  1958. } else {
  1959. $money_text = '未知';
  1960. }
  1961. $delay = 0;
  1962. $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
  1963. $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
  1964. foreach ($force_sub_info as $item) {
  1965. $appid = $item->appid;
  1966. $gzh = OfficialAccountService::officialAccountByAppid(compact('appid'));
  1967. $top_url = OfficialAccountService::getTopUrl($gzh->service_type_info);
  1968. if ($gzh->service_type_info == 1) {
  1969. $top_content = "\r\n\r\n为方便阅读,请<a href='{$top_url}'>设为星标</a>";
  1970. } else {
  1971. $top_content = "\r\n\r\n为方便阅读,请<a href='{$top_url}'>置顶公众号</a>";
  1972. }
  1973. $content .= $top_content . $top_content . $top_content;
  1974. $res['openid'] = $item->openid;
  1975. $res['appid'] = $item->appid;
  1976. $res['content'] = $content;
  1977. $res['type'] = 'one_task';
  1978. $res['send_time'] = date("Y-m-d H:i:s");
  1979. $res['task_id'] = md5('pay_success_push');
  1980. $send_data = array(
  1981. 'send_time' => date("Y-m-d H:i:s"),
  1982. 'data' => $res
  1983. );
  1984. dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
  1985. }
  1986. }
  1987. } catch (\Exception $e) {
  1988. Log::info('pay_success_push error');
  1989. Log::info($e);
  1990. }
  1991. $this->notifyXiyue($uid, $product_info->price, $data->distribution_channel_id, $data->openid);
  1992. return true;
  1993. }
  1994. private function orderStatistical($order)
  1995. {
  1996. try {
  1997. $send_data = array(
  1998. 'type' => 'order_info',
  1999. 'data' => $order->toArray()
  2000. );
  2001. $send = new SendStatisticsList($send_data);
  2002. $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
  2003. dispatch($job);
  2004. } catch (\Exception $e) {
  2005. Log::info($e);
  2006. }
  2007. //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
  2008. try {
  2009. if ($order->from_type) {
  2010. $pv = Redis::hget('push:distribution_channel_id:allpv', $order->from_type);
  2011. if ($pv) {
  2012. Redis::sadd('push:all:paidnum:from:' . $order->from_type, $order->uid);
  2013. $old = Redis::hget('push:all:paidamount', $order->from_type);
  2014. if ($old) {
  2015. Redis::hset('push:all:paidamount', $order->from_type, $old + $order->price);
  2016. } else {
  2017. Redis::hset('push:all:paidamount', $order->from_type, $order->price);
  2018. }
  2019. }
  2020. }
  2021. if (isset($order->pay_merchant_id) && $order->pay_merchant_id) //临时统计队列清0 用于报警
  2022. {
  2023. $key = 'pay_merchant:' . $order->pay_merchant_id;
  2024. Redis::hset($key, 'unpaid_num', 0);
  2025. }
  2026. } catch (\Exception $e) { }
  2027. }
  2028. private function getSubscribe($uid)
  2029. {
  2030. return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
  2031. }
  2032. private function getSubscribeV2($uid)
  2033. {
  2034. return DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->get();
  2035. }
  2036. public function getSubscribeOfficialName($uid, $distribution_channel_id)
  2037. {
  2038. $subscribe = $this->getSubscribe($uid);
  2039. if ($subscribe && isset($subscribe->appid)) {
  2040. $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
  2041. if ($official && isset($official->nickname)) {
  2042. return $official->nickname;
  2043. }
  2044. }
  2045. $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
  2046. if (isset($res->nickname) && !empty($res->nickname)) {
  2047. return $res->nickname;
  2048. }
  2049. try {
  2050. $official = DB::table('official_accounts')
  2051. ->where('distribution_channel_id', $distribution_channel_id)
  2052. ->where('is_auth', 1)
  2053. ->orderBy('id', 'desc')
  2054. ->select('nickname')->first();
  2055. if ($official && isset($official->nickname)) {
  2056. return $official->nickname;
  2057. }
  2058. } catch (\Exception $e) { }
  2059. return '';
  2060. }
  2061. private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
  2062. {
  2063. $change_pay_id = 96;
  2064. if ($pay_merchant_id == 96) {
  2065. $change_pay_id = 9;
  2066. }
  2067. try {
  2068. $time = (int) date('G');
  2069. $change = false;
  2070. if ($time <= 8 || $time >= 23) {
  2071. if (!$pay_info && $pay_merchant_id != $change_pay_id) {
  2072. DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
  2073. $change = true;
  2074. }
  2075. }
  2076. //$number,$template_type,$param=array()
  2077. $phone_arr = ['15868100210', '15088790066', '13858057394', '18668029091', '18668420256'];
  2078. //$phone_arr = ['18668029091'];
  2079. $param = ['pay_id' => $pay_merchant_id];
  2080. if ($n) {
  2081. $template_type = 'pay_channel_remind';
  2082. //$content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
  2083. } else {
  2084. $template_type = 'pay_channel_remind';
  2085. //$content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
  2086. }
  2087. if ($change) {
  2088. $template_type = 'pay_channel_change';
  2089. $param = ['pay_id' => $pay_merchant_id, 'new_pay_id' => $change_pay_id];
  2090. }
  2091. $last_alert_time = Redis::hget('autopayalert', $pay_merchant_id);
  2092. if ($last_alert_time && (time() - $last_alert_time) <= 3600) {
  2093. return;
  2094. }
  2095. Redis::hset('autopayalert', $pay_merchant_id, time());
  2096. foreach ($phone_arr as $phone) {
  2097. AliSMS::send($phone, $template_type, $param);
  2098. }
  2099. } catch (\Exception $e) { }
  2100. }
  2101. private function createSubstituteOrder($order_id, $uid, $pay_uid)
  2102. {
  2103. SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
  2104. }
  2105. private function recordOtherParam($order_id, $uid)
  2106. {
  2107. $gxhp = ReadRecordService::getByField($uid, 'gxhp');
  2108. if ($gxhp) {
  2109. OrderParamService::create($order_id, $gxhp);
  2110. }
  2111. }
  2112. private function substituteOrderPrice($order_id, $prize_fee = 500)
  2113. {
  2114. SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
  2115. }
  2116. private function updateUserSendOrderId(int $uid, $send_order_id)
  2117. {
  2118. if ($send_order_id && $uid) {
  2119. $user = UserService::getById($uid);
  2120. if ($user && !$user->send_order_id) {
  2121. UserService::updateInfo($uid, compact('send_order_id'));
  2122. }
  2123. }
  2124. }
  2125. /**
  2126. * 测书智能推送统计 付费用户数、充值金额
  2127. * @param $bid
  2128. * @param $uid
  2129. * @param $price
  2130. */
  2131. private function smartPushTestBookPaidUv($bid, $uid, $price)
  2132. {
  2133. $smart_bid = ReadRecordService::getSmartPush($uid);
  2134. if ($smart_bid && in_array($bid, $smart_bid)) {
  2135. try {
  2136. //$key = 'smartPushTestBookPaidUv:bid:%s';
  2137. //Redis::sadd(sprintf($key, $bid), $uid);
  2138. $now_amount = Redis::hget('smartPushTestBookPaidAmount', $bid);
  2139. if (!$now_amount) {
  2140. $now_amount = 0;
  2141. }
  2142. Redis::hset('smartPushTestBookPaidAmount', $bid, $now_amount + $price);
  2143. } catch (\Exception $e) { }
  2144. }
  2145. }
  2146. /**
  2147. * 获取用户第几次充值
  2148. * @param $uid
  2149. * @return int
  2150. */
  2151. private function getChargeTimes($uid)
  2152. {
  2153. $count = OrderService::getUserChargeTimes($uid);
  2154. return $count + 1;
  2155. }
  2156. //米玩 订单转移
  2157. private function transfer($distribution_channel_id)
  2158. {
  2159. if (in_array($distribution_channel_id, explode(',', redisEnv('MIWAN_TRANSFER_CHANNEL_ID', 1)))) {
  2160. $random = random_int(1, 100);
  2161. $miwan_transfer_probability = redisEnv('MIWAN_TRANSFER_PROBABILITY', 0);
  2162. $miwan_transfer_to_channel_id = redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0);
  2163. if ($miwan_transfer_to_channel_id && $random <= $miwan_transfer_probability) {
  2164. return $miwan_transfer_to_channel_id;
  2165. }
  2166. }
  2167. return 0;
  2168. }
  2169. private function userProperty($uid, $price, $charge_type, $activity_id)
  2170. {
  2171. /*try{
  2172. $old_property = UserDivisionCpcPropertyService::getUserProperty($uid);
  2173. if(!$activity_id){
  2174. $this->chargeGiveGift($uid,$old_property,$price,$charge_type);
  2175. }
  2176. $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
  2177. if(!$userproperty) {
  2178. return ;
  2179. }
  2180. UserDivisionCpcPropertyService::createorUpdate($userproperty);
  2181. }catch (\Exception $e){}*/
  2182. try {
  2183. $old_property = UserDivisionCpcPropertyService::getUserPropertyV2($uid);
  2184. if (!$activity_id) {
  2185. $this->chargeGiveGift($uid, $old_property, $price, $charge_type);
  2186. }
  2187. $userproperty = UserDivisionCpcPropertyService::calculateUserPropertyV2($uid);
  2188. if (!$userproperty) {
  2189. return;
  2190. }
  2191. UserDivisionCpcPropertyService::createorUpdateV2($userproperty);
  2192. } catch (\Exception $e) {
  2193. Log::error('pay userProperty error');
  2194. Log::error($e);
  2195. }
  2196. }
  2197. private function huaweiActivity($activity_id, $uid, $product_id)
  2198. {
  2199. return;
  2200. /*$huawei_activity_id = redisEnv('HUAWEI_ACTIVITY_ID');
  2201. if($huawei_activity_id && $activity_id && $activity_id == $huawei_activity_id){
  2202. $product_id_relate_lottery_count = ['6117'=>1,'6118'=>2,'6119'=>4,'6120'=>8];
  2203. if(isset($product_id_relate_lottery_count[$product_id])){
  2204. $info = DB::table('huaweilottery_user_counts')->where('uid',$uid)->select('count')->first();
  2205. if($info){
  2206. DB::table('huaweilottery_user_counts')->where('uid',$uid)->update([
  2207. 'uid'=>$uid,
  2208. 'count'=>$info->count+$product_id_relate_lottery_count[$product_id],
  2209. 'updated_at'=>date('Y-m-d H:i:s')
  2210. ]);
  2211. }else{
  2212. DB::table('huaweilottery_user_counts')->insert([
  2213. 'uid'=>$uid,
  2214. 'count'=>$product_id_relate_lottery_count[$product_id],
  2215. 'updated_at'=>date('Y-m-d H:i:s'),
  2216. 'created_at'=>date('Y-m-d H:i:s')
  2217. ]);
  2218. }
  2219. }
  2220. }*/
  2221. }
  2222. private function palmpaySpecial($transaction_id, $trade_no)
  2223. {
  2224. try {
  2225. $order = OrderService::getByTradeNo($trade_no);
  2226. $product_id = $order->product_id;
  2227. $product = ProductService::getProductSingle($product_id);
  2228. $init_order = [
  2229. 'distribution_channel_id' => $order->distribution_channel_id,
  2230. 'uid' => $order->uid,
  2231. 'product_id' => $product_id,
  2232. 'price' => $order->price,
  2233. 'pay_type' => 1,
  2234. 'trade_no' => $order->uid . '-' . $order->id,
  2235. 'pay_merchant_source' => $order->pay_merchant_source,
  2236. 'pay_merchant_id' => $order->pay_merchant_id,
  2237. 'create_ip' => $order->create_ip,
  2238. 'send_order_id' => $order->send_order_id,
  2239. 'send_order_name' => $order->send_order_name,
  2240. 'order_type' => $order->order_type,
  2241. 'from_bid' => $order->from_bid,
  2242. 'from_type' => $order->from_type,
  2243. 'activity_id' => $order->activity_id,
  2244. 'inner_send_order_id' => $order->inner_send_order_id,
  2245. 'status' => 'PAID',
  2246. 'transaction_id' => $transaction_id,
  2247. 'pay_end_at' => date('Y-m-d H:i:s')
  2248. ];
  2249. OrderService::save_order($init_order);
  2250. if ($product->type == 'YEAR_ORDER') {
  2251. Log::info('palmpaySpecial YEAR_ORDER');
  2252. $this->yearOrder($order->uid, $order->distribution_channel_id, $order->price, $order->send_order_id);
  2253. } elseif ($product->type == 'BOOK_ORDER') {
  2254. Log::info('palmpaySpecial BOOK_ORDER');
  2255. $this->bookOrder($product_id, $order->ui, $order->send_order_id, $order->price, $order->distribution_channel_id);
  2256. } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
  2257. Log::info('palmpaySpecial TICKET_RECHARGE');
  2258. $this->userCharge($product, $order->uid);
  2259. } elseif ($product->type == 'FOREVER') {
  2260. Log::info('palmpaySpecial-------FOREVER');
  2261. $this->foreverFreeOrder($order->uid, $order->distribution_channel_id, $order->price, $order->send_order_id);
  2262. }
  2263. } catch (\Exception $e) {
  2264. Log::info($e);
  2265. }
  2266. }
  2267. private function chargeGiveGift($uid, $property, $price, $charge_type)
  2268. {
  2269. $gift = BookGiftsService::chargeGiveGift($property, $price, $charge_type);
  2270. if ($gift) {
  2271. UserGiftService::addGift($uid, $gift);
  2272. }
  2273. return;
  2274. }
  2275. private function orderParamWso($uid, $order_id)
  2276. {
  2277. list($new_outer, $new_inner, $new_total) = ReadRecordService::getByMultiField($uid, 'new_outer', 'new_inner', 'new_total');
  2278. if (!$new_outer && !$new_inner && !$new_total) {
  2279. return;
  2280. }
  2281. if (!$new_total) return;
  2282. $new_outer = $new_outer ? $new_outer : 0;
  2283. $new_inner = $new_inner ? $new_inner : 0;
  2284. $total_type = '';
  2285. $total_param_id = 0;
  2286. if (is_numeric($new_total)) {
  2287. $send_order_info = SendOrderService::getById($new_total);
  2288. $total_param_id = $new_total;
  2289. $total_type = 'none';
  2290. if ($send_order_info && isset($send_order_info->promotion_type)) {
  2291. if ($send_order_info->promotion_type == 'EXTERNAL') {
  2292. $total_type = 'OUTER';
  2293. }
  2294. if ($send_order_info->promotion_type == 'INTERNAL') {
  2295. $total_type = 'INNER';
  2296. }
  2297. }
  2298. }
  2299. if (str_contains($new_total, 'custom')) {
  2300. $total_type = 'CUSTOM';
  2301. $temp = explode('_', $new_total);
  2302. if (isset($temp[1]) && is_numeric($temp[1])) {
  2303. $total_param_id = $temp[1];
  2304. }
  2305. }
  2306. if (str_contains($new_total, 'template')) {
  2307. $total_type = 'TEMPLATE';
  2308. $temp = explode('_', $new_total);
  2309. if (isset($temp[1]) && is_numeric($temp[1])) {
  2310. $total_param_id = $temp[1];
  2311. }
  2312. }
  2313. if ($total_type) {
  2314. OrderParamWsoService::createOrder(
  2315. $order_id,
  2316. $new_inner,
  2317. $new_outer,
  2318. $total_param_id,
  2319. $new_total,
  2320. $total_type
  2321. );
  2322. }
  2323. }
  2324. private function exchangeUpdateAndCreated($order_id)
  2325. {
  2326. /*try{
  2327. $info = DB::table('orders')->where('id',$order_id)->select('created_at','updated_at')->first();
  2328. DB::table('orders')->where('id',$order_id)
  2329. ->update([
  2330. 'created_at'=>$info->updated_at,
  2331. 'updated_at'=>$info->created_at
  2332. ]);
  2333. }catch (\Exception $e){}*/ }
  2334. private function notifyXiyue($uid, $price, $channel_id, $openid, $type = 'recharge')
  2335. {
  2336. if ($channel_id != 6985) return;
  2337. //$xiyue_user = DB::table('xiyue_zsy_users')->where('uid',$uid)->select('openid')->first();
  2338. $xiyue_host = env('XIYUE_HOST');
  2339. if ($xiyue_host) {
  2340. $client = new Client(['timeout' => 5]);
  2341. $url = $xiyue_host . '/order/success';
  2342. $param = ['openid' => $openid, 'price' => $price];
  2343. $param['sign'] = _sign($param, '8T5MJy4FsJoZspq8');
  2344. try {
  2345. $client->request('post', $url, ['form_params' => $param])->getBody()->getContents();
  2346. } catch (\Exception $e) { }
  2347. }
  2348. }
  2349. }