OrdersController.php 105 KB

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