HandleTgPremium.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace App\Task;
  3. use App\Model\Premium\PremiumWalletTradeList;
  4. use App\Model\Premium\PremiumPlatformPackage;
  5. use App\Model\Premium\PremiumPlatformOrder;
  6. use App\Model\Premium\PremiumPlatform;
  7. use App\Service\RsaServices;
  8. use App\Library\Log;
  9. class HandleTgPremium
  10. {
  11. public function execute()
  12. {
  13. try {
  14. $data = PremiumWalletTradeList::from('premium_wallet_trade_list as a')
  15. ->join('premium_platform as b','a.transferto_address','b.receive_wallet')
  16. ->where('a.process_status',1)
  17. ->where('a.coin_name','usdt')
  18. ->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')
  19. ->limit(100)
  20. ->get();
  21. if($data->count() > 0){
  22. foreach ($data as $k => $v) {
  23. $time = nowDate();
  24. if($v->platform_order_rid){
  25. $res = PremiumPlatformOrder::where('rid',$v->platform_order_rid)->where('status',1)->first();
  26. }else{
  27. //匹配金额
  28. $res = PremiumPlatformOrder::where('premium_platform_rid',$v->premium_platform_rid)->where('need_pay_usdt',$v->amount)->where('status',0)->first();
  29. }
  30. if(empty($res)){
  31. $save_data = [];
  32. $save_data['process_status'] = 7; //金额无对应订单
  33. $save_data['process_comments'] = '金额无对应订单'; //处理备注
  34. $save_data['process_time'] = $time; //处理时间
  35. $save_data['premium_platform_rid'] = $v->premium_platform_rid;
  36. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  37. continue;
  38. }else{
  39. $save_data = [];
  40. $save_data['status'] = 1; //待开通
  41. PremiumPlatformOrder::where('rid',$res['rid'])->update($save_data);
  42. }
  43. if($v->status == 1){
  44. $save_data = [];
  45. $save_data['process_status'] = 6; //会员平台未启用
  46. $save_data['process_comments'] = '会员平台未启用'; //处理备注
  47. $save_data['process_time'] = $time; //处理时间
  48. $save_data['premium_platform_rid'] = $v->premium_platform_rid;
  49. $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
  50. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  51. continue;
  52. }
  53. if(empty($v->platform_cookie) || empty($v->platform_hash) || empty($v->platform_phrase)){
  54. $save_data = [];
  55. $save_data['process_status'] = 5; //会员平台未配置正确
  56. $save_data['process_comments'] = '会员平台未配置cookie或hash或助记词'; //处理备注
  57. $save_data['process_time'] = $time; //处理时间
  58. $save_data['premium_platform_rid'] = $v->premium_platform_rid;
  59. $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
  60. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  61. continue;
  62. }
  63. $rsa_services = new RsaServices();
  64. $signstr = $rsa_services->privateDecrypt($v->platform_cookie);
  65. if(empty($signstr)){
  66. $save_data = [];
  67. $save_data['process_status'] = 5; //会员平台未配置正确
  68. $save_data['process_comments'] = '会员平台未配置cookie'; //处理备注
  69. $save_data['process_time'] = $time; //处理时间
  70. $save_data['premium_platform_rid'] = $v->premium_platform_rid;
  71. $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
  72. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  73. continue;
  74. }
  75. $phrase = $rsa_services->privateDecrypt($v->platform_phrase);
  76. if(empty($phrase)){
  77. $save_data = [];
  78. $save_data['process_status'] = 5; //会员平台未配置正确
  79. $save_data['process_comments'] = '会员平台未配置助记词'; //处理备注
  80. $save_data['process_time'] = $time; //处理时间
  81. $save_data['premium_platform_rid'] = $v->premium_platform_rid;
  82. $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
  83. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  84. continue;
  85. }
  86. $this->log('tgpremium',$v->rid.':下单中,直接开始第二步');
  87. $save_data = [];
  88. $save_data['process_status'] = 8; //下单中
  89. $save_data['process_comments'] = '下单中'; //处理备注
  90. $save_data['process_time'] = $time; //处理时间
  91. $save_data['premium_platform_rid'] = $v->premium_platform_rid;
  92. $save_data['premium_package_rid'] = $res['premium_platform_package_rid'];
  93. $save_data['platform_order_rid'] = $res['rid'];
  94. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  95. //goto标志,用于获取payload解码失败重试
  96. $runCount = 0;
  97. runagain:
  98. if($runCount >= 3){
  99. $save_data = [];
  100. $save_data['process_status'] = 4; //下单失败
  101. $save_data['process_comments'] = '执行了3次,获取payload解码失败'; //处理备注
  102. $save_data['process_time'] = $time; //处理时间
  103. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  104. continue;
  105. }
  106. //第二步 创建ton支付订单
  107. $order = curl_post_https("https://fragment.com/api?hash=".$v->platform_hash ,"recipient=".$res['recipient']."&months=".$res['premium_package_month']."&method=initGiftPremiumRequest", null, $signstr);
  108. $json = json_decode($order,true);
  109. $this->log('tgpremium',$v->rid.':第二步返回:'.$order);
  110. $this->log('tgpremium',$v->rid.':第二步返回:'.$json);
  111. if(empty($json['req_id'])){
  112. $save_data = [];
  113. $save_data['process_status'] = 4; //下单失败
  114. $save_data['process_comments'] = '第二步创建ton支付订单失败'; //处理备注
  115. $save_data['process_time'] = $time; //处理时间
  116. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  117. continue;
  118. }else{
  119. $req_id = $json['req_id']; //获得订单号
  120. $amount = $json['amount'];
  121. //第三步 确认支付订单
  122. $confirmOrder = curl_post_https("https://fragment.com/api?hash=".$v->platform_hash, "id=".$req_id."&show_sender=1&method=getGiftPremiumLink", null, $signstr);
  123. $json = json_decode($confirmOrder,true);
  124. $this->log('tgpremium',$v->rid.':第三步返回:'.$confirmOrder);
  125. $this->log('tgpremium',$v->rid.':第三步返回:'.$json);
  126. if(empty($json['ok'])){
  127. $save_data = [];
  128. $save_data['process_status'] = 4; //下单失败
  129. $save_data['process_comments'] = '第三步确认ton支付订单失败'; //处理备注
  130. $save_data['process_time'] = $time; //处理时间
  131. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  132. continue;
  133. }else{
  134. $qr_link = $json['qr_link']; //获得支付地址(自己生成二维码) 任何TON钱包扫这个二维码支付就可以自动开通会员,当然这是手动模式了,这里自动模式用不到
  135. $expire = time() + $json['expire_after'];
  136. //第四步 解码订单数据 并调用TON接口 实现自动支付从而实现自动开通会员
  137. $decodeOrder = curl_get_https("https://fragment.com/tonkeeper/rawRequest?id=".$req_id."&qr=1");
  138. $json = json_decode($decodeOrder,true);
  139. $this->log('tgpremium',$v->rid.':第四步返回:'.$decodeOrder);
  140. $this->log('tgpremium',$v->rid.':第四步返回:'.$json);
  141. if(empty($json['body']['params']['messages'])){
  142. $save_data = [];
  143. $save_data['process_status'] = 4; //下单失败
  144. $save_data['process_comments'] = '第四步解码ton支付订单失败'; //处理备注
  145. $save_data['process_time'] = $time; //处理时间
  146. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  147. continue;
  148. }else{
  149. // $money = base64_decode($json['body']['params']['messages'][0]['amount']); //最终支付金额(精度9) 也就是 amount * 1000000000
  150. $money = bcdiv($json['body']['params']['messages'][0]['amount'], 1000000000, 2);
  151. $base32 = base64_decode($json['body']['params']['messages'][0]['payload']); //不是完整正确的解码
  152. $this->log('tgpremium',$v->rid.':解析第四步payload:'.$base32);
  153. $base32 = explode("#",$base32);
  154. //base64_decode解码后的数据,有时候有乱码,目前正常情况该值均为8位数极其以上
  155. $base32_1 = preg_replace('/[^A-Za-z0-9]/', '', $base32[1]);
  156. if(mb_strlen($base32_1) < 8){
  157. $runCount = $runCount + 1;
  158. goto runagain;
  159. }
  160. //最终(支付网关)订单数据 需要传递给golang 支付网关,#用%23代替,不然go获取参数获取不了#后的内容
  161. if($res['premium_package_month'] == 12){
  162. $base32 = "Telegram Premium for 1 year Ref%23".$base32_1;
  163. }else{
  164. $base32 = "Telegram Premium for ".$res['premium_package_month']." months Ref%23".$base32_1;
  165. }
  166. //第5步 由于只找到JAVA C++ GOlang的SDK,没有找到PHP版本的,所以这里使用GOlang网关(只负责Ton支付业务)
  167. //这里面这个TON钱包地址就是fragment官方开会员的固定收款钱包地址,这个地址不能改
  168. $raw = '{
  169. "EQBAjaOyi2wGWlk-EDkSabqqnF-MrrwMadnwqrurKpkla9nE": "'.$money.'"
  170. }';
  171. //取go的接口域名
  172. $ton_url = configDataDictionary()['ton_url']['url'];
  173. //发起支付
  174. $lastres = curl_get_https($ton_url."/sendTransactions?send_mode=1&phrase=".$phrase."&comment=".$base32,"Content-Type:application/json",$raw);
  175. $this->log('tgpremium',$v->rid.':第五步请求comment:'.$base32);
  176. $this->log('tgpremium',$v->rid.':第五步请求金额:'.$money);
  177. $this->log('tgpremium',$v->rid.':第五步(最后)返回1:'.$lastres);
  178. $this->log('tgpremium',$v->rid.':第五步(最后)返回2:'.json_decode($lastres,true));
  179. if(empty($lastres)){
  180. // $save_data = [];
  181. // $save_data['process_status'] = 4; //下单失败
  182. // $save_data['process_comments'] = '下单失败,最后接口请求空'; //处理备注
  183. // $save_data['process_time'] = $time; //处理时间
  184. // PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  185. // continue;
  186. $save_data = [];
  187. $save_data['status'] = 2;
  188. $save_data['complete_time'] = $time;
  189. $save_data['tx_hash'] = '最后交易返回空,看是否支付成功';
  190. $save_data['req_id'] = $req_id;
  191. $save_data['expire_after'] = date('Y-m-d H:i:s', $expire);
  192. $save_data['qr_link'] = $qr_link;
  193. $save_data['amount'] = $money;
  194. $save_data['base32'] = $base32;
  195. PremiumPlatformOrder::where('rid',$res['rid'])->update($save_data);
  196. $save_data = [];
  197. $save_data['process_status'] = 9; //下单成功
  198. $save_data['process_comments'] = 'SUCCESS'; //处理备注
  199. $save_data['process_time'] = $time; //处理时间
  200. $save_data['tg_notice_status_send'] = 'N'; //重新通知
  201. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  202. continue;
  203. }else{
  204. $lastres = json_decode($lastres,true);
  205. if(isset($lastres['txHash'])){
  206. $save_data = [];
  207. $save_data['status'] = 2;
  208. $save_data['complete_time'] = $time;
  209. $save_data['tx_hash'] = $lastres['txHash'];
  210. $save_data['req_id'] = $req_id;
  211. $save_data['expire_after'] = date('Y-m-d H:i:s', $expire);
  212. $save_data['qr_link'] = $qr_link;
  213. $save_data['amount'] = $money;
  214. $save_data['base32'] = $base32;
  215. PremiumPlatformOrder::where('rid',$res['rid'])->update($save_data);
  216. $save_data = [];
  217. $save_data['process_status'] = 9; //下单成功
  218. $save_data['process_comments'] = 'SUCCESS'; //处理备注
  219. $save_data['process_time'] = $time; //处理时间
  220. $save_data['tg_notice_status_send'] = 'N'; //重新通知
  221. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  222. continue;
  223. }else{
  224. $msg = $lastres['error'] ??'未知,请查看go服务日志';
  225. $save_data = [];
  226. $save_data['process_status'] = 4; //下单失败
  227. $save_data['process_comments'] = $msg; //处理备注
  228. $save_data['process_time'] = $time; //处理时间
  229. PremiumWalletTradeList::where('rid',$v->rid)->update($save_data);
  230. continue;
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }else{
  238. // $this->log('tgpremium','----------没有数据----------');
  239. }
  240. }catch (\Exception $e){
  241. // $this->log('tgpremium','----------任务执行报错,请联系管理员。报错原因:----------'.$e->getMessage());
  242. }
  243. }
  244. /**
  245. * 记入日志
  246. * @param $log_title [日志路径]
  247. * @param $message [内容,不支持数组]
  248. * @param $remarks [备注]
  249. */
  250. protected function log($log_title,$message,$remarks='info'){
  251. Log::get($remarks,$log_title)->info($message);
  252. }
  253. }