OrdersController.php 111 KB

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