SitoBankController.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\SitoBankCashierLogic;
  6. use App\Http\logic\api\SitoBankLogic;
  7. use App\Inter\PayMentInterFace;
  8. use App\Notification\DingDingRobot;
  9. use App\Notification\TelegramBot;
  10. use App\Services\PayConfig;
  11. use App\Services\SitoBank;
  12. use App\Util;
  13. use Illuminate\Http\Request;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Redis;
  16. class SitoBankController implements PayMentInterFace
  17. {
  18. private $retryTimes=0;
  19. public function pay_order($userId, $payAmt, $userName, $userEmail, $userPhone, $GiftsID, $buyIP, $AdId, $eventType, $pay_method = '')
  20. {
  21. $logic = new SitoBankLogic();
  22. try {
  23. $res = $logic->pay_order($userId, $payAmt, $userPhone, $userEmail, $userName, $GiftsID, $buyIP, $AdId, $eventType);
  24. } catch (\Exception $exception) {
  25. Redis::set("PayErro_SitoBank",1);
  26. Redis::expire("PayErro_SitoBank",600);
  27. Util::WriteLog('SitoBank_error', $exception->getMessage().json_encode($logic->result));
  28. TelegramBot::getDefault()->sendProgramNotify("SitoBank Except ",$exception->getMessage(),$exception );
  29. return apiReturnFail($logic->getError());
  30. }
  31. if (!empty($res) && isset($res['status'])&&$res['status'] >0) {
  32. $data = [
  33. 'content' => urldecode($res['reference']),
  34. 'money' => $payAmt,
  35. 'prdOrdNo' => $res['mchOrderNo'],
  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) {
  43. Redis::set("PayErro_SitoBank",1);
  44. Redis::expire("PayErro_SitoBank",600);
  45. TelegramBot::getDefault()->sendProgramNotify("SitoBank 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. $headers = [
  57. "X-Account" => $request->header("X-Account"),
  58. "X-Signature" => $request->header("X-Signature"),
  59. ];
  60. $payload = file_get_contents('php://input');
  61. $service = new SitoBank();
  62. $verify = $service->verify($payload, $headers['X-Signature']);
  63. $post = json_decode($payload, true);
  64. Util::WriteLog('SitoBank', "SitoBank回调订单\n".json_encode(compact("payload", "headers", 'verify')));
  65. if ($headers['X-Account'] ==$service->config['merchantCode']){
  66. //稍后处理verify
  67. }else if(!$verify){
  68. Util::WriteLog('SitoBank','签名错误');
  69. return 'fail';
  70. }
  71. $order_sn = @$post['mchOrderNo'];
  72. $logic = new SitoBankLogic();
  73. try {
  74. $redis = Redis::connection();
  75. $ret= $logic->notify($post);
  76. if($ret=='SUCCESS')$redis->set($order_sn, $order_sn, 3600 * 24);
  77. return $ret;
  78. }catch (\Exception $exception){
  79. Redis::set("PayErro_SitoBank",1);
  80. Redis::expire("PayErro_SitoBank",600);
  81. TelegramBot::getDefault()->sendProgramNotify("SitoBank 订单回调执行 异常 ", json_encode($post),$exception);
  82. Util::WriteLog("SitoBank_error",$post);
  83. return '{"success":false,"message":"商户自定义出错信息"}';
  84. }
  85. }
  86. public function sync_notify(Request $request)
  87. {
  88. Log::info('同步回调');
  89. echo '同步回调';
  90. }
  91. // 提现异步回调
  92. public function cash_notify(Request $request)
  93. {
  94. $headers = [
  95. "X-Account" => $request->header("X-Account"),
  96. "X-Signature" => $request->header("X-Signature"),
  97. ];
  98. $payload = file_get_contents('php://input');
  99. $service = new SitoBank();
  100. $verify = $service->verify($payload, $headers['X-Signature']);
  101. $post = json_decode($payload, true);
  102. Util::WriteLog('SitoBank', "SitoBank cash 异步回调\n".json_encode(compact("payload", "headers", 'verify')));
  103. if ($headers['X-Account']==$service->config['merchantCode']) {
  104. //稍后处理verify
  105. }else if(!$verify){
  106. Util::WriteLog('SitoBank cash','签名错误');
  107. return 'fail';
  108. }
  109. $logic = new SitoBankCashierLogic();
  110. try {
  111. return $logic->notify($post);
  112. }catch (\Exception $exception){
  113. TelegramBot::getDefault()->sendProgramNotify("SitoBank 提现异步回调执行 异常 ", json_encode($post),$exception);
  114. Util::WriteLog("SitoBank_error",$post);
  115. return '{"success":false,"message":"商户自定义出错信息"}';
  116. }
  117. }
  118. }