|
@ -8,11 +8,15 @@ |
|
|
|
|
|
|
|
|
namespace api\logic; |
|
|
namespace api\logic; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use backend\modules\shop\models\ars\Config; |
|
|
|
|
|
use Yii; |
|
|
use backend\modules\shop\models\ars\Order; |
|
|
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 $appId; |
|
|
public $mchId; |
|
|
public $mchId; |
|
|
public $key; |
|
|
public $key; |
|
@ -22,32 +26,12 @@ class WXPaymentLogic extends Component |
|
|
public $tradeType; |
|
|
public $tradeType; |
|
|
public $payType; |
|
|
public $payType; |
|
|
public $app; |
|
|
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() |
|
|
public function wxPayment() |
|
|
{ |
|
|
{ |
|
|
$this->payType = 1; |
|
|
|
|
|
|
|
|
$this->payType = self::PAY_TYPE_WEB; |
|
|
$data = $this->applyPaymentData(); |
|
|
$data = $this->applyPaymentData(); |
|
|
$this->unify($data); |
|
|
$this->unify($data); |
|
|
} |
|
|
} |
|
@ -73,22 +57,39 @@ class WXPaymentLogic extends Component |
|
|
'total_fee' => round($order->pay_fee * 100), |
|
|
'total_fee' => round($order->pay_fee * 100), |
|
|
'openid' => Yii::$app->user->identity->wx_openid ?? '', |
|
|
'openid' => Yii::$app->user->identity->wx_openid ?? '', |
|
|
]; |
|
|
]; |
|
|
|
|
|
$this->tradeType = 'JSAPI'; |
|
|
|
|
|
$this->order = $order; |
|
|
return $data; |
|
|
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() |
|
|
private function _config() |
|
|
{ |
|
|
{ |
|
|
$path = Yii::getAlias('@backend'); |
|
|
$path = Yii::getAlias('@backend'); |
|
|
$config = Config::find()->one(); |
|
|
$config = Config::find()->one(); |
|
|
switch ($this->payType) { |
|
|
switch ($this->payType) { |
|
|
case 1: |
|
|
|
|
|
|
|
|
case self::PAY_TYPE_WEB: |
|
|
$this->appId = trim($config->wx_appId); |
|
|
$this->appId = trim($config->wx_appId); |
|
|
$this->mchId = trim($config->wx_mchId); |
|
|
$this->mchId = trim($config->wx_mchId); |
|
|
$this->key = trim($config->wx_key); |
|
|
$this->key = trim($config->wx_key); |
|
|
$this->certPath = trim($path . $config->wx_certPath); |
|
|
$this->certPath = trim($path . $config->wx_certPath); |
|
|
$this->keyPath = trim($path . $config->wx_keyPath); |
|
|
$this->keyPath = trim($path . $config->wx_keyPath); |
|
|
break; |
|
|
break; |
|
|
case 2: |
|
|
|
|
|
|
|
|
case self::PAY_TYPE_MINI_PROGRAM: |
|
|
$this->appId = trim($config->mini_program_appId); |
|
|
$this->appId = trim($config->mini_program_appId); |
|
|
$this->mchId = trim($config->mini_program_mchId); |
|
|
$this->mchId = trim($config->mini_program_mchId); |
|
|
$this->key = trim($config->mini_program_key); |
|
|
$this->key = trim($config->mini_program_key); |
|
@ -96,15 +97,15 @@ class WXPaymentLogic extends Component |
|
|
$this->keyPath = trim($path . $config->mini_program_keyPath); |
|
|
$this->keyPath = trim($path . $config->mini_program_keyPath); |
|
|
break; |
|
|
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) |
|
|
private function unify($data) |
|
|
{ |
|
|
{ |
|
|
$result = $app->order->unify($this->app); |
|
|
|
|
|
return $result; |
|
|
|
|
|
|
|
|
$this->getPaymentApp(); |
|
|
|
|
|
return $this->app->order->unify($data); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|