NotifyController.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace App\Http\Controllers\Api\v1;
  3. use App\Http\Controllers\Api\BaseController;
  4. use App\Services\NotifyService;
  5. use App\Services\RedisService;
  6. use App\Services\SiyuanService;
  7. use App\Services\WechatService;
  8. /**
  9. * 回调控制器
  10. * @author wesmiler
  11. * @since 2020/11/10
  12. * Class NotifyController
  13. * @package App\Http\Controllers
  14. */
  15. class NotifyController extends BaseController
  16. {
  17. /**
  18. * 构造函数
  19. * @author wesmiler
  20. * @since 2020/11/11
  21. * NotifyController constructor.
  22. */
  23. public function __construct()
  24. {
  25. parent::__construct();
  26. $this->service = new SiyuanService();
  27. }
  28. /**
  29. * 支付回调
  30. */
  31. public function pay($scene){
  32. $postData = request()->all();
  33. $sign = request()->headers->get('Wechatpay-Signature');
  34. $nonce = request()->headers->get('Wechatpay-Nonce');
  35. $timestamp = request()->headers->get('Wechatpay-Timestamp');
  36. /* $sign = 'JnLbXF3HLEeAVuyVoyedezPV8+mRZ93+rmlbHOIm1I0+YKboBTUmVNwlV9KKOVPXPBQENxjHqT6fR5Qx326uVZryb\/JM+lTdvl3j+XVD0F5NJWZKGu7Zykf69yYMY3wbe1++em\/iiouZLg8\/5KRp7nvNMLL383gemOMRbsYN3+dZ++62qrmVZxo00fHlK+AjxXmV38KRCLtfLWfIsUXw9Zi8hXqHbf+s\/pBthV+Ouasw0Eit4f4E3aDjp4baFD70s\/RsbcqTn8UqxCurRNgm7kCtLphzStPnEUC1dfvIWZQIuJ+BaAqstj9y7RW5CdF7aZPqhXnPETvB8ZynSTYl3Q==';
  37. $timestamp = '1626663968';
  38. $nonce = 'yWa6xnLUDyqfVYhjTnWcafDuNzgoXMNa';*/
  39. $postStr = file_get_contents("php://input");
  40. $signStr = "{$timestamp}\n{$nonce}\n{$postStr}\n";
  41. $id = isset($postData['id'])? '_'.$postData['id'] : '';
  42. RedisService::set('caches:payments:'.$scene.':result'.$id.'_'.date('YmdHis'), ['result'=> $postData,'sign'=> $sign,'date'=> date('Y-m-d H:i:s')], 7200);
  43. $postData = isset($postData['resource'])? $postData['resource'] : [];
  44. if(empty($scene) || empty($postData)){
  45. return NotifyService::make()->rebackMsg('回调参数错误');
  46. }
  47. // 解密
  48. $ciphertext = isset($postData['ciphertext'])? $postData['ciphertext'] : '';
  49. RedisService::set('caches:payments:'.$scene.':result'.$id.'_ciphertext', ['result'=> $postData,'date'=> date('Y-m-d H:i:s')], 7200);
  50. if(empty($ciphertext)){
  51. return NotifyService::make()->rebackMsg('解密数据不存在');
  52. }
  53. $postData = WechatService::decryptNotifyData($postData);
  54. $postData = $postData? json_decode($postData, true) : [];
  55. var_dump($postData);
  56. $outTradeNo = isset($postData['out_trade_no']) ? $postData['out_trade_no'] : '';
  57. if(empty($postData) || empty($outTradeNo)){
  58. return NotifyService::make()->rebackMsg('获取解密数据失败');
  59. }
  60. $postData['id'] = $id;
  61. $postData['sign'] = $sign;
  62. RedisService::set('caches:payments:'.$scene.':result_'.$outTradeNo, ['result'=> $postData], 7200);
  63. RedisService::set('caches:payments:'.$scene.':check', ['data'=> $postData,'signStr'=> $signStr,'sign'=> $sign], 3600);
  64. if (WechatService::checkJsapiNotifyV3($signStr, $sign) === true) {
  65. switch($scene){
  66. case 'index': // 供灯订单
  67. return NotifyService::make()->notifyGongdeng($postData, $outTradeNo);
  68. case 'recharge': // 充值
  69. break;
  70. default:
  71. break;
  72. }
  73. }
  74. return NotifyService::make()->rebackMsg('回调失败');
  75. }
  76. }