diff --git a/api/controllers/WxPaymentController.php b/api/controllers/WxPaymentController.php new file mode 100644 index 0000000..9004f62 --- /dev/null +++ b/api/controllers/WxPaymentController.php @@ -0,0 +1,26 @@ +object->wxPayment(); + } + + public function actionMiniProgram() + { + } +} \ No newline at end of file diff --git a/api/logic/WXPaymentLogic.php b/api/logic/WXPaymentLogic.php new file mode 100644 index 0000000..d943db8 --- /dev/null +++ b/api/logic/WXPaymentLogic.php @@ -0,0 +1,112 @@ +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; + } + + + +} \ No newline at end of file