OrdersController.php 111 KB

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