OrderRefundService.php 2.7 KB

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