CryptoController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. //use App\Http\logic\api\UuPayCashierLogic;
  4. use App\dao\Pay\AccountPayInfo;
  5. use App\Http\logic\api\CryptoLogic;
  6. use App\Inter\PayMentInterFace;
  7. use App\Notification\DingDingRobot;
  8. use App\Notification\TelegramBot;
  9. use App\Services\PayConfig;
  10. use App\Util;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\Redis;
  14. class CryptoController implements PayMentInterFace
  15. {
  16. private $retryTimes=0;
  17. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  18. {
  19. $logic = new CryptoLogic();
  20. try {
  21. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType);
  22. } catch (\Exception $exception) {
  23. Redis::set("PayErro_Crypto",1);
  24. Redis::expire("PayErro_Crypto",600);
  25. Util::WriteLog('Crypto_error', $exception->getMessage().json_encode($logic->result));
  26. TelegramBot::getDefault()->sendMsgWithEnv("Crypto Except ".$exception->getMessage().$exception->getTraceAsString() );
  27. return apiReturnFail($logic->getError());
  28. }
  29. if (!empty($res)) {
  30. TelegramBot::getDefault()->sendMsg(json_encode($res));
  31. $data = [
  32. 'content' => urldecode($res['redirectURL']),
  33. 'money' => $payAmt,
  34. 'prdOrdNo' => $res['mchOrderNo'],
  35. 'redirectURL' => urldecode($res['redirectURL']),
  36. ];
  37. return apiReturnSuc($data);
  38. // header("Location: {$res['url']}");
  39. } else if($res==false) {
  40. return apiReturnFail($logic->getError());
  41. }else{
  42. if($this->retryTimes>0 || true) {
  43. Redis::set("PayErro_Crypto",1);
  44. Redis::expire("PayErro_Crypto",600);
  45. TelegramBot::getDefault()->sendProgramNotify("Crypto RetrunFail ", $logic->getError()." | ".json_encode($res));
  46. return apiReturnFail($logic->getError());
  47. }else{
  48. $this->retryTimes++;
  49. return $this->pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType);
  50. }
  51. }
  52. }
  53. // 支付异步回调
  54. public function notify(Request $request)
  55. {
  56. $post = $request->all();
  57. if (!is_array($post)) {
  58. $post = \GuzzleHttp\json_decode($post, true);
  59. }
  60. Util::WriteLog('Crypto', $post);
  61. $post = @$post['data'];
  62. $order_sn = @$post['externalOrderId'];
  63. $logic = new CryptoLogic();
  64. try {
  65. $redis = Redis::connection();
  66. $ret= $logic->notify($post);
  67. if($ret=='SUCCESS')$redis->set($order_sn, $order_sn, 3600 * 24);
  68. return $ret;
  69. }catch (\Exception $exception){
  70. Redis::set("PayErro_Crypto",1);
  71. Redis::expire("PayErro_Crypto",600);
  72. TelegramBot::getDefault()->sendProgramNotify("Crypto 订单回调执行 异常 ", json_encode($post),$exception);
  73. Util::WriteLog("Crypto_error",$post);
  74. return 'fail';
  75. }
  76. }
  77. public function sync_notify(Request $request)
  78. {
  79. Log::info('同步回调');
  80. echo '同步回调';
  81. }
  82. // 提现异步回调
  83. public function cash_notify(Request $request)
  84. {
  85. $headers = [
  86. "X-Account" => $request->header("X-Account"),
  87. "X-Signature" => $request->header("X-Signature"),
  88. ];
  89. $payload = file_get_contents('php://input');
  90. $service = new SitoBank();
  91. $verify = $service->verify($payload, $headers['X-Signature']);
  92. $post = json_decode($payload, true);
  93. Util::WriteLog('SitoBank', "SitoBank cash 异步回调\n".json_encode(compact("payload", "headers", 'verify')));
  94. if ($headers['X-Account']==$service->config['merchantCode']) {
  95. //稍后处理verify
  96. }else if(!$verify){
  97. Util::WriteLog('SitoBank cash','签名错误');
  98. return 'fail';
  99. }
  100. $logic = new SitoBankCashierLogic();
  101. try {
  102. return $logic->notify($post);
  103. }catch (\Exception $exception){
  104. TelegramBot::getDefault()->sendProgramNotify("SitoBank 提现异步回调执行 异常 ", json_encode($post),$exception);
  105. Util::WriteLog("SitoBank_error",$post);
  106. return '{"success":false,"message":"商户自定义出错信息"}';
  107. }
  108. }
  109. }