12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706 |
- <?php
- namespace App\Http\Controllers\Wap\Pay;
- use App\Admin\Models\User;
- use App\Modules\Book\Services\ChapterService;
- use App\Modules\OfficialAccount\Services\OfficialAccountService;
- use App\Modules\Subscribe\Services\OrderParamService;
- use App\Modules\Subscribe\Services\SubstituteOrderService;
- use App\Modules\User\Services\ReadRecordService;
- use App\Modules\User\Services\UserDivisionCpcPropertyService;
- use Illuminate\Routing\Controller;
- use App\Libs\Pay\WechatPay;
- use Illuminate\Http\Request;
- use App\Modules\Subscribe\Services\YearOrderService;
- use App\Modules\Subscribe\Services\BookOrderService;
- use App\Modules\Subscribe\Services\OrderService;
- use App\Modules\Product\Services\ProductService;
- use App\Modules\Book\Services\BookConfigService;
- use App\Modules\User\Services\UserService;
- use App\Modules\Channel\Services\ChannelService;
- use Log;
- use DB;
- use Cookie;
- use Redis;
- use Hashids;
- use EasyWeChat\Foundation\Application;
- use EasyWeChat\Support\XML;
- use App\Modules\OfficialAccount\Services\ForceSubscribeService;
- use App\Jobs\SendTexts;
- use App\Jobs\SendStatisticsList;
- use App\Libs\AliSMS;
- use App\Modules\Activity\Services\ActivityService;
- use App\Modules\SendOrder\Services\SendOrderService;
- use GuzzleHttp\Client;
- class OrdersController extends Controller
- {
- /**
- * @apiDefine pay 支付
- */
- /**
- * 支付唤起入口
- * 功能:授权、请求支付参数、并跳转微信支付
- * TODO 暂时不创建订单
- * tail -f /var/www/ydy_wap_backend/storage/logs/laravel-2017-12-01.log
- * zwap/goToPay?distribution_channel_id=3&price=1&uid=3
- * @apiVersion 1.0.0
- * @apiDescription 支付
- * @api {get} http://pay.aizhuishu.com/goToPay 支付
- * @apiGroup pay
- * @apiName wxindex
- * @apiParam {Int} product_id product_id
- * @apiParam {Int} uid uid
- * @apiParam {Int} distribution_channel_id distribution_channel_id
- * @apiParam {String} pay_redirect_url pay_redirect_url
- * @apiSuccess {int} code 状态码
- * @apiSuccessExample {json} Success-Response:
- * HTTP/1.1 200 OK
- * {
- * code: 0,
- * msg: "",
- * data: {
- * }
- */
- function wxindex(Request $request)
- {
- $product_id = $request->has('product_id') ? $request->get('product_id') : '';
- $uid = $request->has('uid') ? $request->get('uid') : '';
- $distribution_channel_id = $request->has('distribution_channel_id') ? $request->get('distribution_channel_id') : '';
- $p_channel_id = $request->has('p_channel_id') ? $request->get('p_channel_id') : 0;
- $pay_redirect_url = $request->has('pay_redirect_url') ? $request->get('pay_redirect_url') : '';
- $send_order_id = $request->has('send_order_id') ? $request->get('send_order_id') : 0;
- $activity_id = $request->has('activity_id') ? $request->get('activity_id') : 0;
- $ip = $request->has('ip') ? $request->get('ip') : '';
- $n = $request->has('n') ? $request->get('n') : 0;
- $crm = $request->has('crm') ? $request->get('crm') : 0;
- if ($send_order_id && strlen($send_order_id) > 50) {
- try {
- $send_order_id = decrypt($send_order_id);
- } catch (\Exception $e) {
- $send_order_id = 0;
- }
- }
- if ($send_order_id && strlen($send_order_id) > 50) {
- $send_order_id = 0;
- }
- //活动判断
- if ($activity_id) {
- $limit = $request->get('limit', 0);
- if ($limit && $limit == 1) {
- $order = OrderService::userIsParticipateActivity($uid, $activity_id);
- if ($order) {
- $activity = ActivityService::getById($activity_id);
- if ($activity) {
- $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com' . $activity->activity_page;
- return redirect()->to($url);
- }
- return '';
- }
- }
- }
- $suid = $request->get('suid', 0);
- if ($suid) {
- $user_info = UserService::getById($uid);
- $distribution_channel_id = $user_info->distribution_channel_id;
- $pay_redirect_url = 1;
- }
- if (empty($product_id) || empty($uid) || empty($distribution_channel_id) || empty($pay_redirect_url)) {
- return response()->error('WAP_PARAM_ERROR');
- }
- if ($suid && $suid == $uid) {
- return back();
- }
- $openid = $request->has('openid') ? $request->get('openid') : '';
- $bid = $request->has('bid') ? $request->get('bid') : 0;
- $hash_bid = $bid;
- $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from');
- if ($fromtype && strlen($fromtype) > 50) {
- try {
- $fromtype = decrypt($fromtype);
- } catch (\Exception $e) {
- $fromtype = 'main';
- }
- }
- if ($fromtype && strlen($fromtype) > 50) {
- $fromtype = 'main';
- }
- if (!$openid && !$p_channel_id && !$suid) {
- $transfer_channel_id = $this->transfer($distribution_channel_id);
- if ($transfer_channel_id) {
- $p_channel_id = $distribution_channel_id;
- $distribution_channel_id = $transfer_channel_id;
- }
- }
- //根据分校id获取支付配置id
- Log::info($request->all());
- $channel = ChannelService::getById($distribution_channel_id);
- if (!$channel || !$channel->pay_merchant_id) {
- return response()->error('WAP_PARAM_ERROR');
- }
- //获取支付类型
- $pay_merchant = DB::table('pay_merchants')->select('id', 'appid', 'source', 'config_info')->where('id', $channel->pay_merchant_id)->where('is_enabled', 1)->first();
- if (!$pay_merchant || !$pay_merchant->appid || !$pay_merchant->source) return response()->error('WAP_PARAM_ERROR');
- if ($pay_merchant->source == 'PALMPAYV2') {
- return redirect()->to($this->paympay($request, $pay_merchant));
- }
- //重定向 获取用户信息的次数
- $n++;
- if ($n >= 5) {
- //重定向次数过多,则授权公众号出问题
- $this->payAlert($channel->pay_merchant_id, '', '', $n);
- $back_url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com' . '/pay';
- return redirect()->to($back_url);
- }
- if (empty($openid)) {
- $trade_no = date("YmdHis") . str_shuffle(hexdec(uniqid()));
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } else {
- $ip = get_client_ip();
- }
- $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');
- $redirect_url = env('CREATE_PAY_URL') . '?' . http_build_query($params);
- //$redirect_url = env('CREATE_PAY_URL').'?uid='.$uid.'&product_id='.$product_id.'&distribution_channel_id='
- // .$distribution_channel_id.'&send_order_id='.$send_order_id.'&bid='.$bid.'&pay_redirect_url='.urlencode($pay_redirect_url)
- $auth_url = env('AUTH_URL') . '?gzh_app_id=' . $pay_merchant->appid . '&redirect_url=' . urlencode($redirect_url);
- Log::info('redirect_auth_url:' . $auth_url);
- header("Location:" . $auth_url);
- exit();
- }
- $trade_no = $request->input('trade_no');
- $order_info = OrderService::getByTradeNo($trade_no);
- if ($order_info) return response()->error('WAP_SYS_ERROR');
- $cid = $request->has('cid') ? $request->get('cid') : '';
- $product_info = ProductService::getProductSingle($product_id, false);
- //新用户只能冲一次
- //Log::info('新用户只能冲一次:' .$product_info->type);
- if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
- $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
- return redirect()->to($url);
- }
- $price = $product_info->price * 100;
- if ($uid < 32) {
- $price = 1;
- }
- try {
- $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
- } catch (\Exception $e) {
- }
- $this->updateUserSendOrderId($uid, $send_order_id);
- if (in_array($uid, explode(',', env('TEST_UID')))) {
- $price = 1;
- }
- if ($bid) {
- try {
- $bid = Hashids::decode($bid)[0];
- } catch (\Exception $e) {
- $bid = 0;
- }
- }
- try {
- $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
- Redis::hset($key, $uid, time());
- $date = date('Y-m-d');
- Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
- if ($send_order_id) {
- Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
- Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
- }
- } catch (\Exception $e) {
- }
- $data = [];
- $data['price'] = $price;
- $data['create_ip'] = $ip;
- $data['openid'] = $openid;//
- $data['body'] = 'novel read';
- $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
- if ($official_name) {
- $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
- }
- $data['detail'] = 'novel read';
- $data['trade_no'] = $trade_no;
- $data['remark'] = json_encode(
- [
- 'uid' => $uid,
- 'dcd' => $distribution_channel_id,
- 'bid' => $bid,
- 'cp' => $ip,
- 'pms' => $pay_merchant->source,
- 'pmi' => $channel->pay_merchant_id,
- 'pd' => $product_id,
- 'soi' => $send_order_id,
- ]
- );
- if ($product_info->type == 'YEAR_ORDER') {
- $order_type = 'YEAR';
- } elseif ($product_info->type == 'BOOK_ORDER') {
- $order_type = 'BOOK';
- } elseif ($product_info->type == 'TICKET_RECHARGE') {
- $order_type = 'RECHARGE';
- } elseif ($product_info->type == 'NEW_USER') {
- $order_type = 'RECHARGE';
- } else {
- $order_type = 'UNKNOWN';
- }
- /*
- $this->createOrderTotal([
- 'distribution_channel_id'=>$distribution_channel_id,
- 'uid'=>$uid,
- 'product_id'=>$product_id,
- 'price'=>$price/100,
- 'pay_type'=>1,
- 'trade_no'=>$data['trade_no'],
- 'pay_merchant_source'=>$pay_merchant->source,
- 'pay_merchant_id'=>$channel->pay_merchant_id,
- 'create_ip'=>$request->getClientIp(),
- 'send_order_id'=>$send_order_id,
- 'order_type'=>$order_type,
- 'from_bid'=>$bid
- ]);*/
- $send_order_name = '';
- if ($send_order_id) {
- $send_order_info = SendOrderService::getById($send_order_id);
- if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
- $send_order_name = $send_order_info->name;
- }
- if(redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0) == $distribution_channel_id){
- $send_order_id = -$send_order_id;
- }
- }
- if ($suid) {
- $user_info = UserService::getById($uid);
- $distribution_channel_id = $user_info->distribution_channel_id;
- }
- $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
- $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
- $init_order = [
- 'distribution_channel_id' => $distribution_channel_id,
- 'uid' => $uid,
- 'product_id' => $product_id,
- 'price' => $price / 100,
- 'pay_type' => 1,
- 'trade_no' => $data['trade_no'],
- 'pay_merchant_source' => $pay_merchant->source,
- 'pay_merchant_id' => $channel->pay_merchant_id,
- 'create_ip' => $ip,
- 'send_order_id' => $send_order_id,
- 'send_order_name' => $send_order_name,
- 'order_type' => $order_type,
- 'from_bid' => $from_bid,
- 'from_type' => $fromtype,
- 'activity_id' => $activity_id,
- 'inner_send_order_id' => $inner_send_order_id
- ];
- $result = $this->createUnPayOrder($init_order);
- //订单创建成功后增加统计
- if ($result) {
- $this->orderCreated($init_order);
- $this->recordOtherParam($result->id, $uid);
- if($p_channel_id){
- //miwa 转换
- OrderParamService::createByParam([
- 'order_id'=>$result->id,'gxhp'=>'mw_transfer','appid'=>'','bid'=>$bid,'data_hour_key'=>'','times'=>'','distribution_channel_id'=>$p_channel_id
- ]);
- }
- if($crm){
- OrderParamService::createByParam([
- 'order_id'=>$result->id,'gxhp'=>'crm','appid'=>'','bid'=>$bid,'data_hour_key'=>$crm,'times'=>'','distribution_channel_id'=>$distribution_channel_id
- ]);
- }
- }
- if ($request->get('suid')) {
- $this->createSubstituteOrder($result->id, $uid, $request->get('suid'));
- }
- Log::info($data);
- $config = [];
- if ($pay_merchant->config_info) {
- $config = json_decode($pay_merchant->config_info, true);
- }
- $wechatPay = WechatPay::instance($pay_merchant->source, $config);
- if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
- $pay_info = $wechatPay->send($data);
- if (!$pay_info) {
- $pay_info = $wechatPay->send($data);
- }
- if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
- //支付异常
- myLog('order')->info('pay error start---------------------------------');
- myLog('order')->info($pay_info);
- myLog('order')->info($request->all());
- myLog('order')->info('order info is');
- myLog('order')->info($init_order);
- myLog('order')->info('pay error end-----------------------------------');
- $this->payAlert($channel->pay_merchant_id, $trade_no, $pay_info);
- }
- $pay_info['pay_redirect_url'] = urldecode($pay_redirect_url);
- if ($request->has('cid')) {
- $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&cid=' . $request->input('cid');
- }
- // if ($hash_bid) {
- // $pay_info['pay_redirect_url'] = $pay_info['pay_redirect_url'] . '&bid=' . $hash_bid;
- // }
- if ($suid) {
- $prize_fee = (int)(($product_info->price * 100) * 0.1);
- $help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
- $help_pay_page_channel_id = $distribution_channel_id;
- $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
- $pay_info['pay_redirect_url'] = $pay_redirect_url = sprintf(
- $url_format,
- env('PROTOCOL'),
- encodeDistributionChannelId($help_pay_page_channel_id),
- env('CUSTOM_HOST'),
- $prize_fee,
- $uid
- );
- }
- $pay_url_info = parse_url($pay_redirect_url);
- $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']);
- //$h5_scheme = env('H5_SCHEME','https');
- //$jsSdkSign = $this->jsSdkSign($pay_info,str_replace('http',$h5_scheme,url()->current()));
- Log::info('$pay_info is');
- $jsSdkSign = 0;
- Log::info($pay_info);
- Log::info('jsSdkSign---- :' . $jsSdkSign);
- $pay_order = $trade_no;
- // 注册动作-》创建订单
- $action_type = 'CreateOrder';
- $param = [
- 'openid' => isset($openid) ? $openid : '0',
- 'uid' => isset($uid) ? $uid : '0',
- 'order_sn' => isset($trade_no) ? $trade_no : '0',
- 'amount' => isset($price) ? $price : '0',
- ];
- UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
- return view('pay.order.index', compact('pay_info', 'referer', 'jsSdkSign', 'pay_order'));
- }
- private function paympay(Request $request, $pay_merchant)
- {
- $product_id = $request->get('product_id');
- $uid = $request->get('uid');
- $distribution_channel_id = $request->get('distribution_channel_id');
- $pay_redirect_url = $request->get('pay_redirect_url');
- $send_order_id = $request->get('send_order_id', 0);
- $activity_id = $request->get('activity_id', 0);
- $suid = $request->get('suid', 0);
- $bid = $request->has('bid') ? $request->get('bid') : 0;
- $crm = $request->has('crm') ? $request->get('crm') : '';
- $hash_bid = $bid;
- $fromtype = $request->has('fromtype') ? $request->get('fromtype') : $request->get('from', 'main');
- //根据分校id获取支付配置id
- Log::info($request->all());
- $trade_no = $trade_no = date("YmdHis") . str_shuffle(hexdec(uniqid()));
- $order_info = OrderService::getByTradeNo($trade_no);
- if ($order_info) return response()->error('WAP_SYS_ERROR');
- $cid = $request->has('cid') ? $request->get('cid') : '';
- $product_info = ProductService::getProductSingle($product_id, false);
- //新用户只能冲一次
- //Log::info('新用户只能冲一次:' .$product_info->type);
- if ($this->isNewUserSecondCharge($product_info->type, $product_id, $uid)) {
- $url = env('PROTOCOL', 'https') . '://site' . encodeDistributionChannelId($distribution_channel_id) . '.' . env('CUSTOM_HOST', 'leyuee') . '.com/pay';
- return redirect()->to($url);
- }
- $price = $product_info->price * 100;
- if ($uid < 32) {
- $price = 1;
- }
- if (!$send_order_id) {
- try {
- $send_order_id = (int)Redis::hget('book_read:' . $uid, 'send_order_id');
- } catch (\Exception $e) {
- }
- }
- $this->updateUserSendOrderId($uid, $send_order_id);
- if (in_array($uid, explode(',', env('TEST_UID')))) {
- $price = 1;
- }
- try {
- $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
- Redis::hset($key, $uid, time());
- $date = date('Y-m-d');
- Redis::hincrby('order_stat:' . $distribution_channel_id, 'order_num_' . $date, 1);
- if ($send_order_id) {
- Redis::hincrby('order_promotion_stat:' . $send_order_id, 'order_num_' . $date, 1);
- Redis::hincrby('order_promotion_stat:' . $send_order_id, 'total', 1);
- }
- } catch (\Exception $e) {
- }
- if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR']) {
- $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
- } else {
- $ip = get_client_ip();
- }
- $data = [];
- $data['price'] = $price;
- $data['create_ip'] = $ip;
- $data['body'] = 'novel read';
- $official_name = $this->getSubscribeOfficialName($uid, $distribution_channel_id);
- if ($official_name) {
- $data['body'] = '搜索公众号' . $official_name . ',请继续阅读';
- }
- $data['detail'] = 'novel read';
- $data['trade_no'] = $trade_no;
- if ($product_info->type == 'YEAR_ORDER') {
- $order_type = 'YEAR';
- } elseif ($product_info->type == 'BOOK_ORDER') {
- $order_type = 'BOOK';
- } elseif ($product_info->type == 'TICKET_RECHARGE') {
- $order_type = 'RECHARGE';
- } elseif ($product_info->type == 'NEW_USER') {
- $order_type = 'RECHARGE';
- } else {
- $order_type = 'UNKNOWN';
- }
- $send_order_name = '';
- if ($send_order_id) {
- $send_order_info = SendOrderService::getById($send_order_id);
- if ($send_order_info && isset($send_order_info->name) && !empty($send_order_info->name)) {
- $send_order_name = $send_order_info->name;
- }
- }
- if ($suid) {
- $user_info = UserService::getById($uid);
- $distribution_channel_id = $user_info->distribution_channel_id;
- }
- $from_bid = ReadRecordService::getSimpleFirstReadRecord($uid);
- $inner_send_order_id = ReadRecordService::getInnerSendOrderId($uid);
- $init_order = [
- 'distribution_channel_id' => $distribution_channel_id,
- 'uid' => $uid,
- 'product_id' => $product_id,
- 'price' => $price / 100,
- 'pay_type' => 1,
- 'trade_no' => $data['trade_no'],
- 'pay_merchant_source' => 'PALMPAY',
- 'pay_merchant_id' => $pay_merchant->id,
- 'create_ip' => $ip,
- 'send_order_id' => $send_order_id,
- 'send_order_name' => $send_order_name,
- 'order_type' => $order_type,
- 'from_bid' => $from_bid,
- 'from_type' => $fromtype,
- 'activity_id' => $activity_id,
- 'inner_send_order_id' => $inner_send_order_id
- ];
- $result = $this->createUnPayOrder($init_order);
- //订单创建成功后增加统计
- if ($result) {
- $this->orderCreated($init_order);
- $this->recordOtherParam($result->id, $uid);
- if($crm){
- OrderParamService::createByParam([
- 'order_id'=>$result->id,'gxhp'=>'crm','appid'=>'','bid'=>$bid,'data_hour_key'=>$crm,'times'=>'','distribution_channel_id'=>$distribution_channel_id
- ]);
- }
- }
- if ($suid) {
- $this->createSubstituteOrder($result->id, $uid, $suid);
- }
- if ($suid) {
- $prize_fee = (int)(($product_info->price * 100) * 0.1);
- //$help_pay_page_channel_id = env('HELP_PAY_PAGE_CHANNEL_ID', 123);
- $help_pay_page_channel_id = $distribution_channel_id;
- $url_format = '%s://site%s.%s.com/helppay?back=%s&su=%s';
- $pay_redirect_url = sprintf(
- $url_format,
- env('PROTOCOL'),
- encodeDistributionChannelId($help_pay_page_channel_id),
- env('CUSTOM_HOST'),
- $prize_fee,
- $uid
- );
- }
- $de_pay_redirect_url = urldecode($pay_redirect_url);
- /*if ($request->has('cid')) {
- $de_pay_redirect_url = $de_pay_redirect_url . '&cid=' . $request->get('cid');
- }*/
- $pay_url_info = parse_url($de_pay_redirect_url);
- $data['pay_wait_url'] = $pay_url_info['scheme'] . '://' . $pay_url_info['host'] . '/pay/waitv2?order=' . $data['trade_no'] . '&redirect=' . urlencode($de_pay_redirect_url);
- Log::info($data);
- $config = [];
- if ($pay_merchant->config_info) {
- $config = json_decode($pay_merchant->config_info, true);
- }
- $wechatPay = WechatPay::instance($pay_merchant->source, $config);
- \Log::info('$wechatPay is');
- //\Log::info($wechatPay);
- if (!$wechatPay) return response()->error('WAP_PARAM_ERROR');
- \Log::info('$wechatPay ok-------------------');
- $pay_info = $wechatPay->send($data);
- \Log::info('send ok-------------------');
- // 注册动作-》创建订单
- $action_type = 'CreateOrder';
- $param = [
- 'openid' => isset($openid) ? $openid : '0',
- 'uid' => isset($uid) ? $uid : '0',
- 'order_sn' => isset($trade_no) ? $trade_no : '0',
- 'amount' => isset($price) ? $price : '0',
- ];
- \Log::info('FUCK_PALMPAYV2');
- UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
- return $pay_info;
- }
- public function waitPageV2(Request $request)
- {
- $order = $request->get('order');
- $order = (string)$order;
- $url = urldecode($request->get('redirect'));
- if (substr_count($url, '?') > 1) {
- $url = str_replace_last('?', '&', $url);
- }
- $url_info = parse_url($url);
- if (isset($url_info['query']) && !empty($url_info['query'])) {
- parse_str($url_info['query'], $path);
- if (isset($path['success']) && $path['success'] == 0) {
- if (isset($url_info['path']) && str_contains($url_info['path'], 'reader') && isset($path['cid']) && !empty($path['cid'])) {
- $chapter_info = ChapterService::getChapterNameByIdNoCheck($path['cid']);
- $path['cid'] = $chapter_info->prev_cid;
- $url = sprintf('%s://%s/reader?%s', $url_info['scheme'], $url_info['host'], http_build_query($path));
- }
- return redirect($url);
- }
- }
- if (isset($url_info['path']) && str_contains($url_info['path'], 'pay')) {
- $url = sprintf('%s://%s/recent', $url_info['scheme'], $url_info['host']);
- return redirect($url);
- }
- return view('pay.order.wait', compact('order', 'url'));
- }
- private function isNewUserSecondCharge($charge_type, $product_id, $uid)
- {
- //Log::info('新用户只能冲一次:' .$charge_type);
- if ($charge_type == 'NEW_USER') {
- $result = OrderService::getUserOrderByProductId($uid, $product_id);
- //Log::info('isNewUserSecondCharge-------------------------');
- //Log::info($result);
- if ($result) {
- return true;
- }
- }
- return false;
- }
- //订单创建后统计
- private function orderCreated($init_order)
- {
- try {
- if (isset($init_order['pay_merchant_id'])) {
- $key = 'pay_merchant:' . $init_order['pay_merchant_id'];
- Redis::hincrby($key, 'unpaid_num', 1);
- Redis::hset($key, 'last_create_time', time());
- }
- } catch (\Exception $e) {
- }
- }
- //订单回调后统计
- private function orderPaid($init_order)
- {
- try {
- if (isset($init_order['pay_merchant_id'])) {
- $key = 'pay_merchant:' . $init_order['pay_merchant_id'];
- Redis::hset($key, 'unpaid_num', 0);
- }
- } catch (\Exception $e) {
- }
- }
- public function reportError(Request $request)
- {
- $data = $request->post('data');
- DB::table('pay_page_error')->insert([
- 'msg' => $data,
- 'created_at' => date('Y-m-d H:i:s'),
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- return response()->success();
- }
- private function jsSdkSign($pay_info, $url)
- {
- if (!isset($pay_info['appId']) || !isset($pay_info['package'])) {
- return 0;
- }
- $appid = $pay_info['appId'];
- $jsapi_ticket = Redis::hget($appid, 'jsapi_ticket');
- //如果能获取到jsapi_ticket 则直接签名
- if ($jsapi_ticket) {
- $sign_arr = [
- 'noncestr' => $pay_info['nonceStr'],
- 'jsapi_ticket' => $jsapi_ticket,
- 'timestamp' => $pay_info['timeStamp'],
- 'url' => $url
- ];
- return sha1($this->arr_to_url($sign_arr));
- }
- //获取不到jsapi_ticket
- $app_secret_info = DB::table('official_setting')->select('secret')->first();
- if ($app_secret_info && !empty($app_secret_info->secret)) {
- $client = new Client(['timeout' => 5]);
- try {
- //获取access_token
- $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $appid . '&secret=' . $app_secret_info->secret;
- $res = $client->request('get', $url)->getBody()->getContents();
- if (!$res) {
- return 0;
- }
- $res = json_decode($res, 1);
- if (!isset($res['access_token']) || empty($res['access_token'])) {
- return 0;
- }
- Redis::hset($appid, 'access_token', $res['access_token']);
- //获取jsapi_ticket
- $res = null;
- $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $res['access_token'] . '&type=jsapi';
- $res = $client->request('get', $url)->getBody()->getContents();
- if (!$res) {
- return 0;
- }
- $res = json_decode($res, 1);
- if (!isset($res['ticket']) || empty($res['ticket'])) {
- return 0;
- }
- Redis::hset($appid, 'jsapi_ticket', $res['ticket']);
- Redis::EXPIRE($appid, 7000);
- $sign_arr = [
- 'noncestr' => $pay_info['nonceStr'],
- 'jsapi_ticket' => $res['ticket'],
- 'timestamp' => $pay_info['timeStamp'],
- 'url' => $url
- ];
- return sha1($this->arr_to_url($sign_arr));
- } catch (\Exception $e) {
- return 0;
- }
- }
- return 0;
- }
- public function waitPage(Request $request)
- {
- $order = $request->input('order');
- //$order = "201712081711401585932843356442";
- $order = (string)$order;
- $url = urldecode($request->input('redirect'));
- return view('pay.order.wait', compact('order', 'url'));
- //return view('pay.order.wait');
- }
- /**
- * 官方微信回调
- * @param Request $request
- * @return \Symfony\Component\HttpFoundation\Response
- */
- function wcback_official(Request $request)
- {
- libxml_disable_entity_loader(true);
- $xml = XML::parse(strval($request->getContent()));
- Log::info('xml is');
- Log::info($xml);
- if (!$xml || !is_array($xml)) return 'fail';
- $remarks = json_decode($xml['attach'], true);
- $pay_merchant_id = $remarks['pmi'];
- $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
- $application = WechatPay::instance('OFFICIALPAY', json_decode($pay_merchant->config_info, true));
- //$app = new Application($options);
- $app = $application->app;
- $response = $app->payment->handleNotify(function ($notify, $successful) {
- if (!$successful) return 'fail';
- $trade_no = $notify->out_trade_no;
- $order = OrderService::getByTradeNo($trade_no);
- if (isset($order->status) && $order->status == 'PAID') {
- Log::info('has_pay:' . $trade_no);
- return true;
- }
- DB::beginTransaction();
- try {
- $transaction_id = $notify->transaction_id;
- $remarks = json_decode($notify->attach, true);
- $uid = $remarks['uid'];
- $distribution_channel_id = $remarks['dcd'];
- $product_id = $remarks['pd'];
- $product = ProductService::getProductSingle($product_id);
- $pay_merchant_source = $remarks['pms'];
- $pay_merchant_id = $remarks['pmi'];
- $send_order_id = $remarks['soi'];
- $create_ip = $remarks['cp'];
- $price = $product->price;
- $bid = $remarks['bid'];
- //$this->updateOrderTotal($trade_no,$transaction_id);
- $prize_fee = (int)(($product->price * 100) * 0.1);
- $this->substituteOrderPrice($order->id, $prize_fee);
- $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
- //获取用户充值次数
- $order->pay_type = $this->getChargeTimes($order->uid);
- // 更新其他定制Order表
- if ($product->type == 'YEAR_ORDER') {
- Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
- $order_type = 'YEAR';
- $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- //$price = $price/100;
- /*
- $this->createOrder(
- compact(
- 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
- 'pay_merchant_id','create_ip','bid','transaction_id'
- )
- );*/
- } elseif ($product->type == 'BOOK_ORDER') {
- Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
- $order_type = 'BOOK';
- $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- //$price = $price/100;
- /*
- $this->createOrder(
- compact(
- 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
- 'pay_merchant_id','create_ip','bid','transaction_id'
- )
- );*/
- } elseif ($product->type == 'TICKET_RECHARGE') {
- Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
- $order_type = 'RECHARGE';
- $this->userCharge($product, $uid);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- //$price = $price/100;
- /*
- $this->createOrder(
- compact(
- 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
- 'pay_merchant_id','create_ip','bid','transaction_id'
- ));*/
- } elseif ($product->type == 'FOREVER') {
- Log::info('FOREVER------------------------FOREVER');
- $order_type = 'FOREVER';
- $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } else {
- DB::rollback();
- return 'Order not exist.';
- }
- $this->successPayPushMsg($uid, $product, $order->id);
- $this->orderStatistical($order);
- $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
- Redis::hdel($key, $uid);
- DB::commit();
- // 注册动作-》回调订单
- $action_type = 'CallBackOrder';
- $param = [
- 'order_sn' => isset($trade_no) ? $trade_no : '0',
- 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
- ];
- UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
- //用户属性
- $this->userProperty($uid);
- return true;
- } catch (\Exception $e) {
- DB::rollback();
- return 'fail';
- }
- });
- return $response;
- }
- /**
- * 通联支付回调
- * @param Request $request
- */
- function wcback_allinpay(Request $request)
- {
- Log::info('wcback_allinpay_request');
- try {
- $data = $request->all();
- Log::info($request->all());
- if (!$data['trxreserved']) return 'fail';
- $pay_merchant_id = json_decode($data['trxreserved'], true)['pmi'];
- $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
- unset($data['_url']);
- if (!$pay_merchant->config_info) return 'fail';
- $wechatPay = WechatPay::instance('ALLINPAY', json_decode($pay_merchant->config_info, true));
- if ($wechatPay->NotifyValidSign($data, $wechatPay->appkey)) {
- Log::info('allinpay_notify_sign_success');
- /**
- * 只有0000表示交易成功或下单成功,其他为失败
- * 0000:交易成功
- * 3045,3088:交易超时
- * 3008:余额不足
- * 3999:交易失败
- * 2008:交易处理中
- * 3050:交易已撤销
- */
- if ($data['trxstatus'] == '0000') {
- // 修改表比较多,开启事务
- DB::beginTransaction();
- $trade_no = $data['cusorderid'];
- $transaction_id = $data['chnltrxid'];
- //$openid = $data['acct'];
- $remarks = $data['trxreserved'];
- // $remarks = '{"openid":"oEteU1VNvYozhXuu8TXhByPBtSl4","distribution_channel_id":2,"product_id":1,"uid":1,"price":"1","trade_no":"201711301937151585490779316114","create_ip":"::1","servicer":"AllinPay"}';
- $remarks = json_decode($remarks, true);
- Log::info('$remarks');
- Log::info($remarks);
- // 更新Order
- $order = OrderService::getByTradeNo($trade_no);
- // 防止重复推送
- if (isset($order->status) && $order->status == 'PAID') {
- Log::info('has_pay:' . $trade_no);
- echo 'success';
- exit();
- }
- $uid = $remarks['uid'];
- $distribution_channel_id = $remarks['dcd'];
- $product_id = $remarks['pd'];
- $product = ProductService::getProductSingle($product_id);
- $pay_merchant_source = $remarks['pms'];
- $pay_merchant_id = $remarks['pmi'];
- $send_order_id = $remarks['soi'];
- $create_ip = $remarks['cp'];
- $price = $product->price;
- $bid = $remarks['bid'];
- Log::info('save_order_end');
- //$this->updateOrderTotal($trade_no,$transaction_id);
- Log::info($product);
- Log::info('product_type:' . $product->type);
- $prize_fee = (int)(($product->price * 100) * 0.1);
- $this->substituteOrderPrice($order->id, $prize_fee);
- $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
- //获取用户充值次数
- $order->pay_type = $this->getChargeTimes($order->uid);
- // 更新其他定制Order表
- if ($product->type == 'YEAR_ORDER') {
- Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
- $order_type = 'YEAR';
- $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- //$price = $price/100;
- /*
- $this->createOrder(
- compact(
- 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
- 'pay_merchant_id','create_ip','bid','transaction_id'
- )
- );*/
- } elseif ($product->type == 'BOOK_ORDER') {
- Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
- $order_type = 'BOOK';
- $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- //$price = $price/100;
- /*
- $this->createOrder(
- compact(
- 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
- 'pay_merchant_id','create_ip','bid','transaction_id'
- )
- );*/
- } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
- Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
- $order_type = 'RECHARGE';
- $this->userCharge($product, $uid);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- //$price = $price/100;
- /*
- $this->createOrder(
- compact(
- 'uid','distribution_channel_id','product_id','price','trade_no','send_order_id','order_type','pay_merchant_source',
- 'pay_merchant_id','create_ip','bid','transaction_id'
- ));*/
- } elseif ($product->type == 'FOREVER') {
- Log::info('FOREVER------------------------FOREVER');
- $order_type = 'FOREVER';
- $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- }
- $this->successPayPushMsg($uid, $product, $order->id);
- $this->orderStatistical($order);
- DB::commit();
- $this->userProperty($uid);
- //redis 删除未支付的uid
- try {
- $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
- Redis::hdel($key, $uid);
- //Redis::srem($key,$uid);
- } catch (\Exception $e) {
- Log::info('redis remote error-----------------------');
- Log::info($e);
- }
- }
- } else {
- Log::info('allinpay_notify_sign_fail');
- }
- } catch (\Exception $e) {
- DB::rollBack();
- Log::info('receive_allinpay_ept:' . $e->getMessage());
- Log::info('pay_callback_end fail');
- echo 'fail';
- return;
- }
- Log::info('pay_callback_end');
- echo "success";
- }
- public function wcback_lianlianpay(Request $request)
- {
- $data = $request->getContent();
- Log::info('wcback_lianlianpay call back enter');
- Log::info($request->getContent());
- $data = json_decode($data, true);
- if ($data['result_pay'] == 'SUCCESS') {
- $trade_no = $data['no_order'];
- $order = OrderService::getByTradeNo($trade_no);
- if (!$order) return response()->json(['ret_code' => '-1']);
- $pay_merchant_id = $order->pay_merchant_id;
- $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $pay_merchant_id)->where('is_enabled', 1)->first();
- unset($data['_url']);
- if (!$pay_merchant->config_info) response()->json(['ret_code' => '-1']);
- $wechatPay = WechatPay::instance('LIANLIANPAY', json_decode($pay_merchant->config_info, true));
- $uid = $order->uid;
- $distribution_channel_id = $order->distribution_channel_id;
- $price = $order->price;
- $send_order_id = $order->send_order_id;
- $product_id = $order->product_id;
- if (!$wechatPay->rsaCheck($data)) {
- Log::info('sign check error');
- //return response()->json(['ret_code'=>'-1']);
- }
- // 防止重复推送
- if (isset($order->status) && $order->status == 'PAID') {
- Log::info('has_pay:' . $trade_no);
- return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
- }
- DB::beginTransaction();
- try {
- $product = ProductService::getProductSingle($order->product_id);
- $transaction_id = $data['oid_paybill'];
- $prize_fee = (int)(($product->price * 100));
- $this->substituteOrderPrice($order->id, $prize_fee);
- $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
- //获取用户充值次数
- $order->pay_type = $this->getChargeTimes($order->uid);
- // 更新其他定制Order表
- if ($product->type == 'YEAR_ORDER') {
- Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
- $order_type = 'YEAR';
- $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'BOOK_ORDER') {
- Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
- $order_type = 'BOOK';
- $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
- Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
- $order_type = 'RECHARGE';
- $this->userCharge($product, $uid);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'FOREVER') {
- Log::info('FOREVER------------------------FOREVER');
- $order_type = 'FOREVER';
- $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- }
- $this->successPayPushMsg($uid, $product, $order->id);
- $this->orderStatistical($order);
- DB::commit();
- $this->userProperty($uid);
- //redis 删除未支付的uid
- $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id' . $distribution_channel_id;
- Redis::hdel($key, $uid);
- return response()->json(['ret_code' => '0000', 'ret_msg' => '交易成功']);
- } catch (\Exception $e) {
- DB::rollBack();
- Log::info('lianlian:' . $e->getMessage());
- echo 'fail';
- return;
- }
- }
- echo 'success';
- }
- /**
- * 微众支付回调
- * @param Request $request
- */
- function wcback_palmpay(Request $request)
- {
- Log::info('wcback_palmpay_request');
- try {
- $data = $request->except('_url');
- Log::info($request->all());
- //订单
- $trade_no = $data['outTradeNo'];
- $order = OrderService::getByTradeNo($trade_no);
- if (!$order) die('failed');
- // 防止重复推送
- if (isset($order->status) && $order->status == 'PAID') {
- Log::info('has_pay:' . $trade_no);
- echo 'success';
- exit();
- }
- $pay_merchant = DB::table('pay_merchants')->select('appid', 'source', 'config_info')->where('id', $order->pay_merchant_id)->where('is_enabled', 1)->first();
- //校验签名
- $sign = _sign($data, json_decode($pay_merchant->config_info, 1)['appKey']);
- if ($sign != $data['sign']) die('failed');
- $transaction_id = $data['chorderid'];
- if ($data['status'] == 'success') {
- Log::info('wcback_palmpay_notify_sign_success');
- if ($data['status'] == 'success') {
- // 修改表比较多,开启事务
- DB::beginTransaction();
- // 更新Order
- $order = OrderService::getByTradeNo($trade_no);
- $uid = $order->uid;
- $distribution_channel_id = $order->distribution_channel_id;
- $product_id = $order->product_id;
- $product = ProductService::getProductSingle($product_id);
- $send_order_id = $order->send_order_id;
- $price = $product->price;
- Log::info('save_order_end');
- Log::info($product);
- Log::info('product_type:' . $product->type);
- $prize_fee = (int)(($product->price * 100) * 0.1);
- $this->substituteOrderPrice($order->id, $prize_fee);
- $this->smartPushTestBookPaidUv($order->from_bid, $order->uid, $order->price);
- //获取用户充值次数
- $order->pay_type = $this->getChargeTimes($order->uid);
- // 更新其他定制Order表
- if ($product->type == 'YEAR_ORDER') {
- Log::info('YEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDERYEAR_ORDER');
- $order_type = 'YEAR';
- $this->yearOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'BOOK_ORDER') {
- Log::info('BOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDERBOOK_ORDER');
- $order_type = 'BOOK';
- $this->bookOrder($product_id, $uid, $send_order_id, $price, $distribution_channel_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'TICKET_RECHARGE' || $product->type == 'NEW_USER') {
- Log::info('TICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGETICKET_RECHARGE');
- $order_type = 'RECHARGE';
- $this->userCharge($product, $uid);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- } elseif ($product->type == 'FOREVER') {
- Log::info('FOREVER------------------------FOREVER');
- $order_type = 'FOREVER';
- $this->foreverFreeOrder($uid, $distribution_channel_id, $price, $send_order_id);
- $order->order_type = $order_type;
- $order->status = 'PAID';
- $order->pay_end_at = date('Y-m-d H:i:s');
- $order->transaction_id = $transaction_id;
- $order->save();
- }
- $this->successPayPushMsg($uid, $product, $order->id);
- $this->orderStatistical($order);
- DB::commit();
- //redis 删除未支付的uid
- try {
- $key = 'leyuee:to_send_not_pay_uid:distribution_channel_id:' . $distribution_channel_id;
- Redis::hdel($key, $uid);
- } catch (\Exception $e) {
- Log::info('redis remote error-----------------------');
- Log::info($e);
- }
- // 注册动作-》回调订单
- $action_type = 'CallBackOrder';
- $param = [
- 'order_sn' => isset($trade_no) ? $trade_no : '0',
- 'openid' => isset($uid) ? $uid : '0',// 没有openid,用uid写log
- ];
- UserService::PushUserActionToQueue($action_type, $distribution_channel_id, $param);
- $this->userProperty($uid);
- }
- } else {
- Log::info('wcback_palmpay_notify_sign_fail');
- }
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- DB::rollBack();
- Log::info('receive_wcback_palmpay_ept:' . $e->getMessage());
- echo 'fail';
- Log::info('pay_callback_end error');
- return;
- }
- Log::info('pay_callback_end');
- echo "success";
- }
- /**
- * 公众号签名@华灯初上
- * @param $params
- * @return string
- */
- protected function getSign($params, $key)
- {
- $url = $this->arr_to_url($params, false);
- $url = $url . '&key=' . $key;
- $sign = md5($url);
- return $sign;
- }
- /**
- * 公众号签名@华灯初上
- * @param $array
- * @param bool $has_sign
- * @return string
- */
- protected function arr_to_url($array, $has_sign = false)
- {
- ksort($array);
- reset($array);
- $arg = "";
- while (list ($name, $val) = each($array)) {
- if ($name == 'sign' && !$has_sign) continue;
- if (strpos($name, "_") === 0)
- continue;
- if (is_array($val))
- $val = join(',', $val);
- if ($val === "")
- continue;
- $arg .= $name . "=" . $val . "&";
- }
- $arg = substr($arg, 0, count($arg) - 2);
- return $arg;
- }
- /**
- * 单本充值会掉
- * @param $product_id
- * @param $uid
- * @param $send_order_id
- * @param $fee
- */
- protected function bookOrder($product_id, $uid, $send_order_id, $fee, $distribution_channel_id)
- {
- $book_conf = BookConfigService::getBookByProduct($product_id);
- $insert_data['bid'] = isset($book_conf->bid) ? $book_conf->bid : '';
- $insert_data['book_name'] = isset($book_conf->book_name) ? $book_conf->book_name : '';
- $insert_data['uid'] = $uid;
- $insert_data['distribution_channel_id'] = $distribution_channel_id;
- $insert_data['fee'] = $fee;
- $insert_data['send_order_id'] = $send_order_id;
- $insert_data['charge_balance'] = 0;
- $insert_data['reward_balance'] = 0;
- Log::info('start_save_book_order');
- Log::info($insert_data);
- return BookOrderService::save_book_order($insert_data);
- }
- /**
- * 包年
- * @param $uid
- * @param $distribution_channel_id
- * @param $fee
- * @param $send_order_id
- * @return mixed
- */
- protected function yearOrder($uid, $distribution_channel_id, $fee, $send_order_id)
- {
- Log::info('start_save_year_order');
- $insert_data['uid'] = $uid;
- $insert_data['distribution_channel_id'] = $distribution_channel_id;
- $insert_data['fee'] = $fee;
- $insert_data['send_order_id'] = $send_order_id;
- Log::info($insert_data);
- return YearOrderService::save_year_order($insert_data);
- }
- private function foreverFreeOrder($uid, $distribution_channel_id, $fee, $send_order_id)
- {
- Log::info('start_save_year_order');
- ('start_forever_Free_Order');
- $insert_data['uid'] = $uid;
- $insert_data['distribution_channel_id'] = $distribution_channel_id;
- $insert_data['fee'] = $fee;
- $insert_data['send_order_id'] = $send_order_id;
- Log::info($insert_data);
- return YearOrderService::foreverFree($insert_data);
- }
- /**
- * 用户充值
- * @param $product
- * @param $uid\
- */
- protected function userCharge($product, $uid)
- {
- $total = $product->price * 100 + $product->given;
- UserService::addBalance($uid, $total, $product->price * 100, $product->given);
- Log::info('update_user_balance_end:' . $uid . ' balance_add:' . $total);
- }
- /**
- * 添加订单
- * @param array $data
- * @return mixed
- */
- protected function createOrder(array $data)
- {
- $insert_data = array();
- $insert_data['uid'] = $data['uid'];
- $insert_data['distribution_channel_id'] = $data['distribution_channel_id'];
- $insert_data['product_id'] = $data['product_id'];
- $insert_data['price'] = $data['price'];
- $insert_data['status'] = 'PAID';
- $insert_data['pay_type'] = 1;
- $insert_data['trade_no'] = $data['trade_no'];
- $insert_data['send_order_id'] = $data['send_order_id'];
- $insert_data['order_type'] = $data['order_type'];
- $insert_data['pay_merchant_source'] = $data['pay_merchant_source'];
- $insert_data['pay_merchant_id'] = $data['pay_merchant_id'];
- $insert_data['transaction_id'] = $data['transaction_id'];
- $insert_data['pay_end_at'] = date('Y-m-d H:i:s');
- $insert_data['create_ip'] = $data['create_ip'];
- $insert_data['from_bid'] = $data['bid'];
- Log::info('createOrder');
- Log::info($insert_data);
- return OrderService::save_order($insert_data);
- }
- /**
- * 添加位置付订单
- * @param $data
- * @return mixed
- */
- protected function createUnPayOrder($data)
- {
- $data['status'] = 'UNPAID';
- $data['transaction_id'] = '';
- $data['pay_end_at'] = '0000-00-00 00:00:00';
- return OrderService::save_order($data);
- }
- /**
- * order total
- * @param $data
- *
- */
- protected function createOrderTotal($data)
- {
- $data['status'] = 'UNPAID';
- $data['created_at'] = date('Y-m-d H:i:s');
- $data['updated_at'] = date('Y-m-d H:i:s');
- DB::table('orders_total')->insert($data);
- }
- protected function updateOrderTotal($trade_no, $transaction_id)
- {
- $data['status'] = 'PAID';
- $data['transaction_id'] = $transaction_id;
- $data['pay_end_at'] = date('Y-m-d H:i:s');
- //DB::table('orders_total')->where('trade_no',$trade_no)->update($data);
- }
- protected function successPayPushMsg($uid, $product_info, $order_id)
- {
- try {
- $force_sub_info = $this->getSubscribeV2($uid);
- $data = UserService::getById($uid);
- if (!in_array($data->distribution_channel_id, [5, 123])) {
- //return false;
- }
- if ($force_sub_info) {
- //$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>";
- if (SubstituteOrderService::getByOrderId($order_id)) {
- $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>";
- } else {
- $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>";
- }
- if ($product_info->type == 'YEAR_ORDER') {
- $money_text = $product_info->price . '元(尊贵的年费VIP会员)';
- } elseif ($product_info->type == 'TICKET_RECHARGE') {
- if ($product_info->given) {
- $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币+赠送' . $product_info->given . '书币)';
- } else {
- $money_text = $product_info->price . '元(' . ($product_info->price * 100) . '书币)';
- }
- } else {
- $money_text = '未知';
- }
- $delay = 0;
- $url = env('PROTOCOL') . '://site' . encodeDistributionChannelId($data->distribution_channel_id) . '.' . env('CUSTOM_HOST') . '.com/continue';;
- $content = sprintf($content_format, $data->nickname ? $data->nickname : '匿名', $uid, $money_text, $url);
- foreach ($force_sub_info as $item) {
- $res['openid'] = $item->openid;
- $res['appid'] = $item->appid;
- $res['content'] = $content;
- $res['type'] = 'one_task';
- $res['send_time'] = date("Y-m-d H:i:s");
- $res['task_id'] = md5('pay_success_push');
- $send_data = array(
- 'send_time' => date("Y-m-d H:i:s"),
- 'data' => $res
- );
- dispatch((new SendTexts($send_data))->onConnection('rabbitmq')->delay($delay)->onQueue('send_texts_list'));
- }
- }
- } catch (\Exception $e) {
- Log::info('pay_success_push error');
- Log::info($e);
- }
- return true;
- }
- private function orderStatistical($order)
- {
- try {
- $send_data = array(
- 'type' => 'order_info',
- 'data' => $order->toArray()
- );
- $send = new SendStatisticsList($send_data);
- $job = ($send)->onConnection('rabbitmq')->delay(0)->onQueue('send_statistics_list');
- dispatch($job);
- } catch (\Exception $e) {
- Log::info($e);
- }
- //灵界的推送统计\App\Modules\Statistic\Services\WapVisitStatService::customerAllStats
- try {
- if ($order->from_type) {
- $pv = Redis::hget('push:distribution_channel_id:allpv', $order->from_type);
- if ($pv) {
- Redis::sadd('push:all:paidnum:from:' . $order->from_type, $order->uid);
- $old = Redis::hget('push:all:paidamount', $order->from_type);
- if ($old) {
- Redis::hset('push:all:paidamount', $order->from_type, $old + $order->price);
- } else {
- Redis::hset('push:all:paidamount', $order->from_type, $order->price);
- }
- }
- }
- if (isset($order->pay_merchant_id) && $order->pay_merchant_id)//临时统计队列清0 用于报警
- {
- $key = 'pay_merchant:' . $order->pay_merchant_id;
- Redis::hset($key, 'unpaid_num', 0);
- }
- } catch (\Exception $e) {
- }
- }
- private function getSubscribe($uid)
- {
- return ForceSubscribeService::forceSubscribeUsersByUid(['uid' => $uid]);
- }
- private function getSubscribeV2($uid)
- {
- return DB::table('force_subscribe_users')->where('uid', $uid)->where('is_subscribed', 1)->get();
- }
- public function getSubscribeOfficialName($uid, $distribution_channel_id)
- {
- $subscribe = $this->getSubscribe($uid);
- if ($subscribe && isset($subscribe->appid)) {
- $official = DB::table('official_accounts')->where('appid', $subscribe->appid)->select('nickname')->first();
- if ($official && isset($official->nickname)) {
- return $official->nickname;
- }
- }
- $res = OfficialAccountService::canUseOfficialAccountByChannelId(compact('distribution_channel_id'));
- if (isset($res->nickname) && !empty($res->nickname)) {
- return $res->nickname;
- }
- try {
- $official = DB::table('official_accounts')
- ->where('distribution_channel_id', $distribution_channel_id)
- ->where('is_auth', 1)
- ->orderBy('id', 'desc')
- ->select('nickname')->first();
- if ($official && isset($official->nickname)) {
- return $official->nickname;
- }
- } catch (\Exception $e) {
- }
- return '';
- }
- private function payAlert($pay_merchant_id, $trade_no = '', $pay_info = '', $n = 0)
- {
- $change_pay_id = 40;
- if ($pay_merchant_id == 40) {
- $change_pay_id = 9;
- }
- try {
- $time = (int)date('G');
- $change = false;
- if ($time <= 8 || $time >= 23) {
- if (!$pay_info && $pay_merchant_id != $change_pay_id) {
- DB::table('distribution_channels')->where('pay_merchant_id', $pay_merchant_id)->update(['pay_merchant_id' => $change_pay_id]);
- $change = true;
- }
- }
- //$number,$template_type,$param=array()
- $phone_arr = ['15868100210', '18072713392', '15088790066', '13858057394', '18668029091', '18668420256'];
- //$phone_arr = ['18668029091'];
- $param = ['pay_id' => $pay_merchant_id];
- if ($n) {
- $template_type = 'pay_channel_remind';
- //$content = '支付通道:' . $pay_merchant_id . ',获取授权信息失败';
- } else {
- $template_type = 'pay_channel_remind';
- //$content = '支付通道:' . $pay_merchant_id . ',订单号为' . $trade_no . '支付异常,异常信息:' . json_encode($pay_info);
- }
- if ($change) {
- $template_type = 'pay_channel_change';
- $param = ['pay_id' => $pay_merchant_id, 'new_pay_id' => $change_pay_id];
- }
- foreach ($phone_arr as $phone) {
- AliSMS::send($phone, $template_type, $param);
- }
- } catch (\Exception $e) {
- }
- }
- private function createSubstituteOrder($order_id, $uid, $pay_uid)
- {
- SubstituteOrderService::createOrder($order_id, $uid, $pay_uid);
- }
- private function recordOtherParam($order_id, $uid)
- {
- $gxhp = ReadRecordService::getByField($uid, 'gxhp');
- if ($gxhp) {
- OrderParamService::create($order_id, $gxhp);
- }
- }
- private function substituteOrderPrice($order_id, $prize_fee = 500)
- {
- SubstituteOrderService::SubstituteOrderPrize($order_id, $prize_fee);
- }
- private function updateUserSendOrderId(int $uid, $send_order_id)
- {
- if ($send_order_id && $uid) {
- $user = UserService::getById($uid);
- if ($user && !$user->send_order_id) {
- UserService::updateInfo($uid, compact('send_order_id'));
- }
- }
- }
- /**
- * 测书智能推送统计 付费用户数、充值金额
- * @param $bid
- * @param $uid
- * @param $price
- */
- private function smartPushTestBookPaidUv($bid, $uid, $price)
- {
- $smart_bid = ReadRecordService::getSmartPush($uid);
- if ($smart_bid && in_array($bid, $smart_bid)) {
- try {
- $key = 'smartPushTestBookPaidUv:bid:%s';
- Redis::sadd(sprintf($key, $bid), $uid);
- $now_amount = Redis::hget('smartPushTestBookPaidAmount', $bid);
- if (!$now_amount) {
- $now_amount = 0;
- }
- Redis::hset('smartPushTestBookPaidAmount', $bid, $now_amount + $price);
- } catch (\Exception $e) {
- }
- }
- }
- /**
- * 获取用户第几次充值
- * @param $uid
- * @return int
- */
- private function getChargeTimes($uid)
- {
- $count = OrderService::getUserChargeTimes($uid);
- return $count + 1;
- }
- //米玩 订单转移
- private function transfer($distribution_channel_id)
- {
- if (in_array($distribution_channel_id, explode(',', redisEnv('MIWAN_TRANSFER_CHANNEL_ID', 1)))) {
- $random = random_int(1, 100);
- $miwan_transfer_probability = redisEnv('MIWAN_TRANSFER_PROBABILITY', 0);
- $miwan_transfer_to_channel_id = redisEnv('MIWAN_TRANSFER_TO_CHANNEL_ID', 0);
- if ($miwan_transfer_to_channel_id && $random <= $miwan_transfer_probability) {
- return $miwan_transfer_to_channel_id;
- }
- }
- return 0;
- }
- private function userProperty($uid){
- try{
- $userproperty = UserDivisionCpcPropertyService::getUserSubscribeAndChargeInfoByUid($uid);
- if(!$userproperty) return ;
- UserDivisionCpcPropertyService::createorUpdate($userproperty);
- }catch (\Exception $e){}
- }
- }
|