2 changed files with 138 additions and 0 deletions
@ -0,0 +1,26 @@ |
|||
<?php |
|||
/** |
|||
* Created by PhpStorm. |
|||
* User: travis |
|||
* Date: 2019/12/12 |
|||
* Time: 15:59 |
|||
*/ |
|||
|
|||
namespace api\controllers; |
|||
|
|||
|
|||
class WxPaymentController extends CommonController |
|||
{ |
|||
public $modelClass = 'backend\modules\shop\models\ars\Order'; |
|||
public $className = 'api\logic\WXPaymentLogic'; |
|||
|
|||
|
|||
public function actionWeb() |
|||
{ |
|||
return $this->object->wxPayment(); |
|||
} |
|||
|
|||
public function actionMiniProgram() |
|||
{ |
|||
} |
|||
} |
@ -0,0 +1,112 @@ |
|||
<?php |
|||
/** |
|||
* Created by PhpStorm. |
|||
* User: travis |
|||
* Date: 2019/12/12 |
|||
* Time: 6:20 |
|||
*/ |
|||
|
|||
namespace api\logic; |
|||
|
|||
|
|||
use backend\modules\shop\models\ars\Order; |
|||
|
|||
class WXPaymentLogic extends Component |
|||
{ |
|||
public $appId; |
|||
public $mchId; |
|||
public $key; |
|||
public $certPath; |
|||
public $keyPath; |
|||
public $notifyUrl; |
|||
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 function wxPayment() |
|||
{ |
|||
$this->payType = 1; |
|||
$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 ?? '', |
|||
]; |
|||
return $data; |
|||
} |
|||
|
|||
private function _config() |
|||
{ |
|||
$path = Yii::getAlias('@backend'); |
|||
$config = Config::find()->one(); |
|||
switch ($this->payType) { |
|||
case 1: |
|||
$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: |
|||
$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 . '/orders/notify'; |
|||
$this->tradeType = 'JSAPI'; |
|||
} |
|||
|
|||
|
|||
private function unify($data) |
|||
{ |
|||
$result = $app->order->unify($this->app); |
|||
return $result; |
|||
} |
|||
|
|||
|
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue