OrdersController.php 108 KB

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