diff --git a/api/logic/WXPaymentLogic.php b/api/logic/WXPaymentLogic.php index d943db8..36af8e5 100644 --- a/api/logic/WXPaymentLogic.php +++ b/api/logic/WXPaymentLogic.php @@ -8,11 +8,15 @@ namespace api\logic; - +use backend\modules\shop\models\ars\Config; +use Yii; use backend\modules\shop\models\ars\Order; -class WXPaymentLogic extends Component +class WXPaymentLogic extends \yii\base\BaseObject { + const PAY_TYPE_WEB = 1; + const PAY_TYPE_MINI_PROGRAM = 2; + public $appId; public $mchId; public $key; @@ -22,32 +26,12 @@ class WXPaymentLogic extends Component public $tradeType; public $payType; public $app; - public function init() - { - parent::init(); - $this->app = self::_getPaymentApp(); - } - - private static function _getPaymentApp() - { - $this->_config(); - $config = [ - 'app_id' => $this->appId, - 'mch_id' => $this->mchId, - 'key' => $this->key, - 'cert_path' => $this->certPath, - 'key_path' => $this->keyPath, - 'notify_url' => $this->notifyUrl, - 'trade_type' => $this->tradeType - ]; - $app = Factory::payment($config); - return $app; - } + public $order; public function wxPayment() { - $this->payType = 1; + $this->payType = self::PAY_TYPE_WEB; $data = $this->applyPaymentData(); $this->unify($data); } @@ -73,22 +57,39 @@ class WXPaymentLogic extends Component 'total_fee' => round($order->pay_fee * 100), 'openid' => Yii::$app->user->identity->wx_openid ?? '', ]; + $this->tradeType = 'JSAPI'; + $this->order = $order; return $data; } + private function getPaymentApp() + { + $this->_config(); + $config = [ + 'app_id' => $this->appId, + 'mch_id' => $this->mchId, + 'key' => $this->key, + 'cert_path' => $this->certPath, + 'key_path' => $this->keyPath, + 'notify_url' => $this->notifyUrl, + 'trade_type' => $this->tradeType + ]; + $this->app = Factory::payment($config); + } + private function _config() { $path = Yii::getAlias('@backend'); $config = Config::find()->one(); switch ($this->payType) { - case 1: + case self::PAY_TYPE_WEB: $this->appId = trim($config->wx_appId); $this->mchId = trim($config->wx_mchId); $this->key = trim($config->wx_key); $this->certPath = trim($path . $config->wx_certPath); $this->keyPath = trim($path . $config->wx_keyPath); break; - case 2: + case self::PAY_TYPE_MINI_PROGRAM: $this->appId = trim($config->mini_program_appId); $this->mchId = trim($config->mini_program_mchId); $this->key = trim($config->mini_program_key); @@ -96,15 +97,15 @@ class WXPaymentLogic extends Component $this->keyPath = trim($path . $config->mini_program_keyPath); break; } - $this->notifyUrl = $config->api_domain_name . '/orders/notify'; - $this->tradeType = 'JSAPI'; + $this->notifyUrl = $config->api_domain_name . $this->order->notify_url; + } private function unify($data) { - $result = $app->order->unify($this->app); - return $result; + $this->getPaymentApp(); + return $this->app->order->unify($data); }