Wenzhenorder.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace app\common\model;
  3. use think\Db;
  4. use think\Model;
  5. use addons\epay\library\Service;
  6. /**
  7. * 问诊模型
  8. */
  9. class Wenzhenorder extends Model
  10. {
  11. // 表名
  12. protected $name = 'wenzhen_order';
  13. // 开启自动写入时间戳字段
  14. protected $autoWriteTimestamp = false;
  15. // 定义时间戳字段名
  16. protected $createTime = false;
  17. protected $updateTime = false;
  18. // 退款
  19. public function old_refund($order, $refund_price)
  20. {
  21. $table = 'wenzhen_order';
  22. $remark = '问诊退款';
  23. // 生成退款单
  24. $refund_data = [
  25. 'order_id' => $order['id'],
  26. 'out_refund_no'=> createUniqueNo('WR',$order['id']),
  27. 'pay_fee' => $order['total_fee'],
  28. 'refund_price' => $refund_price,
  29. 'pay_type' => $order['pay_type'],
  30. 'status' => 0,
  31. 'createtime' => time(),
  32. 'updatetime' => time(),
  33. 'table' => $table,
  34. 'table_id' => $order['id'],
  35. ];
  36. $refund_log_id = Db::name('pay_order_refund_log')->insertGetId($refund_data);
  37. if(!$refund_log_id){
  38. return '退款失败';
  39. }
  40. if ($order['pay_type'] == 'wechat' || $order['pay_type'] == 'alipay') {
  41. // 微信|支付宝退款
  42. // 退款数据
  43. $order_data = [
  44. 'out_trade_no' => $order['pay_out_trade_no']
  45. ];
  46. if ($order['pay_type'] == 'wechat') {
  47. $total_fee = $order['total_fee'] * 100;
  48. $refund_fee = $refund_price * 100;
  49. $order_data = array_merge($order_data, [
  50. 'out_refund_no' => $refund_data['out_refund_no'],
  51. 'total_fee' => $total_fee,
  52. 'refund_fee' => $refund_fee,
  53. 'refund_desc' => $remark,
  54. ]);
  55. } else {
  56. $order_data = array_merge($order_data, [
  57. 'out_request_no' => $refund_data['out_refund_no'],
  58. 'refund_amount' => $refund_price,
  59. ]);
  60. }
  61. //
  62. if ($order['pay_type'] == 'wechat') {
  63. $wxpay = new \app\common\library\Wxpay;
  64. $result = $wxpay->WxPayRefund($order_data);
  65. // 微信通知回调 pay->notifyr
  66. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  67. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  68. return true;
  69. } else {
  70. return $result['return_msg'];
  71. }
  72. } else {
  73. $result = Service::submitRefund($order['total_fee'],$refund_price,$order['pay_out_trade_no'],$refund_data['out_refund_no'],$order['pay_type'],$remark,'');
  74. if($result['code'] == '10000'){
  75. Db::name('pay_order_refund_log')->where('id',$refund_log_id)->update(['status'=>1]);
  76. return true;
  77. }else{
  78. return $result['msg'];
  79. }
  80. /* return 'alipay wrong way';
  81. $alipay = new \app\common\library\AliPay;
  82. $result = $alipay->AliPayRefund($order_data);
  83. // 支付宝通知回调 pay->notifyx
  84. return $result;*/
  85. /*if ($result['code'] == "10000") {
  86. return true;
  87. } else {
  88. throw new \Exception($result['msg']);
  89. }*/
  90. }
  91. // { // 微信返回结果
  92. // "return_code":"SUCCESS",
  93. // "return_msg":"OK",
  94. // "appid":"wx39cd0799d4567dd0",
  95. // "mch_id":"1481069012",
  96. // "nonce_str":"huW9eIAb5BDPn0Ma",
  97. // "sign":"250316740B263FE53F5DFF50AF5A8FA1",
  98. // "result_code":"SUCCESS",
  99. // "transaction_id":"4200000497202004072822298902",
  100. // "out_trade_no":"202010300857029180027000",
  101. // "out_refund_no":"1586241595",
  102. // "refund_id":"50300603862020040700031444448",
  103. // "refund_channel":[],
  104. // "refund_fee":"1",
  105. // "coupon_refund_fee":"0",
  106. // "total_fee":"1",
  107. // "cash_fee":"1",
  108. // "coupon_refund_count":"0",
  109. // "cash_refund_fee":"1
  110. // }
  111. // { // 支付宝返回结果
  112. // "code": "10000",
  113. // "msg": "Success",
  114. // "buyer_logon_id": "157***@163.com",
  115. // "buyer_user_id": "2088902485164146",
  116. // "fund_change": "Y",
  117. // "gmt_refund_pay": "2020-08-15 16:11:45",
  118. // "out_trade_no": "202002460317545607015300",
  119. // "refund_fee": "0.01",
  120. // "send_back_fee": "0.00",
  121. // "trade_no": "2020081522001464141438570535"
  122. // }
  123. }
  124. return true;
  125. }
  126. }