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.
 
 
 

113 lines
3.1 KiB

<?php
/**
* Created by PhpStorm.
* User: travis
* Date: 2019/12/12
* Time: 6:20
*/
namespace api\logic;
use backend\modules\shop\models\ars\Config;
use Yii;
use backend\modules\shop\models\ars\Order;
class WXPaymentLogic extends \yii\base\BaseObject
{
const PAY_TYPE_WEB = 1;
const PAY_TYPE_MINI_PROGRAM = 2;
public $appId;
public $mchId;
public $key;
public $certPath;
public $keyPath;
public $notifyUrl;
public $tradeType;
public $payType;
public $app;
public $order;
public function wxPayment()
{
$this->payType = self::PAY_TYPE_WEB;
$data = $this->applyPaymentData();
$this->unify($data);
}
/**
* @return array
*/
private function applyPaymentData()
{
$orderId = Yii::$app->request->getBodyParam('orderId');/*int 商品id*/
if (empty($orderId)) {
throw new BadRequestHttpException(Helper::REQUEST_BAD_PARAMS);
}
$order = Order::find()
->where(['id' => $orderId, 'user_id' => Yii::$app->user->getId()])
->one();
if (empty($order)) {
throw new NotFoundHttpException('未找到该订单');
}
$data = [
'body' => '订单支付',
'out_trade_no' => $order->order_sn,
'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 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 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);
$this->certPath = trim($path . $config->mini_program_certPath);
$this->keyPath = trim($path . $config->mini_program_keyPath);
break;
}
$this->notifyUrl = $config->api_domain_name . $this->order->notify_url;
}
private function unify($data)
{
$this->getPaymentApp();
return $this->app->order->unify($data);
}
}