OrdersController.php 111 KB

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