OrderRefundService.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. require __DIR__.'/../library/upacp_demo_b2c/demo/api_01_gateway/Form_6_4_Refund.php';
  47. list($succ,$msg)=uniCompanyRefund($this->refund->order_no,$this->payment->pay_no,$this->getAmount('f'),$this->notifyUrl());
  48. if(!$succ){
  49. throw_user($msg);
  50. }
  51. $this->refund->refundResult(true);
  52. }
  53. public function wechat(){
  54. try {
  55. Service::getApp('wechat','')->refund([
  56. 'out_trade_no' => $this->payment->order_no,
  57. 'out_refund_no' => $this->refund->order_no,
  58. 'total_fee' => moneyFormat($this->payment->amount,'f'),
  59. 'refund_fee' => $this->getAmount('f'),
  60. 'refund_desc' => $this->body,
  61. ]);
  62. $this->refund->refundResult(true);
  63. }catch (GatewayException|BusinessException $e){
  64. throw_user($e->getMessage());
  65. }
  66. }
  67. public function alipay(){
  68. try {
  69. Service::getApp('alipay','')->refund([
  70. 'out_trade_no' => $this->payment->order_no,
  71. 'refund_amount' => $this->getAmount(),
  72. ]);
  73. $this->refund->refundResult(true);
  74. }catch (GatewayException $e){
  75. throw_user($e->raw['alipay_trade_refund_response']['sub_msg']);
  76. }
  77. }
  78. protected function notifyUrl(){
  79. return request()->root(true)."/index/payment/refund/order_no/".$this->refund->order_no;
  80. }
  81. public function bankUnion(){
  82. require __DIR__.'/../library/upacp_demo_b2c/sdk/acp_service.php';
  83. list($bool,$msg)=AcpService::refund($this->refund->order_no,$this->payment->pay_no,$this->getAmount('f'),$this->notifyUrl());
  84. user_log("OrderRefundService/bankUnion/{$this->refund->order_no}",compact('bool','msg'));
  85. if(!$bool){
  86. throw_user($msg);
  87. }
  88. $this->refund->refundResult(true);
  89. }
  90. public function otherUser(){}
  91. public function offline(){}
  92. public function pay(){
  93. $method=Arr::get(OrderPayService::$methods,$this->payment->pay_type);
  94. return App::invokeMethod([$this,$method]);
  95. }
  96. }