You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
2.7 KiB

<?php
namespace backend\modules\shop\logic\payment;
use api\logic\Helper;
use api\logic\WxPaymentLogic;
use backend\modules\shop\models\ars\PaymentLog;
use backend\modules\shop\models\ars\RefundLog;
use yii\db\Exception;
use yii\web\BadRequestHttpException;
use yii\web\NotFoundHttpException;
use Yii;
class WxPaymentManager extends WxPaymentLogic
{
/**
* @param RefundLog $refundLog
* @param int $operatorId
* @return array|mixed
* @throws BadRequestHttpException
* @throws NotFoundHttpException
*/
public function refund($refundLog, $operatorId)
{
$paymentLog = PaymentLog::findOne(['order_id' => $refundLog->order_id]);
if (empty($paymentLog)) {
throw new NotFoundHttpException('订单支付信息未找到');
}
$refundLog = RefundLog::findOne(['order_id' => $orderId, 'status' => self::STATUS_REFUND_WAIT]);
if (!$refundLog) {
throw new NotFoundHttpException('订单退款信息未找到');
}
/*参数分别为:微信订单号、商户退款单号、订单金额、退款金额、其他参数*/
$this->getApp();
$config = ['refund_desc' => '退款'];
$result = $this->app->refund->byTransactionId(
$paymentLog->wx_refund_id,
$refundLog->wx_refund_id,
round($paymentLog->payment_amount * 100),
round($refundLog->refund_amount * 100),
$config
);
Yii::info($result, 'refund_log');
if ($result['return_code'] == 'FAIL' || $result['return_msg'] != 'OK' || $result['result_code'] == 'FAIL') {
throw new BadRequestHttpException($result['return_msg']);
}
if ($result['err_code_des']) {
throw new BadRequestHttpException($result['err_code_des']);
}
$tra = Yii::$app->db->beginTransaction();
try {
$paymentLog->status = $refundLog->refund_amount < $paymentLog->payment_amount ? self::STATUS_REFUND_SUCCESS : self::STATUS_REFUND_PORTION;
if (!$paymentLog->save()) {
throw new Exception(Helper::errorMessageStr($paymentLog->errors));
}
$refundLog->operator_id = $operatorId;
$refundLog->status = $refundLog->refund_amount < $paymentLog->payment_amount ? self::STATUS_REFUND_SUCCESS : self::STATUS_REFUND_PORTION;
$refundLog->finished_at = time();
if (!$refundLog->save()) {
throw new Exception(Helper::errorMessageStr($refundLog->errors));
}
$tra->commit();
return ['status' => 'true'];
} catch (Exception $e) {
$tra->rollBack();
return ['status' => false, 'info' => $e->getMessage()];
}
}
}