feat: init project codebase
This commit is contained in:
@@ -0,0 +1,213 @@
|
||||
<?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();
|
||||
$this->load->library('session');
|
||||
}
|
||||
|
||||
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' => '样本编号',
|
||||
'rules' => 'trim|required|min_length[4]|max_length[20]',
|
||||
'errors' => array(
|
||||
'required' => '请提供以下信息:%s.',
|
||||
),
|
||||
)
|
||||
);
|
||||
$data = $this->json_validation($config);
|
||||
//样本信息
|
||||
$this->load->model('sample_model');
|
||||
$sample = $this->sample_model->get($data['id'],'device_id');
|
||||
if(!$sample){
|
||||
$this->error('样本不存在');
|
||||
}
|
||||
//序列号信息
|
||||
$this->load->model('series_number_model');
|
||||
$series = $this->series_number_model->get($data['id'],'device_id');
|
||||
if(empty($series)){
|
||||
$this->error('序列号信息不存在');
|
||||
}
|
||||
//如果样本中没有记录套餐,则根据序列号绑定的套餐做升级
|
||||
$package_ori = !empty($sample['package_id'])?$sample['package_id']:$series['package_ori'];
|
||||
//套餐信息
|
||||
$this->load->model('package_model');
|
||||
$from_package = $this->package_model->get($package_ori);
|
||||
if(empty($from_package['package_up'])){
|
||||
$this->error('您的套餐已经升级到最高套餐');
|
||||
}
|
||||
$to_package = $this->package_model->get($from_package['package_up']);
|
||||
if(empty($to_package)){
|
||||
$this->error('套餐信息不存在');
|
||||
}
|
||||
$total = round($to_package['price'] - $from_package['price'],2);
|
||||
if($total <= 0){
|
||||
$this->error('升级后套餐价格比当前套餐价格低');
|
||||
}
|
||||
|
||||
//用户信息处理,获取用户的openid
|
||||
$this->load->model('customer_model');
|
||||
if(!empty($this->session->{Customer_model::SESSION_KEY})){
|
||||
//如已登录,获取用户的open
|
||||
$customer = $this->customer_model->get(
|
||||
$this->session->{Customer_model::SESSION_KEY},
|
||||
'id'
|
||||
);
|
||||
//用户可能会被删除
|
||||
if($customer){
|
||||
$open_id = $customer['openid'];
|
||||
}
|
||||
}
|
||||
if(empty($open_id) && !empty($this->session->{Customer_model::SESSION_KEY_WX_OPEN_ID})){
|
||||
$open_id = $this->session->{Customer_model::SESSION_KEY_WX_OPEN_ID};
|
||||
}
|
||||
if(empty($open_id)){
|
||||
$this->success(['need_open_id'=>true]);
|
||||
}
|
||||
|
||||
|
||||
//生成订单,订单不能重复,每次都重新创建
|
||||
$this->load->model('sample_upgrade_order_model');
|
||||
/*
|
||||
//同样订单升级方案只能创建一个订单
|
||||
$order_id = null;
|
||||
$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('您的订单已支付');
|
||||
}
|
||||
//如果金额不一致,需要做金额修改
|
||||
if($total!=$value['grand_total']){
|
||||
$this->sample_upgrade_order_model->update([
|
||||
'grand_total'=>$total,
|
||||
],[
|
||||
'id'=>$value['id']
|
||||
]);
|
||||
}
|
||||
$order_id = $value['id'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(is_null($order_id)){
|
||||
}*/
|
||||
$order_id = $this->sample_upgrade_order_model->add([
|
||||
'sample_id'=>$sample['id'],
|
||||
'package_before'=>$from_package['id'],
|
||||
'package_after'=>$to_package['id'],
|
||||
'grand_total'=>$total,
|
||||
]);
|
||||
if(!$order_id){
|
||||
$this->error('订单创建失败');
|
||||
}
|
||||
|
||||
$attributes = [
|
||||
'body' => '有哈检测套餐升级',
|
||||
'out_trade_no' => $order_id,
|
||||
//'total_fee' => (ENVIRONMENT !== 'production'?101:$total*100),//沙箱环境固定为1.01,正式环境固定为分
|
||||
'total_fee' => round($total*100),//单位:分
|
||||
//'spbill_create_ip' => '', // 可选,如不传该参数,SDK 将会自动获取相应 IP 地址
|
||||
'notify_url' => base_url().'api/user/'.strtolower(__CLASS__).'/callback', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
|
||||
'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
|
||||
'openid' => $open_id,
|
||||
];
|
||||
//echo base_url().'api/user/'.strtolower(__CLASS__).'/callback';die;
|
||||
$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_post()
|
||||
{
|
||||
log_message('error',json_encode($_GET));
|
||||
log_message('error',json_encode($_POST));
|
||||
log_message('error',file_get_contents('php://input'));
|
||||
$app = $this->getApp();
|
||||
$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['pay_status'] = 2;
|
||||
$this->sample_upgrade_order_model->update($data,[
|
||||
'id'=>$notify->out_trade_no
|
||||
]);
|
||||
return true;
|
||||
}
|
||||
$this->load->model('sample_model');
|
||||
$this->sample_model->update([
|
||||
'package_id'=>$order['package_after']
|
||||
],[
|
||||
'id'=>$order['sample_id']
|
||||
]);
|
||||
|
||||
// 不是已经支付状态则修改为已经支付状态
|
||||
$data['paid_time'] = time(); // 更新支付时间为当前时间
|
||||
$data['pay_status'] = 1;
|
||||
$this->sample_upgrade_order_model->update($data,[
|
||||
'id'=>$notify->out_trade_no
|
||||
]);
|
||||
//样本更改套餐
|
||||
|
||||
|
||||
return true; // 返回处理完成
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user