OrdersController.php 110 KB

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