| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- <?php
- namespace bizGhs\order\classes;
- use biz\shop\classes\ShopClass;
- use bizGhs\shop\classes\MainClass;
- use common\components\dict;
- use common\components\lakala\Lakala;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- class ScanPayRefundClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\order\models\ScanPayRefund';
- const STATUS_COMPLETE = 1;
- public static function createRefund($scanPayId, $post)
- {
- $refundPrice = round($post['price'] ?? 0, 2);
- if ($refundPrice <= 0) {
- util::fail('请填写退款金额');
- }
- $mainId = $post['mainId'] ?? 0;
- $order = ScanPayClass::getLockById($scanPayId);
- if (empty($order)) {
- util::fail('没有找到流水');
- }
- if ($order->mainId != $mainId) {
- util::fail('没有权限');
- }
- if ($order->status != 2 || $order->payStatus != 1) {
- util::fail('仅已付款流水可退款');
- }
- $actPrice = $order->actPrice ?? 0;
- $tkPrice = $order->tkPrice ?? 0;
- $canRefund = bcsub($actPrice, $tkPrice, 2);
- if ($refundPrice > $canRefund) {
- util::fail('退款金额超过可退金额');
- }
- $payWay = $order->payWay ?? 0;
- $wxPay = dict::getDict('payWay', 'wxPay');
- $aliPay = dict::getDict('payWay', 'alipay');
- if ($payWay != $wxPay && $payWay != $aliPay) {
- util::fail('暂不支持该支付方式原路退款');
- }
- $returnCode = $order->returnCode ?? '';
- if (empty($returnCode)) {
- util::fail('缺少支付流水号,无法原路退款');
- }
- $shopId = $order->shopId ?? 0;
- $shop = ShopClass::getById($shopId, true);
- if (empty($shop)) {
- util::fail('没有找到门店');
- }
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有找到资产信息');
- }
- $snData = [
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'hdId' => $order->hdId ?? 0,
- 'customId' => $order->customId ?? 0,
- ];
- $refundOrderSn = orderSn::getScanPayRefundSn($snData);
- $relateOrderSn = $order->orderSn ?? '';
- $data = [
- 'orderSn' => $refundOrderSn,
- 'relateOrderSn' => $relateOrderSn,
- 'scanPayId' => $scanPayId,
- 'mainId' => $mainId,
- 'shopId' => $shopId,
- 'hdId' => $order->hdId ?? 0,
- 'customId' => $order->customId ?? 0,
- 'customName' => $order->customName ?? '',
- 'refundPrice' => $refundPrice,
- 'payWay' => $payWay,
- 'status' => self::STATUS_COMPLETE,
- 'shopAdminId' => $post['shopAdminId'] ?? 0,
- 'shopAdminName' => $post['shopAdminName'] ?? '',
- 'remark' => $post['remark'] ?? '',
- 'addTime' => date('Y-m-d H:i:s'),
- ];
- $refund = self::add($data, true);
- $termNo = $shop->lklScanTermNo ?? '';
- if ($payWay == $aliPay) {
- $termNo = $shop->lklB2BTermNo ?? '';
- }
- if (empty($shop->lklSjNo) || empty($termNo)) {
- util::fail('门店未配置拉卡拉商户信息');
- }
- $merchantPrivateKeyPath = Yii::getAlias('@vendor/lakala') . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias('@vendor/lakala') . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $termNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundFee = bcmul($refundPrice, 100);
- $aliParams = [
- 'refundSn' => $refundOrderSn,
- 'orderSn' => $relateOrderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => $data['remark'],
- 'thirdNo' => $returnCode,
- ];
- $response = $laResource->refund($aliParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '退款失败';
- util::fail('退款失败:' . $errMsg);
- }
- $refundTradeNo = $response['resp_data']['trade_no'] ?? '';
- $refund->thirdRefundNo = $refundTradeNo;
- $refund->save();
- $currentTkPrice = bcadd($tkPrice, $refundPrice, 2);
- $order->tkPrice = $currentTkPrice;
- $order->refundLog = 1;
- $order->save();
- $capitalType = dict::getDict('capitalType', 'scanPayRefund', 'id');
- ShopClass::customScanPayRefundReduceBalance($main, $shop, $refundPrice, $order, $refund, $capitalType);
- return $refund;
- }
- }
|