OrdersController.php 108 KB

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