Browse Source

测试判断微信支付方式

master
lixiaoyu 3 years ago
parent
commit
e95ebeb020
  1. 64
      mobile/plugins/payment/wxpay.php

64
mobile/plugins/payment/wxpay.php

@ -1,6 +1,7 @@
<?php
defined('BASE_PATH') OR exit('No direct script access allowed');
defined('BASE_PATH') or exit('No direct script access allowed');
session_start();
class wxpay
{
private $parameters; // cft 参数
@ -9,7 +10,8 @@ class wxpay
/**
* 对openid加密的算法
*/
public function encrypt($plaintext) {
public function encrypt($plaintext)
{
$length = mb_strlen($plaintext);
$ciphertext = '';
for ($i = 0; $i < $length; $i += 2) {
@ -17,11 +19,12 @@ class wxpay
}
return $ciphertext;
}
/**
* 对openid解密的算法
*/
public function decrypt($ciphertext) {
public function decrypt($ciphertext)
{
$length = mb_strlen($ciphertext);
$plaintext = '';
for ($i = 0; $i < $length; $i += 2) {
@ -32,9 +35,10 @@ class wxpay
/**
* 递归include文件
* 递归include文件
*/
public function includeDirectory($directory) {
public function includeDirectory($directory)
{
if ($handle = opendir($directory)) {
while (($file = readdir($handle)) !== false) {
if ($file != '.' && $file != '..') {
@ -50,18 +54,19 @@ class wxpay
closedir($handle);
}
}
/**
* 生成支付代码
* @param array $order 订单信息
* @param array $payment 支付方式信息
* @param array $order 订单信息
* @param array $payment 支付方式信息
*/
public function get_code($order, $payment)
{
include_once(BASE_PATH.'helpers/payment_helper.php');
include_once(BASE_PATH . 'helpers/payment_helper.php');
// 配置参数
$this->payment = $payment;
// 网页授权获取用户openid
if (! isset($_SESSION['openid']) || empty($_SESSION['openid'])) {
if (!isset($_SESSION['openid']) || empty($_SESSION['openid'])) {
// $button = '<a class="box-flex btn-submit" type="button" onclick="">微信支付</a>';
// return $button;
@ -126,7 +131,7 @@ class wxpay
// 获取微信支付需要的其他参数
$payinfo = $response_data['payinfo'];
$payinfo = json_decode($payinfo,true);
$payinfo = json_decode($payinfo, true);
// var_dump($payinfo);
$appId = $payinfo['appId'];
$timeStamp = $payinfo['timeStamp'];
@ -134,7 +139,7 @@ class wxpay
$signType = $payinfo['SignType'];
$paySign = $payinfo['paySign'];
// ....实现自己的$this->getPrepayId()
// 获取prepay_id 和其他必要参数
$package = $payinfo['package'];
@ -165,15 +170,15 @@ class wxpay
$this->parameters["appid"] = $payinfo['appId']; // 公众账号ID
// 调用秘钥里的配置文件
if (!isset($_SESSION['org_num'])){
if (!isset($_SESSION['org_num'])) {
echo "org_num is not set!\r\n";
}
$org_num = $_SESSION['org_num'];
// echo "\r\n" . $org_num . "\r\n";
unset($_SESSION['org_num']);
// echo "\r\n" . $org_num . "\r\n";
$this->parameters["mch_id"] = $org_num; // 商户号 sc_test
// $this->parameters["spbill_create_ip"] = $_SERVER['REMOTE_ADDR']; // 终端ip
@ -204,7 +209,7 @@ class wxpay
// var_dump($signType);
// echo "\r\n";
// var_dump($package);
$jsApiParameters = json_encode($jsApiParameters);
// 接收回调front_url参数
@ -212,7 +217,7 @@ class wxpay
// var_dump($front_url);
// location.href="' . return_url(basename(__FILE__, '.php')) . '"
// wxjsbridge
$js = '
<script language="javascript">
@ -234,6 +239,17 @@ class wxpay
{
console.log(123456);
console.log(typeof WeixinJSBridge);
wx.miniProgram.getEnv(function(res) {
if(res.miniprogram){
// true代表在小程序里
console.log(4567893);
}else{
//false代表在公众号里
console.log(123486789);
}
})
if (typeof WeixinJSBridge == "undefined")
{
if( document.addEventListener )
@ -257,7 +273,7 @@ class wxpay
// $button = '<a class="box-flex btn-submit" type="button" onclick="">微信支付(测试)</a>';
return $button;
}
/**
@ -277,8 +293,8 @@ class wxpay
*/
public function notify($data)
{
include_once(BASE_PATH.'helpers/payment_helper.php');
if (! empty($data['postStr'])) {
include_once(BASE_PATH . 'helpers/payment_helper.php');
if (!empty($data['postStr'])) {
$payment = get_payment($data['code']);
$postdata = json_decode(json_encode(simplexml_load_string($data['postStr'], 'SimpleXMLElement', LIBXML_NOCDATA)), true);
/* 检查插件文件是否存在,如果存在则验证支付是否成功,否则则返回失败信息 */
@ -313,13 +329,13 @@ class wxpay
// 获取log_id
$out_trade_no = explode('O', $postdata['out_trade_no']);
$order_sn = $out_trade_no[1]; // 订单号log_id
// 改变订单状态
// 改变订单状态
order_paid($order_sn, 2);
// 修改订单信息(openid,tranid)
model()->table('pay_log')
->data(array('openid'=>$postdata['openid'], 'transid'=>$postdata['transaction_id']))
->where(array('log_id'=>$order_sn))
->data(array('openid' => $postdata['openid'], 'transid' => $postdata['transaction_id']))
->where(array('log_id' => $order_sn))
->update();
/*if(method_exists('WechatController', 'do_oauth')){
// 如果需要,微信通知 wanglu
@ -376,7 +392,7 @@ class wxpay
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i ++) {
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;

Loading…
Cancel
Save