123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- namespace App\Task;
- use App\Model\Premium\PremiumWalletTradeList;
- use App\Model\Premium\PremiumPlatformPackage;
- use App\Model\Premium\PremiumPlatformOrder;
- use App\Model\Premium\PremiumPlatform;
- use App\Service\RsaServices;
- use App\Library\Log;
- class HandleTgPremium
- {
- public function execute()
- {
- try {
- $data = PremiumWalletTradeList::from('premium_wallet_trade_list as a')
- ->join('premium_platform as b','a.transferto_address','b.receive_wallet')
- ->where('a.process_status',1)
- ->where('a.coin_name','usdt')
- ->select('a.rid','a.transferfrom_address','a.amount','b.rid as premium_platform_rid','b.platform_cookie','b.platform_hash','b.status','b.platform_name','b.platform_phrase','a.platform_order_rid')
- ->limit(100)
- ->get();
- if($data->count() > 0){
- foreach ($data as $k => $v) {
- $time = nowDate();
-
- if($v->platform_order_rid){
- $res = PremiumPlatformOrder::where('rid',$v->platform_order_rid)->where('status',1)->first();
- }else{
-
- $res = PremiumPlatformOrder::where('premium_platform_rid',$v->premium_platform_rid)->where('need_pay_usdt',$v->amount)->where('status',0)->first();
- }
-
- if(empty($res)){
- $save_data = [];
- $save_data['process_status'] = 7;
- $save_data['process_comments'] = '金额无对应订单';
- $save_data['process_time'] = $time;
- $save_data['premium_platform_rid'] = $v->premium_platform_rid;
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }else{
- $save_data = [];
- $save_data['status'] = 1;
- PremiumPlatformOrder::where('rid',$res['rid'])->update($save_data);
- }
- if($v->status == 1){
- $save_data = [];
- $save_data['process_status'] = 6;
- $save_data['process_comments'] = '会员平台未启用';
- $save_data['process_time'] = $time;
- $save_data['premium_platform_rid'] = $v->premium_platform_rid;
- $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }
-
- if(empty($v->platform_cookie) || empty($v->platform_hash) || empty($v->platform_phrase)){
- $save_data = [];
- $save_data['process_status'] = 5;
- $save_data['process_comments'] = '会员平台未配置cookie或hash或助记词';
- $save_data['process_time'] = $time;
- $save_data['premium_platform_rid'] = $v->premium_platform_rid;
- $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }
-
- $rsa_services = new RsaServices();
-
- $signstr = $rsa_services->privateDecrypt($v->platform_cookie);
-
- if(empty($signstr)){
- $save_data = [];
- $save_data['process_status'] = 5;
- $save_data['process_comments'] = '会员平台未配置cookie';
- $save_data['process_time'] = $time;
- $save_data['premium_platform_rid'] = $v->premium_platform_rid;
- $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }
-
- $phrase = $rsa_services->privateDecrypt($v->platform_phrase);
-
- if(empty($phrase)){
- $save_data = [];
- $save_data['process_status'] = 5;
- $save_data['process_comments'] = '会员平台未配置助记词';
- $save_data['process_time'] = $time;
- $save_data['premium_platform_rid'] = $v->premium_platform_rid;
- $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }
-
- $this->log('tgpremium',$v->rid.':下单中,直接开始第二步');
-
- $save_data = [];
- $save_data['process_status'] = 8;
- $save_data['process_comments'] = '下单中';
- $save_data['process_time'] = $time;
- $save_data['premium_platform_rid'] = $v->premium_platform_rid;
- $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
- $save_data['platform_order_rid'] = $res['rid'];
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
-
-
- $runCount = 0;
- runagain:
-
- if($runCount >= 3){
- $save_data = [];
- $save_data['process_status'] = 4;
- $save_data['process_comments'] = '执行了3次,获取payload解码失败';
- $save_data['process_time'] = $time;
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }
-
-
- $order = curl_post_https("https://fragment.com/api?hash=".$v->platform_hash ,"recipient=".$res['recipient']."&months=".$res['premium_package_month']."&method=initGiftPremiumRequest", null, $signstr);
- $json = json_decode($order,true);
- $this->log('tgpremium',$v->rid.':第二步返回:'.$order);
- $this->log('tgpremium',$v->rid.':第二步返回:'.$json);
-
- if(empty($json['req_id'])){
- $save_data = [];
- $save_data['process_status'] = 4;
- $save_data['process_comments'] = '第二步创建ton支付订单失败';
- $save_data['process_time'] = $time;
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }else{
- $req_id = $json['req_id'];
- $amount = $json['amount'];
-
-
- $confirmOrder = curl_post_https("https://fragment.com/api?hash=".$v->platform_hash, "id=".$req_id."&show_sender=1&method=getGiftPremiumLink", null, $signstr);
- $json = json_decode($confirmOrder,true);
- $this->log('tgpremium',$v->rid.':第三步返回:'.$confirmOrder);
- $this->log('tgpremium',$v->rid.':第三步返回:'.$json);
-
- if(empty($json['ok'])){
- $save_data = [];
- $save_data['process_status'] = 4;
- $save_data['process_comments'] = '第三步确认ton支付订单失败';
- $save_data['process_time'] = $time;
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }else{
- $qr_link = $json['qr_link'];
- $expire = time() + $json['expire_after'];
-
-
- $decodeOrder = curl_get_https("https://fragment.com/tonkeeper/rawRequest?id=".$req_id."&qr=1");
- $json = json_decode($decodeOrder,true);
- $this->log('tgpremium',$v->rid.':第四步返回:'.$decodeOrder);
- $this->log('tgpremium',$v->rid.':第四步返回:'.$json);
-
- if(empty($json['body']['params']['messages'])){
- $save_data = [];
- $save_data['process_status'] = 4;
- $save_data['process_comments'] = '第四步解码ton支付订单失败';
- $save_data['process_time'] = $time;
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }else{
-
- $money = bcdiv($json['body']['params']['messages'][0]['amount'], 1000000000, 2);
- $base32 = base64_decode($json['body']['params']['messages'][0]['payload']);
- $this->log('tgpremium',$v->rid.':解析第四步payload:'.$base32);
- $base32 = explode("#",$base32);
-
-
- $base32_1 = preg_replace('/[^A-Za-z0-9]/', '', $base32[1]);
- if(mb_strlen($base32_1) < 8){
- $runCount = $runCount + 1;
- goto runagain;
- }
-
-
- if($res['premium_package_month'] == 12){
- $base32 = "Telegram Premium for 1 year Ref%23".$base32_1;
- }else{
- $base32 = "Telegram Premium for ".$res['premium_package_month']." months Ref%23".$base32_1;
- }
-
-
-
- $raw = '{
- "EQBAjaOyi2wGWlk-EDkSabqqnF-MrrwMadnwqrurKpkla9nE": "'.$money.'"
- }';
-
-
- $ton_url = configDataDictionary()['ton_url']['url'];
-
- $lastres = curl_get_https($ton_url."/sendTransactions?send_mode=1&phrase=".$phrase."&comment=".$base32,"Content-Type:application/json",$raw);
- $this->log('tgpremium',$v->rid.':第五步请求comment:'.$base32);
- $this->log('tgpremium',$v->rid.':第五步请求金额:'.$money);
- $this->log('tgpremium',$v->rid.':第五步(最后)返回1:'.$lastres);
- $this->log('tgpremium',$v->rid.':第五步(最后)返回2:'.json_decode($lastres,true));
-
- if(empty($lastres)){
-
-
-
-
-
-
-
- $save_data = [];
- $save_data['status'] = 2;
- $save_data['complete_time'] = $time;
- $save_data['tx_hash'] = '最后交易返回空,看是否支付成功';
- $save_data['req_id'] = $req_id;
- $save_data['expire_after'] = date('Y-m-d H:i:s', $expire);
- $save_data['qr_link'] = $qr_link;
- $save_data['amount'] = $money;
- $save_data['base32'] = $base32;
- PremiumPlatformOrder::where('rid',$res['rid'])->update($save_data);
-
- $save_data = [];
- $save_data['process_status'] = 9;
- $save_data['process_comments'] = 'SUCCESS';
- $save_data['process_time'] = $time;
- $save_data['tg_notice_status_send'] = 'N';
-
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }else{
- $lastres = json_decode($lastres,true);
- if(isset($lastres['txHash'])){
- $save_data = [];
- $save_data['status'] = 2;
- $save_data['complete_time'] = $time;
- $save_data['tx_hash'] = $lastres['txHash'];
- $save_data['req_id'] = $req_id;
- $save_data['expire_after'] = date('Y-m-d H:i:s', $expire);
- $save_data['qr_link'] = $qr_link;
- $save_data['amount'] = $money;
- $save_data['base32'] = $base32;
- PremiumPlatformOrder::where('rid',$res['rid'])->update($save_data);
-
- $save_data = [];
- $save_data['process_status'] = 9;
- $save_data['process_comments'] = 'SUCCESS';
- $save_data['process_time'] = $time;
- $save_data['tg_notice_status_send'] = 'N';
-
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }else{
- $msg = $lastres['error'] ??'未知,请查看go服务日志';
- $save_data = [];
- $save_data['process_status'] = 4;
- $save_data['process_comments'] = $msg;
- $save_data['process_time'] = $time;
- PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
- continue;
- }
- }
- }
- }
- }
- }
- }else{
-
- }
- }catch (\Exception $e){
-
- }
- }
-
- protected function log($log_title,$message,$remarks='info'){
- Log::get($remarks,$log_title)->info($message);
- }
- }
|