RefundPayAdapter.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: blank
  5. * Date: 2022/11/4
  6. * Time: 10:22
  7. */
  8. namespace app\common\modules\refund;
  9. use app\common\models\PayType;
  10. use app\common\services\PayFactory;
  11. /**
  12. * 统一化调用退款方法返回的数据格式
  13. * Class RefundPayAdapter
  14. * @package app\common\modules\refund
  15. */
  16. class RefundPayAdapter
  17. {
  18. protected $pay_type_id;
  19. protected $pay;
  20. protected $pay_sn;
  21. protected $total_amount;
  22. protected $refund_amount;
  23. public function __construct($pay_type_id)
  24. {
  25. $this->pay_type_id = $pay_type_id;
  26. $this->pay = PayFactory::create($this->pay_type_id);
  27. }
  28. protected function getPayTypeRefund()
  29. {
  30. $result = $this->pay->doRefund($this->pay_sn, $this->total_amount, $this->refund_amount);
  31. return $result;
  32. }
  33. /**
  34. * @param $pay_sn
  35. * @param $total_amount
  36. * @param $refund_amount
  37. * @return array|bool|mixed|string
  38. */
  39. public function pay($pay_sn, $total_amount, $refund_amount)
  40. {
  41. $this->pay_sn = $pay_sn;
  42. $this->total_amount = $total_amount;
  43. $this->refund_amount = $refund_amount;
  44. try {
  45. $result = $this->refundPay();
  46. if (!$result['status']) {
  47. \Log::debug('<---doRefund退款请求失败------'.$this->pay_sn, $result);
  48. }
  49. return $result;
  50. } catch (\Exception $exception) {
  51. \Log::debug('<---doRefund退款失败------'.$this->pay_sn, $exception->getMessage());
  52. return $this->fail(['pay_sn' => $pay_sn], $exception->getMessage());
  53. }
  54. }
  55. public function refundPay()
  56. {
  57. switch ($this->pay_type_id) {
  58. case PayType::WECHAT_PAY:
  59. case PayType::WECHAT_MIN_PAY:
  60. case PayType::WECHAT_H5:
  61. case PayType::WECHAT_NATIVE:
  62. case PayType::WECHAT_JSAPI_PAY:
  63. case PayType::WechatApp:
  64. case PayType::WECHAT_CPS_APP_PAY:
  65. $result = $this->wechat();
  66. break;
  67. case PayType::ALIPAY:
  68. case PayType::AlipayApp:
  69. $result = $this->alipay();
  70. break;
  71. case PayType::CREDIT:
  72. $result = $this->balance();
  73. break;
  74. case PayType::WECHAT_HJ_PAY:
  75. case PayType::ALIPAY_HJ_PAY:
  76. case PayType::CONVERGE_UNION_PAY:
  77. $result = $this->convergePayRefund();
  78. break;
  79. case PayType::CONVERGE_QUICK_PAY:
  80. $result = $this->convergeQuickPay();
  81. break;
  82. default:
  83. $result = $this->noAdapterType();
  84. }
  85. return $result;
  86. }
  87. //微信JSAPI、H5、NATIVE、小程序、APP支付退款入口
  88. protected function wechat()
  89. {
  90. $result = $this->getPayTypeRefund();
  91. if (!$result) {
  92. return $this->fail($result, '支付类退款方法失败');
  93. }
  94. return $this->success($result);
  95. }
  96. protected function alipay()
  97. {
  98. $result = $this->getPayTypeRefund();
  99. if ($result === false) {
  100. return $this->fail($result, '支付类退款方法失败');
  101. }
  102. return $this->success($result);
  103. }
  104. protected function balance()
  105. {
  106. $result = $this->getPayTypeRefund();
  107. if ($result !== true) {
  108. return $this->fail($result, '支付类退款方法失败');
  109. }
  110. return $this->success($result);
  111. }
  112. protected function convergePay()
  113. {
  114. $result = $this->getPayTypeRefund();
  115. if ($result['ra_Status'] == '101') {
  116. return $this->fail($result, '汇聚微信或支付宝退款失败,失败原因' . $result['rc_CodeMsg']);
  117. }
  118. return $this->success($result);
  119. }
  120. /**
  121. * 汇聚聚合支付统一退款方法
  122. * @return array|bool|mixed|string
  123. * @throws AdminException
  124. * @throws \app\common\exceptions\AppException
  125. */
  126. protected function convergePayRefund()
  127. {
  128. $result = $this->getPayTypeRefund();
  129. if ($result['ra_Status'] == '101') {
  130. return $this->fail($result, '汇聚退款失败,失败原因:' . $result['rc_CodeMsg']);
  131. }
  132. return $this->success($result);
  133. }
  134. protected function convergeQuickPay()
  135. {
  136. $result = $this->getPayTypeRefund();
  137. if (!$result['code']) {
  138. return $this->fail($result, $result['msg']);
  139. }
  140. return $this->success($result);
  141. }
  142. protected function noAdapterType()
  143. {
  144. $result = $this->getPayTypeRefund();
  145. if (!$result) {
  146. \Log::debug("<--{$this->pay_type_id}----没适配器支付方式退款-------".$this->pay_sn, $result);
  147. return $this->fail($result);
  148. }
  149. return $this->success($result);
  150. }
  151. public function fail($result,$msg = '退款请求失败')
  152. {
  153. return $this->format(0, $msg, $result);
  154. }
  155. public function success($result,$msg = '退款请求成功')
  156. {
  157. return $this->format(1, $msg, $result);
  158. }
  159. protected function format($status, $msg = '', $result)
  160. {
  161. return ['status' => $status, 'msg' => $msg, 'data' => $result];
  162. }
  163. }