jh-admin/addon/cardservice/api/controller/Pay.php

201 lines
9.4 KiB
PHP

<?php
/**
* SAAS应用系统 --- 十年开发经验汇集巨献!
* ==========================================================
* Copy right 2020-2050 成都众联思索科技有限公司,保留所有权利。
* ----------------------------------------------------------
* 官方网址: https://www.zoomtk.com
* 这不是自由软件!未经允许不得用于商业目或程序代码摘取及修改。
* 任何企业和个人未经允许对程序代码以任何形式任何目的再发布传播。
* 唯一发布渠道www.zoomtk.com;非官方渠道统一视为侵权行为。
* ==========================================================
*/
namespace addon\cardservice\api\controller;
use app\api\controller\BaseApi;
use think\facade\Db;
class Pay extends BaseApi
{
public function info()
{
$out_trade_no = $this->params['out_trade_no'];
$store_id = $this->params['store_id'] ?? 0;
$json = [
[
'order_goods gs',
'a.order_id = gs.order_id',
'left'
],
[
'goods_card c',
'gs.goods_id = c.goods_id',
'left'
]
];
$order_info = model('order')
->getInfo([['a.out_trade_no', '=', $out_trade_no]], 'a.order_id,a.order_no,a.order_type,a.order_name,a.store_id,a.order_from,a.promotion_type,a.promotion_status_name,
c.goods_id,c.discount_type,c.downPaymentPeriods,c.downPaymentDate,c.validity_type,c.validity_day,c.validity_time,c.average_original_price,c.downPayment,c.perPeriodPriceArray,c.average_discount_price,c.cloud_card_type,c.common_num,c.periods,c.card_template_id', 'a', $json);
if (!empty($order_info)) {
$original_price = $order_info['average_original_price'];
$deductionAmount = $order_info['average_discount_price'];
$down_payment = $order_info['downPayment'];
$card_type = $order_info['cloud_card_type'];
$periods = $order_info['periods'] ?: $order_info['common_num'];
$down_payment_periods = $order_info['downPaymentPeriods'];
if ($order_info['order_from'] == 'cashier' && $order_info['promotion_type'] == 'cardservice') {
$promotion_info = json_decode($order_info['promotion_status_name'], true);
$original_price = $promotion_info['average_original_price'];
$deductionAmount = $promotion_info['average_discount_price'];
$down_payment = $promotion_info['downPayment'];
$card_type = $promotion_info['cloud_card_type'];
$periods = $promotion_info['periods'];
$down_payment_periods = $promotion_info['downPaymentPeriods'];
}
} else {
return $this->error(-1, '订单不存在');
}
$merchantPid = '';
$shop_id = '';
if ($store_id && $order_info['store_id'] == $store_id) {
$storeInfo = Db::name('store')->cache('store_info' . $store_id)->where('store_id', $store_id)->find();
if (!empty($storeInfo) && $storeInfo['is_settlement'] == 1 && $storeInfo['settlement_type'] == 1 && $storeInfo['merchant_smid'] != '') {
$merchantPid = $storeInfo['merchant_smid'];
if ($storeInfo['is_mystore']) {
$shop_id = json_decode($storeInfo['storeinfo'], true)['shop_id'];
}
}
}
if ($merchantPid == '') {
$pay = new \addon\alipay\model\Config();
$payConfig = $pay->getPayConfig(request()->siteid())['data']['value'];
if ($payConfig && isset($payConfig['merchant_smid']) && $payConfig['merchant_smid']) {
$merchantPid = $payConfig['merchant_smid'];
} else {
$where = [
'site_id' => request()->siteid(),
'is_platform' => 1,
'pay_type' => 'alipay',
];
$merchantPid = Db::name('pay_shop')->where($where)->cache(json_encode($where))->value('merchant_smid');
if (empty($merchantPid)) {
cache(json_encode($where), null);
}
}
}
$proxy_url = config('alipay.cloudAlipay.proxy_url');
if ($proxy_url) { //代理模式
$post = [
'userId' => $out_trade_no,
'domain' => addon_url('pay/pay/cloudnotify'),
'site_id' => request()->siteid()
];
$this->http_post_data($proxy_url, $post);
}
$pub = [
'merchantPid' => $merchantPid, //商户pid
'productName' => trim($order_info['order_name']), //产品秒杀
'productNo' => $order_info['goods_id'],
'nickName' => '支付宝用户',
'outSubscriptionNo' => $out_trade_no,
'deductionAmount' => sprintf("%.2f", $deductionAmount), //每期费用
'periods' => (int)$periods, //总期数
'downPayment' => $down_payment, //优惠价格
'downPaymentPeriods' => $down_payment_periods, //优惠期数
// 'shopId' => '2024052500502007090093617395',
// 'productUrl' => '/pages_market/goods/detail?goods_id='.$order_info['goods_id'],
];
if ($pub['downPaymentPeriods'] == 0) {
unset($pub['downPayment'], $pub['downPaymentPeriods']);
}
$downPaymentDate = 0;
if ($order_info['downPaymentDate'] > 0) { //首期扣款时间
$downPaymentDate = $order_info['downPaymentDate'];
}
if ($shop_id) {
// $pub['shopId'] = $shop_id;
}
switch ($card_type) {
case 'week':
$data = [];
$info['initData']['openWeek'] = array_merge($pub, $data);
break;
case 'month':
$data = [];
$info['initData']['openMonth'] = array_merge($pub, $data);
break;
case 'discount_month':
if (!$downPaymentDate) $downPaymentDate = 30;
$data = [
'enableUserActivePay' => 'Y', //主动付款
'originalPrice' => sprintf("%.2f", $original_price), //订购原价
'downPaymentDate' => date("Y-m-d", strtotime("+ {$downPaymentDate} day")), //首期扣款时间 当前时间增加7天
];
$info['initData']['openDiscountMonth'] = array_merge($pub, $data);
break;
case 'discount_week':
if (!$downPaymentDate) $downPaymentDate = 7;
$data = [
'enableUserActivePay' => 'Y', //主动付款
'originalPrice' => sprintf("%.2f", $original_price), //订购原价
'downPaymentDate' => date("Y-m-d", strtotime("+ {$downPaymentDate} day")), //首期扣款时间 当前时间增加7天
];
$info['initData']['openDiscountWeek'] = array_merge($pub, $data);
break;
case 'num':
$data = [];
$info['initData']['openTime'] = array_merge($pub, $data);
break;
case 'discount_num':
$data = [
'enableUserActivePay' => 'Y', //主动付款
'existVerificationCode' => 'Y',//主动付款
'originalPrice' => sprintf("%.2f", $original_price), //订购原价
'overdueDate' => '', //过期时间
];
switch ($order_info['validity_type']) {
case '0':
$time = mktime(0, 0, 0, date('m'), date('d'), date('Y') + 10);
$data['overdueDate'] = date('Y-m-d', $time);
break;
case '1':
$time = mktime(0, 0, 0, date('m'), date('d') + $order_info['validity_day'], date('Y'));
$data['overdueDate'] = date('Y-m-d', $time);
break;
case '2' :
$data['overdueDate'] = date('Y-m-d', $order_info['validity_time']);
break;
}
$info['initData']['openDiscountTime'] = array_merge($pub, $data);
$card_type = 'discount_time';
}
if ($order_info['card_template_id'] != '') {
$info['card_template_id'] = $order_info['card_template_id'];
$info['outOrderNo'] = $order_info['order_no'];
$info['is_public_domain'] = 1;
$info['backUrl'] = '/pages_promotion/cardservice/card/zm_payment';
}
$pub['subscriptionType'] = strtoupper($card_type);
cache($out_trade_no, array_merge($pub, $data), 7200);
return $this->response($this->success($info));
}
private function http_post_data($url, $params = array())
{
if (is_array($params)) {
$params = http_build_query($params, null, '&');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置请求超时时间为10秒
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); // 设置连接超时时间为5秒
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
}