OrdersController.php 111 KB

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