OrdersController.php 110 KB

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