Files
wsa_server/controllers/api/user/Upgrade.bak.php
T
2026-09-14 16:32:07 +08:00

160 lines
5.6 KiB
PHP

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
use EasyWeChat\Foundation\Application as OfficialAccount;
use EasyWeChat\Payment\Order as WxPayOrder;
class Upgrade extends ApiController
{
function __construct()
{
header('Access-Control-Allow-Credentials: true');
parent::__construct();
}
protected function getApp(){
$conf = $this->config->item('weixin_uah');
$config = [
'app_id' => $conf['AppId'],
'secret' => $conf['AppSecert'],
'token' => $conf['Token'],
'aes_key' => $conf['EncodingAESKey'],
'response_type' => 'array',
'payment'=>[
// 必要配置
'app_id' => $conf['AppId'],
'mch_id' => $conf['mch_id'],
'key' => $conf['pay_api_key'], // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置
'cert_path' => $conf['pay_cert_path'], // 绝对路径
'key_path' => $conf['pay_key_path'], // 绝对路径
'notify_url' => '默认的订单回调地址', // 下单时单独设置来覆盖它
//'sandbox_mode' => (ENVIRONMENT !== 'production'), // 设置为 false 或注释则关闭沙箱模式
]
];
return new OfficialAccount($config);
}
//创建升级订单
public function order_post()
{
$config = array(
array(
'field' => 'id',
'label' => 'ID',
'rules' => 'trim|required|min_length[4]|max_length[20]'
),
array(
'field' => 'package_id',
'label' => 'ID',
'rules' => 'trim|required|min_length[1]|max_length[20]'
)
);
$data = $this->json_validation($config);
$this->load->model('sample_model');
$sample = $this->sample_model->get($data['id'],'device_id');
if(empty($sample)){
$this->error('样品不存在');
}
if($sample['package_id'] == $data['package_id']){
$this->error('升级信息不正确');
}
$this->load->model('package_model');
$to_package = $this->package_model->get($data['package_id'],'id');
$from_package = $this->package_model->get($sample['package_id'],'id');
if(empty($to_package) || empty($from_package)){
$this->error('套餐信息不存在');
}
$total = $to_package['price'] - $from_package['price'];
//生成订单
//同样订单升级方案只能创建一个订单
$order_id = null;
$this->load->model('sample_upgrade_order_model');
$order_list = $this->sample_upgrade_order_model->get($sample['id'],'base.sample_id',true);
foreach ($order_list as $value) {
if($value['package_before'] == $from_package['id'] &&
$value['package_after'] == $to_package['id']
){
if($value['paid_time'] > 0){
$this->error('您的订单已支付');
}
$order_id = $value['id'];
}
}
if(is_null($order_id)){
$order_id = $this->sample_upgrade_order_model->add([
'sample_id'=>$sample['id'],
'grand_total'=>$total,
'package_before'=>$from_package['id'],
'package_after'=>$to_package['id'],
'grand_total'=>$total,
]);
}
if(!$order_id){
$this->error('订单创建失败');
}
//获取用户的openid
$this->load->model('customer_model');
$customer = $this->customer_model->get($sample['user_id'],'id');
$attributes = [
'body' => '有哈检测套餐升级',
'out_trade_no' => 'testadsfdsffadsf',
//'total_fee' => (ENVIRONMENT !== 'production'?101:$total*100),//沙箱环境固定为1.01,正式环境固定为分
'total_fee' => $total*100,//单位:分
//'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
'notify_url' => base_url().'api/user/'.strtolower(__CLASS__).'/callback', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'openid' => $customer['openid'],
];
$order = new WxPayOrder($attributes);
//print_r($attributes);die;
$app = $this->getApp();
$result = $app->payment->prepare($order);
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS'){
$prepayId = $result->prepay_id;
$json = $app->payment->configForPayment($prepayId,false); // 返回 json 字符串,如果想返回数组,传第二个参数
$this->success($json);
}
//print_r($result);die;
log_message('error', json_encode($data).''.json_encode($result));
$this->error('下单失败,请联系客服');
}
public function callback_get()
{
log_message('error',json_encode($_GET));
log_message('error',json_encode($_POST));
log_message('error',json_encode(file_get_contents('php://input')));
$app = $this->getApp();
$this->$app->payment->handleNotify(function($notify, $successful){
// 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
$this->load->model('sample_upgrade_order_model');
$order = $this->sample_upgrade_order_model->get($notify->out_trade_no);
if (!$order) { // 如果订单不存在
return 'Order not exist.'; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
}
// 如果订单存在
// 检查订单是否已经更新过支付状态
if ($order['paid_time']) { // 假设订单字段“支付时间”不为空代表已经支付
return true; // 已经支付成功了就不再更新了
}
$data = [];
// 用户是否支付成功
if ($successful) {
// 不是已经支付状态则修改为已经支付状态
$data['paid_time'] = time(); // 更新支付时间为当前时间
$data['pay_status'] = 1;
} else { // 用户支付失败
$data['pay_status'] = 2;
}
$this->sample_upgrade_order_model->update($data,[
'id'=>$notify->out_trade_no
]);
return true; // 返回处理完成
});
}
}