PayController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/4/21
  6. * Time: 下午6:16
  7. */
  8. namespace app\backend\modules\refund\controllers;
  9. use app\common\components\BaseController;
  10. use app\common\events\order\AfterOrderRefundSuccessEvent;
  11. use app\common\exceptions\AppException;
  12. use app\common\exceptions\ShopException;
  13. use app\common\modules\refund\services\RefundService;
  14. use app\backend\modules\refund\services\RefundMessageService;
  15. use app\backend\modules\refund\models\RefundApply;
  16. class PayController extends BaseController
  17. {
  18. private $refundApply;
  19. public $transactionActions = [];
  20. public function preAction()
  21. {
  22. parent::preAction();
  23. $this->validate([
  24. 'refund_id' => 'required',
  25. ]);
  26. $this->refundApply = RefundApply::find(request()->input('refund_id'));
  27. if (!isset($this->refundApply)) {
  28. throw new AdminException('退款记录不存在');
  29. }
  30. }
  31. public function api()
  32. {
  33. $request = request()->input();
  34. try {
  35. $result = \Illuminate\Support\Facades\DB::transaction(function () use ($request) {
  36. $result = (new RefundService)->pay($request['refund_id']);
  37. if (!$result) {
  38. throw new AppException('操作失败');
  39. }
  40. return $result;
  41. });
  42. } catch (\Exception $e) {
  43. \Log::debug('<------售后退款失败------:'.$e->getMessage());
  44. throw new AppException($e->getMessage());
  45. }
  46. return $this->successJson('退款成功');
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function index()
  52. {
  53. $request = request()->input();
  54. $this->validate([
  55. 'refund_id' => 'required'
  56. ]);
  57. $restrictAccess = \app\common\services\RequestTokenService::limitRepeat('agree_refund_'. $request['refund_id']);
  58. if (!$restrictAccess) {
  59. throw new ShopException('短时间内重复操作,请等待10秒后再操作');
  60. }
  61. try {
  62. /**
  63. * @var $this ->refundApply RefundApply
  64. */
  65. $result = \Illuminate\Support\Facades\DB::transaction(function () use ($request) {
  66. $result = (new RefundService)->pay($request['refund_id']);
  67. if (!$result) {
  68. throw new ShopException('操作失败');
  69. }
  70. return $result;
  71. });
  72. } catch (\Exception $e) {
  73. \Log::debug('<------售后退款失败------:'.$e->getMessage());
  74. throw new ShopException($e->getMessage());
  75. }
  76. if (is_string($result)) {
  77. redirect($result)->send();
  78. }
  79. if (is_array($result) && isset($result['action']) && isset($result['input'])) {
  80. echo $this->formPost($result);exit();
  81. }
  82. // RefundMessageService::passMessage($this->refundApply);//通知买家
  83. //
  84. // event(new AfterOrderRefundSuccessEvent($this->refundApply));
  85. // if (app('plugins')->isEnabled('instation-message')) {
  86. // event(new \Yunshop\InstationMessage\event\OrderRefundSuccessEvent($this->refundApply));
  87. // }
  88. return $this->message('操作成功');
  89. }
  90. /**
  91. * 表单POST请求
  92. * @param $trxCode
  93. * @param $data
  94. */
  95. public function formPost($data)
  96. {
  97. $echo = "<form style='display:none;' id='form1' name='form1' method='post' action='" . $data['action']."'>";
  98. foreach ($data['input'] as $k => $v) {
  99. $echo .= "<input name='{$k}' type='text' value='{$v}' />";
  100. }
  101. $echo .= "</form>";
  102. $echo .= "<script type='text/javascript'>function load_submit(){document.form1.submit()}load_submit();</script>";
  103. echo $echo;
  104. }
  105. }