| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace app\controller;
- use app\BaseController;
- use think\facade\Db;
- use phpu\facade\ThinkCaptcha;
- use Ramsey\Uuid\Uuid;
- use think\captcha\facade\Captcha;
- /**
- * 支付类(彩虹易支付)
- */
- class Payment extends BaseController
- {
- protected $noNeedLogin = ['payOrder','payCallback'];
- public $baseUrl = 'https://pay.v8jisu.cn';
- public $merchantId = '28399';
- public $key = '0QknLivpRQB50Bq76r06PiR464SrTrvQ';
- // 生成 MD5 签名
- private function makeMd5Sign($data)
- {
- $md5_key = $this->key; // 商户密钥
- ksort($data); // 按照 ASCII 码排序
- $str = '';
- foreach ($data as $k => $v) {
- if ($v !== "" && $k != 'sign' && $k != 'sign_type') {
- // 排除空值、sign 和 sign_type
- $str .= "{$k}={$v}&";
- }
- }
- $str = rtrim($str, "&"); // 去掉最后一个 &
- $str .= $md5_key; // 按文档要求,直接拼接 KEY
-
- return md5($str); // 小写
- }
- //请求
- private function HttpRequest($url, $postData)
- {
- // 将数组编码为 x-www-form-urlencoded 格式的查询字符串
- $postData1 = http_build_query($postData);
- // 初始化 cURL 会话
- $ch = curl_init($url);
- // 设置 cURL 选项
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 将响应作为字符串返回
- curl_setopt($ch, CURLOPT_POST, true); // 使用 POST 方法
- curl_setopt($ch, CURLOPT_POSTFIELDS, $postData1); // 设置 POST 数据
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/x-www-form-urlencoded' // 设置 Content-Type 头
- ));
- // 执行请求并获取响应
- $response = curl_exec($ch);
- // 关闭 cURL 会话
- curl_close($ch);
- // 检查请求是否成功
- if ($response === false) {
- return "cURL Error: " . curl_error($ch);
- } else {
- // 处理响应
- return $response;
- }
- }
- /**
- * 发起支付
- */
- public function payOrder()
- {
- $goodsName = $this->request->post('goodsName'); // 商品名称
- $goodsPrice = $this->request->post('goodsPrice'); // 商品金额
- $type = $this->request->post('type'); // 支付方式
- $user_id = $this->getUser()->user_id; // 用户ID
- if(!$user_id) {
- $this->fail(500, '用户未登录');
- }
- $data['pid'] = $this->merchantId;
- // $data['method'] = 'web'; // 接口类型
- // $datap['device'] = 'pc'; // 设备类型
- $data['type'] = $type; // 支付方式
- $data['out_trade_no'] = date("YmdHis") . rand(100, 999); // 订单号
- $data['notify_url'] = 'https://api.danjiwanjia.com/payment/payCallback'; // 服务器异步通知地址
- $data['return_url'] = 'https://www.danjiwanjia.com/#/userCenter'; // 页面跳转通知地址
- $data['name'] = $goodsName; // 商品名称
- $data['money'] = $goodsPrice; // 商品金额
- $data['clientip'] = $this->request->ip(); // 用户ip地址
- $data['timestamp'] = time(); // 当前时间戳
- $data['sign_type'] = 'MD5'; // 当前时间戳
- $data['sign'] = $this->makeMd5Sign($data); // 签名生成
- // 插入数据库
- Db::name('tb_user_order')->insert([
- 'user_id' => $user_id,
- 'order_no' => $data['out_trade_no'],
- 'balance' => $goodsPrice,
- 'state' => 0,
- 'create_at' => time(),
- ]);
- $url = $this->baseUrl . '/mapi.php';
- $result = $this->HttpRequest($url, $data);
- // header('Content-Type: application/json');
- // echo $result;
- // exit();
- if($result['code'] == 1) {
- $this->success('success', $result['payurl']);
- } else {
- $this->fail(500, $result['msg']);
- }
- }
- /**
- * 支付回调
- */
- public function payCallback()
- {
- $data = $this->request->param();
- if(!$data) {
- exit('success');
- }
- $order_sn = $data['out_trade_no'];
- $amount = $data['money'];
- $order = Db::name('tb_user_order')->where('order_no', $order_sn)->find();
- if (!$order) {
- exit('订单不存在');
- }
- if ($order['state'] == 1) {
- // 订单已支付 不走后面的逻辑但是要返回success
- exit('success');
- }
- // 订单校验通过 更新订单状态
- Db::name('tb_user_order')
- ->where('order_no', $order_sn)
- ->update([
- 'state' => 1,
- 'pay_at' => time(),
- ]);
- exit('success');
- }
- }
|