123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\logic;
-
-
- use app\common\enum\IntegralGoodsEnum;
- use app\common\enum\IntegralOrderEnum;
- use app\common\enum\PayEnum;
- use app\common\model\integral\IntegralGoods;
- use app\common\model\integral\IntegralOrder;
- use app\common\model\AccountLog;
- use app\common\model\integral\IntegralOrderRefund;
- use app\common\model\user\User;
- use app\common\server\AliPayServer;
- use app\common\server\DouGong\pay\PayZhengsaoRefund;
- use app\common\server\WeChatPayServer;
- use app\common\server\WeChatServer;
- use think\Exception;
-
-
- class IntegralOrderRefundLogic
- {
-
-
-
- public static function cancelOrder(int $order_id)
- {
-
- $order = IntegralOrder::findOrEmpty($order_id);
- $order->cancel_time = time();
- $order->order_status = IntegralOrderEnum::ORDER_STATUS_DOWN;
- $order->save();
-
-
- $goods_snap = $order['goods_snap'];
-
- IntegralGoods::where([['id', '=', $goods_snap['id']], ['sales', '>=', $order['total_num']]])
- ->inc('stock', $order['total_num'])
- ->dec('sales', $order['total_num'])
- ->update();
- }
-
-
-
-
- public static function refundOrderAmount(int $order_id)
- {
-
- $order = IntegralOrder::findOrEmpty($order_id);
-
- $goods_snap = $order['goods_snap'];
-
-
- if ($goods_snap['type'] == IntegralGoodsEnum::TYPE_GOODS
- && $order['refund_status'] == IntegralOrderEnum::NO_REFUND
- ) {
- if ($order['order_amount'] <= 0) {
- return true;
- }
-
- IntegralOrder::where(['id' => $order['id']])->update([
- 'refund_status' => IntegralOrderEnum::IS_REFUND,
- 'refund_amount' => $order['order_amount'],
- ]);
-
-
- $refund_log = self::addRefundLog($order, $order['order_amount'], 1, $order['order_amount']);
-
- switch ($order['pay_way']) {
-
- case PayEnum::BALANCE_PAY:
- self::balancePayRefund($order, $order['order_amount']);
- break;
-
- case PayEnum::WECHAT_PAY:
- self::wechatPayRefund($order, $refund_log);
- break;
-
- case PayEnum::ALI_PAY:
- self::aliPayRefund($order, $refund_log);
- break;
- case PayEnum::HFDG_WECHAT:
- case PayEnum::HFDG_ALIPAY:
- $payZsRefund = new PayZhengsaoRefund([
- 'refund' => [
- 'id' => $refund_log['id'],
- 'money' => $order['order_amount'],
- ],
- 'order' => [
- 'id' => $order['id'],
- 'transaction_id' => $order['transaction_id'],
- 'hfdg_params' => $order['hfdg_params'],
- ],
- 'from' => 'integral',
- ]);
- $result = $payZsRefund->request()->getRefundResult();
- if ($result['code'] != 1) {
- throw new \Exception($result['msg']);
- }
- break;
- }
- }
- return true;
- }
-
-
-
-
- public static function refundOrderIntegral($id)
- {
- $order = IntegralOrder::findOrEmpty($id);
- if ($order['order_integral'] > 0) {
-
- User::where(['id' => $order['user_id']])
- ->inc('user_integral', $order['order_integral'])
- ->update();
-
- AccountLogLogic::AccountRecord(
- $order['user_id'],
- $order['order_integral'], 1,
- AccountLog::cancel_integral_order,
- '', $order['id'], $order['order_sn']
- );
-
- IntegralOrder::where(['id' => $id])->update([
- 'refund_integral' => $order['order_integral']
- ]);
- }
- return true;
- }
-
-
-
-
-
- public static function addRefundLog($order, $refund_amount, $status, $msg = '')
- {
- return IntegralOrderRefund::create([
- 'order_id' => $order['id'],
- 'user_id' => $order['user_id'],
- 'refund_sn' => createSn('order_refund', 'refund_sn'),
- 'order_amount' => $order['order_amount'],
- 'refund_amount' => $refund_amount,
- 'transaction_id' => $order['transaction_id'],
- 'create_time' => time(),
- 'refund_status' => $status,
- 'refund_at' => time(),
- 'refund_msg' => json_encode($msg, JSON_UNESCAPED_UNICODE),
- ]);
- }
-
-
-
-
- public static function wechatPayRefund($order, $refund)
- {
- $config = WeChatServer::getPayConfigBySource($order['order_source'])['config'];
- if (empty($config)) {
- throw new Exception('请联系管理员设置微信相关配置!');
- }
- if (!isset($config['cert_path']) || !isset($config['key_path'])) {
- throw new Exception('请联系管理员设置微信证书!');
- }
- if (!file_exists($config['cert_path']) || !file_exists($config['key_path'])) {
- throw new Exception('微信证书不存在,请联系管理员!');
- }
-
- $result = WeChatPayServer::refund($config, [
- 'transaction_id' => $order['transaction_id'],
- 'refund_sn' => $refund['refund_sn'],
- 'total_fee' => intval(strval($refund['order_amount'] * 100)),
- 'refund_fee' => intval(strval($refund['refund_amount'] * 100)),
- ]);
-
- if (isset($result['return_code']) && $result['return_code'] == 'FAIL') {
- throw new Exception($result['return_msg']);
- }
- if (isset($result['err_code_des'])) {
- throw new Exception($result['err_code_des']);
- }
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
-
- IntegralOrderRefund::where(['id' => $refund['id']])->update([
- 'wechat_refund_id' => $result['refund_id'] ?? 0,
- 'refund_msg' => json_encode($result, JSON_UNESCAPED_UNICODE),
- ]);
- } else {
- throw new Exception('微信支付退款失败');
- }
- }
-
-
-
-
- public static function aliPayRefund($order, $refund)
- {
- $result = (new AliPayServer())->refund($order['order_sn'], $order['order_amount']);
-
- if ($result['code'] == '10000' && $result['msg'] == 'Success' && $result['fund_change'] == 'Y') {
-
- IntegralOrderRefund::where(['id' => $refund])->update([
- 'refund_msg' => json_encode($result, JSON_UNESCAPED_UNICODE),
- ]);
- } else {
- throw new Exception('支付宝退款失败');
- }
- }
-
-
-
-
-
- public static function balancePayRefund($order, $refund_amount)
- {
- $user = User::find($order['user_id']);
- $user->user_money = ['inc', $refund_amount];
- $user->save();
-
- AccountLogLogic::AccountRecord(
- $order['user_id'],
- $refund_amount,
- 1,
- AccountLog::cancel_order_refund,
- '',
- $order['id'],
- $order['order_sn']
- );
- return true;
- }
-
-
-
- }
|