OrdersController.php 110 KB

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