PayController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace backend\controllers;
  3. use backend\models\PayApi;
  4. use backend\models\RateApi;
  5. use Yii;
  6. use yii\helpers\VarDumper;
  7. use yii\httpclient\Client;
  8. use yii\web\Controller;
  9. class PayController extends Controller
  10. {
  11. public $enableCsrfValidation = false;
  12. /**
  13. * 支付异步回调
  14. * @return string
  15. */
  16. public function actionNotify()
  17. {
  18. $payType = intval(Yii::$app->getRequest()->get('payType'));
  19. if ($payType <= 0) {
  20. return 'invalid request';
  21. }
  22. if ($payType == 9) {
  23. $data = @json_decode(file_get_contents("php://input"),true);
  24. } else {
  25. if (Yii::$app->getRequest()->getIsPost()) {
  26. $data = Yii::$app->getRequest()->post();
  27. if (empty($data)) {
  28. $data = @json_decode(Yii::$app->getRequest()->getRawBody(), true);
  29. }
  30. } else {
  31. $data = Yii::$app->getRequest()->get();
  32. }
  33. }
  34. Yii::warning('支付异步通知参数111,' . VarDumper::dumpAsString($data), __METHOD__);
  35. $api = new PayApi();
  36. $result = $api->callNotify($data, $payType);
  37. return $result['data'];
  38. }
  39. /**
  40. * 支付同步回调
  41. * @return string|\yii\web\Response
  42. */
  43. public function actionReturn()
  44. {
  45. $payType = intval(Yii::$app->getRequest()->get('payType'));
  46. if ($payType <= 0) {
  47. return 'invalid request';
  48. }
  49. if (Yii::$app->getRequest()->getIsPost()) {
  50. $data = Yii::$app->getRequest()->post();
  51. if (empty($data)) {
  52. $data = @json_decode(Yii::$app->getRequest()->getRawBody(), true);
  53. }
  54. } else {
  55. $data = Yii::$app->getRequest()->get();
  56. }
  57. Yii::warning('支付同步通知参数,' . VarDumper::dumpAsString($data), __METHOD__);
  58. $api = new PayApi();
  59. $result = $api->callReturn($data, $payType);
  60. return $this->redirect(['/user/pay/succeed']);
  61. // if ($result['code'] == 1) {
  62. // return $this->redirect(['/user/pay/succeed']);
  63. // } else {
  64. // return $this->redirect(['/user/pay/failed']);
  65. // }
  66. }
  67. /**
  68. * 汇率获取
  69. */
  70. public function actionRate()
  71. {
  72. $rateApi = new RateApi();
  73. $res = $rateApi->getRate();
  74. echo json_encode($res);
  75. }
  76. }