170 lines
6.5 KiB
PHP
170 lines
6.5 KiB
PHP
<?php
|
||
|
||
namespace Shop\Model;
|
||
|
||
use Think\Model\RelationModel;
|
||
use Org\pay\lib;
|
||
|
||
class ShopOrderModel extends RelationModel {
|
||
|
||
public function __construct() {
|
||
parent::__construct();
|
||
ini_set('date.timezone', 'Asia/Shanghai');
|
||
//error_reporting(E_ERROR);
|
||
require_once "./ThinkPHP/Library/Vendor/Wxpay/lib/WxPay.Api.php";
|
||
require_once "./ThinkPHP/Library/Vendor/Wxpay/example/WxPay.NativePay.php";
|
||
require_once './ThinkPHP/Library/Vendor/Wxpay/example/WxPay.JsApiPay.php';
|
||
require_once './ThinkPHP/Library/Vendor/Wxpay/example/log.php';
|
||
require_once './ThinkPHP/Library/Vendor/Wxpay/example/phpqrcode/phpqrcode.php';
|
||
require_once './ThinkPHP/Library/Vendor/Wxpay/example/WxpayService.php';
|
||
}
|
||
|
||
//关联模型
|
||
Protected $_link = array(
|
||
"shop_order_detail" => array(
|
||
'mapping_type' => self::HAS_MANY,
|
||
"parent_key" => 'order_id', //parent_key一定是主键,否则会列不出子表的数据
|
||
'foreign_key' => 'order_id',
|
||
),
|
||
);
|
||
//验证
|
||
protected $_validate = array(
|
||
//array("字段","验证规则","错误提示",["验证条件","附加条件","验证时间"]),
|
||
array("provinceName", "require", "送货地址不能为空"),
|
||
array("cityName", "require", "送货地址不能为空"),
|
||
array("countryName", "require", "送货地址不能为空"),
|
||
array("detailInfo", "require", "送货地址不能为空"),
|
||
array("userName", "require", "送货地址不能为空"),
|
||
array("telNumber", "require", "送货地址不能为空"),
|
||
);
|
||
|
||
public function Wxpay_order_h5($order) {
|
||
|
||
$input = new \WxpayService();
|
||
|
||
$input->SetAttach($order["order_id"]);
|
||
$input->setTotalFee($order["money"]*100);
|
||
$input->setOutTradeNo($order["order_num"]."1");
|
||
$input->setOrderName("维健乐");
|
||
$input->setWapUrl("http://" . $_SERVER['HTTP_HOST']);
|
||
$input->setWapName("维健乐");
|
||
$input->setNotifyUrl("http://" . $_SERVER['HTTP_HOST'] . "/payment.php");
|
||
|
||
$where["order_id"] = $order["order_id"];
|
||
$where["order_num"] = $order["order_num"];
|
||
$rs = M("transaction")->where($where)->find();
|
||
if (!$rs) {
|
||
$data = array();
|
||
$data["order_type"] = 1;
|
||
$data["pay_type"] = 1;
|
||
$data["order_num"] = $order["order_num"];
|
||
$data["order_id"] = $order["order_id"];
|
||
$data["money"] = $order["money"];
|
||
$data["addtime"] = $order["addtime"];
|
||
M("transaction")->add($data);
|
||
}
|
||
|
||
return $input->createJsBizPackage();
|
||
}
|
||
|
||
public function Wxpay_order_jssdk($order) {
|
||
//①、获取用户openid
|
||
$tools = new \JsApiPay();
|
||
$openId = session("member_r.openid");
|
||
// ②、统一下单
|
||
$input = new \WxPayUnifiedOrder();
|
||
$input->SetBody("维健乐");
|
||
$input->SetAttach($order["order_id"]);
|
||
$input->SetOut_trade_no($order["order_num"]."2");
|
||
$input->SetTotal_fee($order["money"]*100);
|
||
$input->SetTime_start(date("YmdHis"));
|
||
$input->SetTime_expire(date("YmdHis", time() + 600));
|
||
$input->SetGoods_tag("维健乐");
|
||
$input->SetNotify_url("http://".$_SERVER['HTTP_HOST']."/payment.php");
|
||
$input->SetTrade_type("JSAPI");
|
||
$input->SetOpenid($openId);
|
||
$orders = \WxPayApi::unifiedOrder($input);
|
||
$jsApiParameters = $tools->GetJsApiParameters($orders);
|
||
$where["order_id"] = $order["order_id"];
|
||
$where["order_num"] = $order["order_num"];
|
||
$rs = M("transaction")->where($where)->find();
|
||
if(!$rs){
|
||
$data = array();
|
||
$data["order_type"] = 1;
|
||
$data["pay_type"] = 1;
|
||
$data["order_num"] = $order["order_num"];
|
||
$data["order_id"] = $order["order_id"];
|
||
$data["money"] = $order["money"];
|
||
$data["addtime"] = $order["addtime"];
|
||
M("transaction")->add($data);
|
||
}
|
||
return $jsApiParameters;
|
||
}
|
||
|
||
public function Wxpay_order($order) {
|
||
//模式二
|
||
/**
|
||
* 流程:
|
||
* 1、调用统一下单,取得code_url,生成二维码
|
||
* 2、用户扫描二维码,进行支付
|
||
* 3、支付完成之后,微信服务器会通知支付成功 $order["money"]*100
|
||
* 4、在支付成功通知中需要查单确认是否真正支付成功(见:notify.php)
|
||
*/
|
||
$notify = new \NativePay();
|
||
$input = new \WxPayUnifiedOrder();
|
||
$input->SetBody("维健乐");
|
||
$input->SetAttach($order["order_id"]);
|
||
$input->SetOut_trade_no($order["order_num"]."3");
|
||
$input->SetTotal_fee($order["money"]*100);
|
||
$input->SetTime_start(date("YmdHis"));
|
||
$input->SetTime_expire(date("YmdHis", time() + 600));
|
||
$input->SetGoods_tag("维健乐");
|
||
$input->SetNotify_url("http://" . $_SERVER['HTTP_HOST'] . "/payment.php");
|
||
$input->SetTrade_type("NATIVE");
|
||
$input->SetProduct_id();
|
||
$result = $notify->GetPayUrl($input);
|
||
$url = $result["code_url"];
|
||
$where["order_id"] = $order["order_id"];
|
||
$where["order_num"] = $order["order_num"];
|
||
$rs = M("transaction")->where($where)->find();
|
||
if (!$rs) {
|
||
$data = array();
|
||
$data["order_type"] = 1;
|
||
$data["pay_type"] = 1;
|
||
$data["order_num"] = $order["order_num"];
|
||
$data["order_id"] = $order["order_id"];
|
||
$data["money"] = $order["money"];
|
||
$data["addtime"] = $order["addtime"];
|
||
M("transaction")->add($data);
|
||
}
|
||
return $url;
|
||
}
|
||
|
||
public function qrcode($url) {
|
||
\QRcode::png($url);
|
||
}
|
||
|
||
|
||
function getIPaddress(){
|
||
$IPaddress='';
|
||
if (isset($_SERVER)){
|
||
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
|
||
$IPaddress = $_SERVER["HTTP_X_FORWARDED_FOR"];
|
||
} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
|
||
$IPaddress = $_SERVER["HTTP_CLIENT_IP"];
|
||
} else {
|
||
$IPaddress = $_SERVER["REMOTE_ADDR"];
|
||
}
|
||
} else {
|
||
if (getenv("HTTP_X_FORWARDED_FOR")){
|
||
$IPaddress = getenv("HTTP_X_FORWARDED_FOR");
|
||
} else if (getenv("HTTP_CLIENT_IP")) {
|
||
$IPaddress = getenv("HTTP_CLIENT_IP");
|
||
} else {
|
||
$IPaddress = getenv("REMOTE_ADDR");
|
||
}
|
||
}
|
||
return $IPaddress;
|
||
}
|
||
}
|