Refund.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\common\service;
  3. use addons\epay\library\Service;
  4. use app\common\model\MobileOrder;
  5. class Refund{
  6. protected $type;
  7. protected $mobileOrder;
  8. protected $types=[
  9. 1=>'wechat',
  10. 2=>'alipay',
  11. ];
  12. public static function setType(MobileOrder $mobileOrder){
  13. $a=new self;
  14. $a->mobileOrder=$mobileOrder;
  15. $a->type=$mobileOrder['pay_type'];
  16. return $a;
  17. }
  18. public function refund(){
  19. if(!isset($this->types[$this->type])){
  20. throw_user('此付款方式不支持退款');
  21. }
  22. $this->{$this->types[$this->type]}();
  23. }
  24. public function getAmount($unit='f'){
  25. return $unit=='y'?$this->mobileOrder['amount_refund']:$this->mobileOrder['amount_refund']*100;
  26. }
  27. public function wechat(){
  28. $res=Service::getApp('wechat',Service::refundUrl('wechat',$this->mobileOrder['pay_no']))->refund([
  29. 'transaction_id'=>$this->mobileOrder['pay_no'],
  30. 'out_refund_no'=>$this->mobileOrder['refund_no'],
  31. 'total_fee'=>$this->mobileOrder['amount']*100,
  32. 'refund_fee'=>$this->getAmount(),
  33. 'op_user_id'=>Service::getWechatOpUser(),
  34. ]);
  35. if($res['return_code']!=='SUCCESS'){
  36. throw_user($res['return_msg']);
  37. }
  38. if($res['result_code']!='SUCCESS'){
  39. throw_user($res['err_code_des']);
  40. }
  41. }
  42. /**
  43. *["code"] => string(5) "10000"
  44. ["msg"] => string(7) "Success"
  45. ["buyer_logon_id"] => string(16) "xie***@gmail.com"
  46. ["buyer_user_id"] => string(16) "2088702452773896"
  47. ["fund_change"] => string(1) "Y"
  48. ["gmt_refund_pay"] => string(19) "2022-06-30 09:56:23"
  49. ["out_trade_no"] => string(26) "7dris4160g3k4hlmk69fdplav7"
  50. ["refund_fee"] => string(4) "0.01"
  51. ["send_back_fee"] => string(4) "0.00"
  52. ["trade_no"] => string(28) "2022063022001473891456574041"
  53. */
  54. public function alipay(){
  55. $res=Service::getApp('alipay',Service::refundUrl('alipay',$this->mobileOrder['pay_no']))->refund([
  56. 'trade_no'=>$this->mobileOrder['pay_no'],
  57. 'refund_amount'=>$this->getAmount('y'),
  58. 'refund_reason'=>$this->reason(),
  59. 'out_request_no'=>$this->mobileOrder['refund_no'],
  60. ]);
  61. if($res['code']!=10000){
  62. throw_user($res['msg']);
  63. }
  64. /*if($res['sub_code']!='TRADE_HAS_SUCCESS'){
  65. throw_user($res['sub_msg']);
  66. }*/
  67. }
  68. public function reason(){
  69. return sprintf("订单[%s]退款",$this->mobileOrder['order_no']);
  70. }
  71. }