OrdersController.php 108 KB

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