OrdersController.php 110 KB

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