|
|
@@ -1,11 +1,15 @@
|
|
|
<?php
|
|
|
/**
|
|
|
* hdApp 中央钱包(可用余额)接口
|
|
|
+ * 用途:变动明细、微信充值下单
|
|
|
*/
|
|
|
namespace hd\controllers;
|
|
|
|
|
|
use bizHd\shop\classes\MainWalletChangeClass;
|
|
|
+use bizHd\shop\classes\MainWalletRechargeClass;
|
|
|
+use bizHd\wx\classes\WxOpenClass;
|
|
|
use common\components\dateUtil;
|
|
|
+use common\components\dict;
|
|
|
use common\components\util;
|
|
|
use Yii;
|
|
|
|
|
|
@@ -50,4 +54,71 @@ class MainWalletController extends BaseController
|
|
|
util::success($list);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 中央钱包微信充值:建未支付单并返回 JSAPI 支付参数
|
|
|
+ * POST:amount、miniOpenId(可选,缺省取当前 admin)
|
|
|
+ */
|
|
|
+ public function actionRecharge()
|
|
|
+ {
|
|
|
+ ini_set('date.timezone', 'Asia/Shanghai');
|
|
|
+ $post = Yii::$app->request->post();
|
|
|
+ $amount = isset($post['amount']) ? floor(((float) $post['amount']) * 100) / 100 : 0;
|
|
|
+ if ($amount <= 0) {
|
|
|
+ util::fail('请输入充值金额');
|
|
|
+ }
|
|
|
+
|
|
|
+ $admin = !empty($this->admin) ? $this->admin->attributes : [];
|
|
|
+ $openId = trim((string) ($post['miniOpenId'] ?? ($admin['miniOpenId'] ?? '')));
|
|
|
+ if ($openId === '') {
|
|
|
+ // 前端可据此走 uni.login 补 openId 后重试
|
|
|
+ util::success(['hasNoMiniOpenId' => 1]);
|
|
|
+ }
|
|
|
+
|
|
|
+ $recharge = MainWalletRechargeClass::createPending(
|
|
|
+ (int) $this->mainId,
|
|
|
+ (int) $this->shopId,
|
|
|
+ $amount
|
|
|
+ );
|
|
|
+ $orderId = (int) ($recharge->id ?? 0);
|
|
|
+ $orderSn = (string) ($recharge->orderSn ?? '');
|
|
|
+ if ($orderId <= 0 || $orderSn === '') {
|
|
|
+ util::fail('创建充值单失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ $capitalType = dict::getDict('capitalType', 'mainWalletRecharge', 'id');
|
|
|
+ $attach = 'capitalType=' . $capitalType . '&mainId=' . (int) $this->mainId;
|
|
|
+
|
|
|
+ $now = time();
|
|
|
+ $expireTime = $now + 300;
|
|
|
+
|
|
|
+ $wx = Yii::getAlias('@vendor/weixin');
|
|
|
+ require_once $wx . '/lib/WxPay.Api.php';
|
|
|
+ require_once $wx . '/example/WxPay.JsApiPay.php';
|
|
|
+ $input = new \WxPayUnifiedOrder();
|
|
|
+ $input->SetBody('中央钱包充值');
|
|
|
+ $input->SetOut_trade_no($orderSn);
|
|
|
+ $input->SetTotal_fee((int) round($amount * 100));
|
|
|
+ $input->SetTime_start(date('YmdHis', $now));
|
|
|
+ $input->SetAttach($attach);
|
|
|
+ $input->SetTime_expire(date('YmdHis', $expireTime));
|
|
|
+ $input->SetNotify_url(Yii::$app->params['hdHost'] . '/notice/wx-callback/');
|
|
|
+ $input->SetTrade_type('JSAPI');
|
|
|
+
|
|
|
+ $merchantExtend = WxOpenClass::getWxInfo();
|
|
|
+ $input->SetOpenid($openId);
|
|
|
+ $merchantExtend['wxAppId'] = $merchantExtend['miniAppId'];
|
|
|
+
|
|
|
+ $wxOrder = \WxPayApi::unifiedOrder($input, 6, $merchantExtend);
|
|
|
+ $tools = new \JsApiPay();
|
|
|
+ $jsApiParameters = $tools->GetJsApiParameters($wxOrder, $merchantExtend);
|
|
|
+ $newParams = json_decode($jsApiParameters, true) ?: [];
|
|
|
+ $newParams['orderId'] = $orderId;
|
|
|
+ $newParams['orderSn'] = $orderSn;
|
|
|
+ $newParams['needPay'] = 1;
|
|
|
+ $newParams['paid'] = 0;
|
|
|
+ $newParams['amount'] = $amount;
|
|
|
+ $newParams['hasNoMiniOpenId'] = 0;
|
|
|
+ util::success($newParams);
|
|
|
+ }
|
|
|
+
|
|
|
}
|