OrdersController.php 88 KB

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