ScanPayRefundClass.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\shop\classes\ShopClass;
  4. use bizGhs\shop\classes\MainClass;
  5. use common\components\dict;
  6. use common\components\lakala\Lakala;
  7. use common\components\orderSn;
  8. use common\components\util;
  9. use Yii;
  10. use bizGhs\base\classes\BaseClass;
  11. class ScanPayRefundClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizGhs\order\models\ScanPayRefund';
  14. const STATUS_COMPLETE = 1;
  15. public static function createRefund($scanPayId, $post)
  16. {
  17. $refundPrice = round($post['price'] ?? 0, 2);
  18. if ($refundPrice <= 0) {
  19. util::fail('请填写退款金额');
  20. }
  21. $mainId = $post['mainId'] ?? 0;
  22. $order = ScanPayClass::getLockById($scanPayId);
  23. if (empty($order)) {
  24. util::fail('没有找到流水');
  25. }
  26. if ($order->mainId != $mainId) {
  27. util::fail('没有权限');
  28. }
  29. if ($order->status != 2 || $order->payStatus != 1) {
  30. util::fail('仅已付款流水可退款');
  31. }
  32. $actPrice = $order->actPrice ?? 0;
  33. $tkPrice = $order->tkPrice ?? 0;
  34. $canRefund = bcsub($actPrice, $tkPrice, 2);
  35. if ($refundPrice > $canRefund) {
  36. util::fail('退款金额超过可退金额');
  37. }
  38. $payWay = $order->payWay ?? 0;
  39. $wxPay = dict::getDict('payWay', 'wxPay');
  40. $aliPay = dict::getDict('payWay', 'alipay');
  41. if ($payWay != $wxPay && $payWay != $aliPay) {
  42. util::fail('暂不支持该支付方式原路退款');
  43. }
  44. $returnCode = $order->returnCode ?? '';
  45. if (empty($returnCode)) {
  46. util::fail('缺少支付流水号,无法原路退款');
  47. }
  48. $shopId = $order->shopId ?? 0;
  49. $shop = ShopClass::getById($shopId, true);
  50. if (empty($shop)) {
  51. util::fail('没有找到门店');
  52. }
  53. $main = MainClass::getLockById($mainId);
  54. if (empty($main)) {
  55. util::fail('没有找到资产信息');
  56. }
  57. $snData = [
  58. 'mainId' => $mainId,
  59. 'shopId' => $shopId,
  60. 'hdId' => $order->hdId ?? 0,
  61. 'customId' => $order->customId ?? 0,
  62. ];
  63. $refundOrderSn = orderSn::getScanPayRefundSn($snData);
  64. $relateOrderSn = $order->orderSn ?? '';
  65. $data = [
  66. 'orderSn' => $refundOrderSn,
  67. 'relateOrderSn' => $relateOrderSn,
  68. 'scanPayId' => $scanPayId,
  69. 'mainId' => $mainId,
  70. 'shopId' => $shopId,
  71. 'hdId' => $order->hdId ?? 0,
  72. 'customId' => $order->customId ?? 0,
  73. 'customName' => $order->customName ?? '',
  74. 'refundPrice' => $refundPrice,
  75. 'payWay' => $payWay,
  76. 'status' => self::STATUS_COMPLETE,
  77. 'shopAdminId' => $post['shopAdminId'] ?? 0,
  78. 'shopAdminName' => $post['shopAdminName'] ?? '',
  79. 'remark' => $post['remark'] ?? '',
  80. 'addTime' => date('Y-m-d H:i:s'),
  81. ];
  82. $refund = self::add($data, true);
  83. $termNo = $shop->lklScanTermNo ?? '';
  84. if ($payWay == $aliPay) {
  85. $termNo = $shop->lklB2BTermNo ?? '';
  86. }
  87. if (empty($shop->lklSjNo) || empty($termNo)) {
  88. util::fail('门店未配置拉卡拉商户信息');
  89. }
  90. $merchantPrivateKeyPath = Yii::getAlias('@vendor/lakala') . '/production/api_private_key.pem';
  91. $lklCertificatePath = Yii::getAlias('@vendor/lakala') . '/production/lkl-apigw-v1.cer';
  92. $params = [
  93. 'appid' => 'OP00002119',
  94. 'serial_no' => '018b08cfddbd',
  95. 'merchant_no' => $shop->lklSjNo,
  96. 'term_no' => $termNo,
  97. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  98. 'lklCertificatePath' => $lklCertificatePath,
  99. ];
  100. $laResource = new Lakala($params);
  101. $refundFee = bcmul($refundPrice, 100);
  102. $aliParams = [
  103. 'refundSn' => $refundOrderSn,
  104. 'orderSn' => $relateOrderSn,
  105. 'refundAmount' => $refundFee,
  106. 'refundReason' => $data['remark'],
  107. 'thirdNo' => $returnCode,
  108. ];
  109. $response = $laResource->refund($aliParams);
  110. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  111. $errMsg = $response['msg'] ?? '退款失败';
  112. util::fail('退款失败:' . $errMsg);
  113. }
  114. $refundTradeNo = $response['resp_data']['trade_no'] ?? '';
  115. $refund->thirdRefundNo = $refundTradeNo;
  116. $refund->save();
  117. $currentTkPrice = bcadd($tkPrice, $refundPrice, 2);
  118. $order->tkPrice = $currentTkPrice;
  119. $order->refundLog = 1;
  120. $order->save();
  121. $capitalType = dict::getDict('capitalType', 'scanPayRefund', 'id');
  122. ShopClass::customScanPayRefundReduceBalance($main, $shop, $refundPrice, $order, $refund, $capitalType);
  123. return $refund;
  124. }
  125. }