OrderRefundService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace app\common\service;
  3. use addons\epay\library\Service;
  4. use app\common\model\Payment;
  5. use com\unionpay\acp\sdk\AcpService;
  6. use fast\Arr;
  7. use think\App;
  8. use think\Model;
  9. use Yansongda\Pay\Exceptions\BusinessException;
  10. use Yansongda\Pay\Exceptions\GatewayException;
  11. class OrderRefundService{
  12. /** @var Payment */
  13. protected $payment;
  14. /** @var Model */
  15. protected $refund;
  16. protected $body='退款';
  17. /**
  18. * @param Payment $payment
  19. */
  20. public function setPayment(Payment $payment): void
  21. {
  22. $this->payment = $payment;
  23. }
  24. /**
  25. * @param string $body
  26. */
  27. public function setBody(string $body): void
  28. {
  29. $this->body = $body;
  30. }
  31. /**
  32. * @param Model $refund
  33. */
  34. public function setRefund(Model $refund): void
  35. {
  36. $this->refund = $refund;
  37. }
  38. /**
  39. * @return mixed
  40. */
  41. public function getAmount($unit='y')
  42. {
  43. return moneyFormat($this->refund->getRefundAmount(),$unit);
  44. }
  45. public function companyBank(){}
  46. public function wechat(){
  47. try {
  48. Service::getApp('wechat','')->refund([
  49. 'out_trade_no' => $this->payment->order_no,
  50. 'out_refund_no' => $this->refund->order_no,
  51. 'total_fee' => moneyFormat($this->payment->amount,'f'),
  52. 'refund_fee' => $this->getAmount('f'),
  53. 'refund_desc' => $this->body,
  54. ]);
  55. $this->refund->refundResult(true);
  56. }catch (GatewayException|BusinessException $e){
  57. throw_user($e->getMessage());
  58. }
  59. }
  60. public function alipay(){
  61. try {
  62. Service::getApp('alipay','')->refund([
  63. 'out_trade_no' => $this->payment->order_no,
  64. 'refund_amount' => $this->getAmount(),
  65. ]);
  66. $this->refund->refundResult(true);
  67. }catch (GatewayException $e){
  68. throw_user($e->raw['alipay_trade_refund_response']['sub_msg']);
  69. }
  70. }
  71. protected function notifyUrl(){
  72. return request()->root(true)."/index/payment/refund/order_no/".$this->refund->order_no;
  73. }
  74. public function bankUnion(){
  75. require __DIR__.'/../library/upacp_demo_b2c/sdk/acp_service.php';
  76. list($bool,$msg)=AcpService::refund($this->refund->order_no,$this->payment->pay_no,$this->getAmount('f'),$this->notifyUrl());
  77. user_log("OrderRefundService/bankUnion/{$this->refund->order_no}",compact('bool','msg'));
  78. if(!$bool){
  79. throw_user($msg);
  80. }
  81. $this->refund->refundResult(true);
  82. }
  83. public function otherUser(){}
  84. public function offline(){}
  85. public function pay(){
  86. $method=Arr::get(OrderPayService::$methods,$this->payment->pay_type);
  87. return App::invokeMethod([$this,$method]);
  88. }
  89. }