|
|
@@ -62,17 +62,21 @@ class PaymentService extends BaseService
|
|
|
|
|
|
/**
|
|
|
* 创建支付
|
|
|
- * @param string $scene 场景,deposit-保证金,depositRefund-保证金退款,withdraw-提现
|
|
|
+ * @param string $scene 场景,vip-VIP会员
|
|
|
* @param int $payType
|
|
|
+ * @param int $isMin 是否是小程序
|
|
|
* @return false|\Yansongda\Pay\Provider\Alipay|Wechat
|
|
|
*/
|
|
|
- public function createPay($scene, $payType = 10)
|
|
|
+ public function createPay($scene, $payType = 10, $payPt= '')
|
|
|
{
|
|
|
$config = ConfigService::make()->getConfigOptionByGroup(6);
|
|
|
if ($payType == 10) {
|
|
|
$appid = isset($config['wxpay_appid']) ? $config['wxpay_appid'] : '';
|
|
|
+ $mpAppid = isset($config['wxpay_mp_appid']) ? $config['wxpay_mp_appid'] : '';
|
|
|
+ $minAppid = isset($config['wxpay_min_appid']) ? $config['wxpay_min_appid'] : '';
|
|
|
$mchid = isset($config['wxpay_mchd']) ? $config['wxpay_mchd'] : '';
|
|
|
$secretV3Key = isset($config['wxpay_key_v3']) ? $config['wxpay_key_v3'] : '';
|
|
|
+ $secretV2Key = isset($config['wxpay_key_v2']) ? $config['wxpay_key_v2'] : '';
|
|
|
$wxpaySecretCert = isset($config['wxpay_secret_cert']) ? $config['wxpay_secret_cert'] : '';
|
|
|
$wxpayPublicCert = isset($config['wxpay_public_cert']) ? $config['wxpay_public_cert'] : '';
|
|
|
if (empty($appid) || empty($mchid) || empty($secretV3Key)) {
|
|
|
@@ -83,8 +87,23 @@ class PaymentService extends BaseService
|
|
|
// 支付参数
|
|
|
$payConfig = config('payment.wechat');
|
|
|
$payConfig['wechat']['default']['mch_id'] = $mchid;
|
|
|
- $payConfig['wechat']['default']['app_id'] = $appid;
|
|
|
- $payConfig['wechat']['default']['mch_secret_key'] = $secretV3Key;
|
|
|
+ if($payPt == 'min'){
|
|
|
+ // 小程序支付
|
|
|
+ $payConfig['wechat']['default']['mini_app_id'] = $minAppid? $minAppid : $appid;
|
|
|
+ }else if($payPt == 'mp'){
|
|
|
+ // 公众号
|
|
|
+ $payConfig['wechat']['default']['mp_app_id'] = $mpAppid? $mpAppid : $appid;
|
|
|
+ }else{
|
|
|
+ // APP支付
|
|
|
+ $payConfig['wechat']['default']['app_id'] = $appid;
|
|
|
+ }
|
|
|
+
|
|
|
+ if($secretV3Key){
|
|
|
+ $payConfig['wechat']['default']['mch_secret_key'] = $secretV3Key;
|
|
|
+ }else if($secretV2Key){
|
|
|
+ $payConfig['wechat']['default']['mch_secret_key_v2'] = $secretV2Key;
|
|
|
+ }
|
|
|
+
|
|
|
if ($wxpaySecretCert) {
|
|
|
$payConfig['wechat']['default']['mch_secret_cert'] = $wxpaySecretCert;
|
|
|
}
|
|
|
@@ -131,6 +150,83 @@ class PaymentService extends BaseService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
+ * 微信小程序支付
|
|
|
+ * @param $userInfo
|
|
|
+ * @param $order
|
|
|
+ * @param string $scene
|
|
|
+ * @return false|\Yansongda\Supports\Collection
|
|
|
+ */
|
|
|
+ public function minPay($userInfo, $order, $scene = 'pay')
|
|
|
+ {
|
|
|
+ $amount = isset($order['pay_money']) ? $order['pay_money'] : 0;
|
|
|
+ $openid = isset($order['openid']) ? $order['openid'] : '';
|
|
|
+ if ($amount < 0) {
|
|
|
+ $this->error = 2615;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(empty($openid)){
|
|
|
+ $this->error = 2614;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $outTradeNo = isset($order['order_no']) && $order['order_no'] ? $order['order_no'] : get_order_num('PR');
|
|
|
+ // 是否调用过支付,是则用新的支付单号
|
|
|
+ if ($outTradeNo && $this->model->where(['out_trade_no' => $outTradeNo, 'mark' => 1])->value('id')) {
|
|
|
+ $outTradeNo = $outTradeNo . date('is') . rand(1, 9);
|
|
|
+ }
|
|
|
+ $body = isset($order['body']) ? $order['body'] : '';
|
|
|
+ $payData = [
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
+ 'description' => $body ? $body : '订单支付',
|
|
|
+ 'amount' => [
|
|
|
+ 'total' => intval($amount * 100),
|
|
|
+ 'currency' => 'CNY'
|
|
|
+ ],
|
|
|
+ 'payer' => [
|
|
|
+ 'openid' => $openid,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+
|
|
|
+ // 创建支付
|
|
|
+ try {
|
|
|
+ $pay = $this->createPay($scene, 10, 'min');
|
|
|
+ RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}", ['order' => $order, 'config' => $this->config], 7200);
|
|
|
+ if (empty($pay)) {
|
|
|
+ $this->error = 2616;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ $pay = $pay->mini($payData);
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ RedisService::set("caches:payments:wechat:{$scene}_{$outTradeNo}_error", ['order' => $order,'error'=>$exception->getTrace(), 'config' => $this->config], 7200);
|
|
|
+ $this->error = $exception->getMessage();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($pay->package) {
|
|
|
+ $data = [
|
|
|
+ 'user_id' => $userInfo['id'],
|
|
|
+ 'out_trade_no' => $outTradeNo,
|
|
|
+ 'order_no' => $order['order_no'],
|
|
|
+ 'params' => json_encode($pay, 256),
|
|
|
+ 'total_fee' => $amount,
|
|
|
+ 'pay_type' => 10,
|
|
|
+ 'create_time' => time(),
|
|
|
+ 'status' => 2,
|
|
|
+ 'mark' => 1,
|
|
|
+ ];
|
|
|
+ if ($this->model->insertGetId($data)) {
|
|
|
+ $this->error = 2617;
|
|
|
+ return $pay;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $this->error = 2618;
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
* 微信支付
|
|
|
* @param $userInfo
|
|
|
* @param $order
|