💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# php对接码支付 ``` <pre class="calibre15">``` <span class="token"><</span><span class="token">?</span>php namespace app\index\controller<span class="token1">;</span> <span class="token6">//use app\common\controller\Api;</span> use app\common\controller\Frontend<span class="token1">;</span> use think\Db<span class="token1">;</span> use think\Session<span class="token1">;</span> use think\Config<span class="token1">;</span> <span class="token2">class</span> <span class="token3">Movie</span> <span class="token2">extends</span> <span class="token3">Frontend</span> <span class="token1">{</span> <span class="token2">protected</span> $noNeedLogin <span class="token">=</span> <span class="token1">[</span><span class="token4">'*'</span><span class="token1">]</span><span class="token1">;</span> <span class="token2">protected</span> $noNeedRight <span class="token">=</span> <span class="token1">[</span><span class="token4">'*'</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">//$codepay_id = ;//这里改成码支付ID</span> <span class="token6">//$codepay_key = Config::get('site.codepay_key'); //这是您的通讯密钥</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $count <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'ziyuan'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">count</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> $rands <span class="token">=</span> <span class="token3">random_int</span><span class="token1">(</span><span class="token5">1</span><span class="token1">,</span>$count<span class="token1">)</span><span class="token1">;</span> $links <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'ziyuan'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'id'</span><span class="token1">,</span>$rands<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">field</span><span class="token1">(</span><span class="token4">'link'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">find</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">return</span> $links<span class="token1">[</span><span class="token4">'link'</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">//$this->success($links['link']);</span> <span class="token6">//$this->success('movies');</span> <span class="token1">}</span> <span class="token6">//用户注册</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">register</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span>$<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">isPost</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> $username <span class="token">=</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">post</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> $password <span class="token">=</span>$<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">post</span><span class="token1">(</span><span class="token4">'password'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$username<span class="token1">)</span><span class="token">||</span><span class="token3">empty</span><span class="token1">(</span>$password<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> $info <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'yonghu'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">,</span>$username<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">find</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$info<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">"用户已被注册"</span><span class="token1">;</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> $res <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'yonghu'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">insert</span><span class="token1">(</span><span class="token1">[</span> <span class="token4">'username'</span><span class="token">=></span>$username<span class="token1">,</span> <span class="token4">'password'</span><span class="token">=></span>$password<span class="token1">,</span> <span class="token4">'createtime'</span><span class="token">=></span><span class="token3">time</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$res<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'注册成功'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token6">//用户登录</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">login</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span> <span class="token1">(</span>$<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">isPost</span><span class="token1">(</span><span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> $username <span class="token">=</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">post</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">)</span><span class="token1">;</span> $password <span class="token">=</span>$<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">post</span><span class="token1">(</span><span class="token4">'password'</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span><span class="token3">empty</span><span class="token1">(</span>$username<span class="token1">)</span><span class="token">||</span><span class="token3">empty</span><span class="token1">(</span>$password<span class="token1">)</span><span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token5">false</span><span class="token1">;</span> <span class="token1">}</span> $usernameinfo <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'yonghu'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'username'</span><span class="token1">,</span>$username<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">find</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$usernameinfo<span class="token1">)</span> <span class="token1">{</span> $pwdinfo <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'yonghu'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'password'</span><span class="token1">,</span>$password<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">find</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$pwdinfo<span class="token1">)</span> <span class="token1">{</span> <span class="token6">//return '登陆成功'</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'yonghu'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'password'</span><span class="token1">,</span>$password<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">update</span><span class="token1">(</span><span class="token1">[</span><span class="token4">'logintime'</span><span class="token">=></span><span class="token3">time</span><span class="token1">(</span><span class="token1">)</span><span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//查看是否是会员</span> Session<span class="token1">:</span><span class="token1">:</span><span class="token2">set</span><span class="token1">(</span><span class="token4">'user'</span><span class="token1">,</span>$username<span class="token1">)</span><span class="token1">;</span> $res <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'payorder'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'pay_id'</span><span class="token1">,</span>$username<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">find</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span><span class="token1">(</span>$res<span class="token1">)</span><span class="token1">{</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span><span class="token3">redirect</span><span class="token1">(</span><span class="token4">'index/index'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">return</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span><span class="token3">createorder</span><span class="token1">(</span>$username<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">return</span> <span class="token4">'密码不正确'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">return</span> <span class="token4">'用户名不存在'</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token6">//对接码支付 创建订单</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">createorder</span><span class="token1">(</span>$username<span class="token1">)</span> <span class="token1">{</span> $data<span class="token">=</span><span class="token1">[</span> <span class="token4">'id'</span><span class="token">=></span>Config<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'site.codepay_id'</span><span class="token1">)</span><span class="token1">,</span><span class="token6">//码支付ID</span> <span class="token4">'type'</span><span class="token">=></span>Config<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'site.type'</span><span class="token1">)</span><span class="token1">,</span><span class="token6">//1:支付宝 2:QQ钱包 3:微信支付。默认值:1</span> <span class="token4">'price'</span><span class="token">=></span>Config<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'site.price'</span><span class="token1">)</span><span class="token1">,</span> <span class="token6">//支付金额</span> <span class="token4">'pay_id'</span><span class="token">=></span>$username<span class="token1">,</span><span class="token6">//用户订单号</span> <span class="token4">'param'</span><span class="token">=></span><span class="token4">''</span><span class="token1">,</span><span class="token6">//自定义参数</span> <span class="token4">'notify_url'</span><span class="token">=></span><span class="token4">''</span><span class="token1">,</span><span class="token6">//异步通知地址</span> <span class="token4">"return_url"</span><span class="token">=></span><span class="token4">"http://zhifu.xoxo88.net/index/movie/notify"</span><span class="token1">,</span><span class="token6">//付款后跳转地址</span> <span class="token1">]</span><span class="token1">;</span> <span class="token3">ksort</span><span class="token1">(</span>$data<span class="token1">)</span><span class="token1">;</span> <span class="token6">//重新排序$data数组</span> <span class="token3">reset</span><span class="token1">(</span>$data<span class="token1">)</span><span class="token1">;</span> <span class="token6">//内部指针指向数组中的第一个元素</span> $sign <span class="token">=</span> <span class="token4">''</span><span class="token1">;</span> <span class="token6">//初始化需要签名的字符为空</span> $urls <span class="token">=</span> <span class="token4">''</span><span class="token1">;</span> <span class="token6">//初始化URL参数为空</span> <span class="token3">foreach</span> <span class="token1">(</span>$data <span class="token5">AS</span> $key <span class="token">=></span> $val<span class="token1">)</span> <span class="token1">{</span> <span class="token6">//遍历需要传递的参数</span> <span class="token2">if</span> <span class="token1">(</span>$val <span class="token">==</span> <span class="token4">''</span><span class="token">||</span>$key <span class="token">==</span> <span class="token4">'sign'</span><span class="token1">)</span> <span class="token2">continue</span><span class="token1">;</span> <span class="token6">//跳过这些不参数签名</span> <span class="token2">if</span> <span class="token1">(</span>$sign <span class="token">!=</span> <span class="token4">''</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">//后面追加&拼接URL</span> $sign <span class="token1">.</span><span class="token">=</span> <span class="token4">"&"</span><span class="token1">;</span> $urls <span class="token1">.</span><span class="token">=</span> <span class="token4">"&"</span><span class="token1">;</span> <span class="token1">}</span> $sign <span class="token1">.</span><span class="token">=</span> <span class="token4">"$key=$val"</span><span class="token1">;</span> <span class="token6">//拼接为url参数形式</span> $urls <span class="token1">.</span><span class="token">=</span> <span class="token4">"$key="</span> <span class="token1">.</span> <span class="token3">urlencode</span><span class="token1">(</span>$val<span class="token1">)</span><span class="token1">;</span> <span class="token6">//拼接为url参数形式并URL编码参数值</span> <span class="token1">}</span> $query <span class="token">=</span> $urls <span class="token1">.</span> <span class="token4">'&sign='</span> <span class="token1">.</span> <span class="token3">md5</span><span class="token1">(</span>$sign <span class="token1">.</span>Config<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'site.codepay_key'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//创建订单所需的参数</span> $url <span class="token">=</span> <span class="token4">"http://api5.xiuxiu888.com/creat_order/?{$query}"</span><span class="token1">;</span> <span class="token6">//支付页面</span> <span class="token6">//$this->redirect($url);</span> <span class="token2">return</span> $url<span class="token1">;</span> <span class="token1">}</span> <span class="token6">//付款跳转界面</span> <span class="token6">// public function zhifusuccess(){</span> <span class="token6">// return "订单核实中";</span> <span class="token6">// }</span> <span class="token6">//付款完毕通知地址</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">notify</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $_POST <span class="token">=</span>$<span class="token2">this</span><span class="token">-</span><span class="token">></span>request<span class="token">-</span><span class="token">></span><span class="token3">param</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">ksort</span><span class="token1">(</span>$_POST<span class="token1">)</span><span class="token1">;</span> <span class="token6">//排序post参数</span> <span class="token3">reset</span><span class="token1">(</span>$_POST<span class="token1">)</span><span class="token1">;</span> <span class="token6">//内部指针指向数组中的第一个元素</span> $codepay_key<span class="token">=</span>Config<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'site.codepay_key'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//这是您的密钥</span> $sign <span class="token">=</span> <span class="token4">''</span><span class="token1">;</span><span class="token6">//初始化</span> <span class="token3">foreach</span> <span class="token1">(</span>$_POST <span class="token5">AS</span> $key <span class="token">=></span> $val<span class="token1">)</span> <span class="token1">{</span> <span class="token6">//遍历POST参数</span> <span class="token2">if</span> <span class="token1">(</span>$val <span class="token">==</span> <span class="token4">''</span> <span class="token">||</span> $key <span class="token">==</span> <span class="token4">'sign'</span><span class="token1">)</span> <span class="token2">continue</span><span class="token1">;</span> <span class="token6">//跳过这些不签名</span> <span class="token2">if</span> <span class="token1">(</span>$sign<span class="token1">)</span> $sign <span class="token1">.</span><span class="token">=</span> <span class="token4">'&'</span><span class="token1">;</span> <span class="token6">//第一个字符串签名不加& 其他加&连接起来参数</span> $sign <span class="token1">.</span><span class="token">=</span> <span class="token4">"$key=$val"</span><span class="token1">;</span> <span class="token6">//拼接为url参数形式</span> <span class="token1">}</span> <span class="token2">if</span> <span class="token1">(</span><span class="token">!</span>$_POST<span class="token1">[</span><span class="token4">'pay_no'</span><span class="token1">]</span> <span class="token">||</span> <span class="token3">md5</span><span class="token1">(</span>$sign <span class="token1">.</span> $codepay_key<span class="token1">)</span> <span class="token">!=</span> $_POST<span class="token1">[</span><span class="token4">'sign'</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> <span class="token6">//不合法的数据</span> <span class="token3">exit</span><span class="token1">(</span><span class="token4">'fail'</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//返回失败 继续补单</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> <span class="token6">//合法的数据</span> <span class="token6">//业务处理</span> $pay_id <span class="token">=</span> $_POST<span class="token1">[</span><span class="token4">'pay_id'</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">//需要充值的ID 或订单号 或用户名</span> $money <span class="token">=</span> <span class="token1">(</span>float<span class="token1">)</span>$_POST<span class="token1">[</span><span class="token4">'money'</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">//实际付款金额</span> $price <span class="token">=</span> <span class="token1">(</span>float<span class="token1">)</span>$_POST<span class="token1">[</span><span class="token4">'price'</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">//订单的原价</span> $pay_no <span class="token">=</span> $_POST<span class="token1">[</span><span class="token4">'pay_no'</span><span class="token1">]</span><span class="token1">;</span> <span class="token6">//流水号</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'payorder'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">insert</span><span class="token1">(</span><span class="token1">[</span> <span class="token4">'pay_id'</span><span class="token">=></span>$pay_id<span class="token1">,</span> <span class="token4">'money'</span><span class="token">=></span>$money<span class="token1">,</span> <span class="token4">'price'</span><span class="token">=></span>$price<span class="token1">,</span> <span class="token4">'pay_no'</span><span class="token">=></span>$pay_no <span class="token1">]</span><span class="token1">)</span><span class="token1">;</span> <span class="token6">//exit('success'); //返回成功 不要删除哦</span> <span class="token6">//跳转到首页</span> $<span class="token2">this</span><span class="token">-</span><span class="token">></span><span class="token3">redirect</span><span class="token1">(</span><span class="token4">'index/index'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token6">//检测是否付款成功</span> <span class="token6">//检测是否付款成功</span> <span class="token2">public</span> <span class="token2">function</span> <span class="token3">paystatus</span><span class="token1">(</span><span class="token1">)</span> <span class="token1">{</span> $username <span class="token">=</span> Session<span class="token1">:</span><span class="token1">:</span><span class="token2">get</span><span class="token1">(</span><span class="token4">'user'</span><span class="token1">)</span><span class="token1">;</span> $payinfo <span class="token">=</span> Db<span class="token1">:</span><span class="token1">:</span><span class="token3">name</span><span class="token1">(</span><span class="token4">'payorder'</span><span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">where</span><span class="token1">(</span><span class="token4">'pay_id'</span><span class="token1">,</span>$username<span class="token1">)</span><span class="token">-</span><span class="token">></span><span class="token3">find</span><span class="token1">(</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">if</span> <span class="token1">(</span>$payinfo<span class="token1">)</span> <span class="token1">{</span> <span class="token2">return</span> <span class="token4">'1'</span><span class="token1">;</span><span class="token6">//已付款</span> <span class="token1">}</span><span class="token2">else</span><span class="token1">{</span> <span class="token2">return</span> <span class="token4">'0'</span><span class="token1">;</span><span class="token6">//未付款</span> <span class="token1">}</span> <span class="token1">}</span> <span class="token1">}</span> ``` ```