You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/webht/third_party/pay/controllers/AlipayTradeService.php

235 lines
7.2 KiB
PHTML

8 years ago
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class AlipayTradeService extends CI_Controller
{
//支付宝网关地址
public $gateway_url = "https://openapi.alipay.com/gateway.do";
//支付宝公钥
public $alipay_public_key;
//商户私钥
public $private_key;
//应用id
public $appid;
//UID
public $seller_id;
//编码格式
public $charset = "UTF-8";
public $token = NULL;
//返回数据格式
public $format = "json";
//签名方式
public $signtype = "RSA2";
// cht
public function __construct(){
parent::__construct();
$this->load->library('alipay/AopSdk');
// real URL
// $this->config->load('alipay');
// test URL
$this->config->load('dev_alipay');
$this->load->model('AlipayTradePagePayContentBuilder');
$this->load->model('AlipayTradeQueryContentBuilder');
$this->gateway_url = $this->config->item('gatewayUrl');
$this->appid = $this->config->item('app_id');
$this->seller_id = $this->config->item('seller_id');
$this->private_key = $this->config->item('merchant_private_key');
$this->alipay_public_key = $this->config->item('alipay_public_key');
$this->charset = $this->config->item('charset');
$this->signtype = $this->config->item('sign_type');
if(empty($this->appid)||trim($this->appid)==""){
throw new Exception("appid should not be NULL!");
}
if(empty($this->private_key)||trim($this->private_key)==""){
throw new Exception("private_key should not be NULL!");
}
if(empty($this->alipay_public_key)||trim($this->alipay_public_key)==""){
throw new Exception("alipay_public_key should not be NULL!");
}
if(empty($this->charset)||trim($this->charset)==""){
throw new Exception("charset should not be NULL!");
}
if(empty($this->gateway_url)||trim($this->gateway_url)==""){
throw new Exception("gateway_url should not be NULL!");
}
}
// cht
public function pay_fun()
{
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = trim($this->input->get_post('out_trade_no'));
//订单名称,必填
$subject = trim($this->input->get_post('subject'));
//付款金额,必填
$total_amount = trim($this->input->get_post('total_amount'));
//商品描述,可空
$body = trim($this->input->get_post('body'));
$this->AlipayTradePagePayContentBuilder->setBody($body);
$this->AlipayTradePagePayContentBuilder->setSubject($subject);
$this->AlipayTradePagePayContentBuilder->setTotalAmount($total_amount);
$this->AlipayTradePagePayContentBuilder->setOutTradeNo($out_trade_no);
$url = $this->pagePay(
$this->AlipayTradePagePayContentBuilder,
$this->config->item('return_url'),
$this->config->item('notify_url'));
return;
}
/**
* alipay.trade.page.pay
* @param $builder 业务参数使用buildmodel中的对象生成。
* @param $return_url 同步跳转地址,公网可以访问
* @param $notify_url 异步通知地址,公网可以访问
* @return $response 支付宝返回的信息
*/
function pagePay($builder,$return_url,$notify_url) {
$biz_content=$builder->getBizContent();
//打印业务参数 cht
log_message('error',"\r\n"." Begin \r\n".$biz_content);
$request = new AlipayTradePagePayRequest();
$request->setNotifyUrl($notify_url);
$request->setReturnUrl($return_url);
$request->setBizContent ( $biz_content );
// 首先调用支付api
$response = $this->aopclientRequestExecute ($request,true);
return $response;
}
/**
* sdkClient
* @param $request 接口请求参数对象。
* @param $ispage 是否是页面接口,电脑网站支付是页面表单接口。
* @return $response 支付宝返回的信息
*/
function aopclientRequestExecute($request,$ispage=false) {
$aop = new AopClient ();
$aop->gatewayUrl = $this->gateway_url;
$aop->appId = $this->appid;
$aop->rsaPrivateKey = $this->private_key;
$aop->alipayrsaPublicKey = $this->alipay_public_key;
$aop->apiVersion ="1.0";
$aop->postCharset = $this->charset;
$aop->format= $this->format;
$aop->signType=$this->signtype;
// 开启页面信息输出
$aop->debugInfo=true;
$result = null;
if($ispage)
{
$result = $aop->pageExecute($request,"post");
echo $result;
}
else
{
$result = $aop->Execute($request);
}
//打开后将报文写入log文件 cht
log_message('error',"\r\n\r\nResponse: \r\n".var_export($result,true));
return $result;
}
/**
* alipay.trade.query (统一收单线下交易查询)
* @param $builder 业务参数使用buildmodel中的对象生成。
* @return $response 支付宝返回的信息
*/
function Query($builder){
$biz_content=$builder->getBizContent();
//打印业务参数
$request = new AlipayTradeQueryRequest();
$request->setBizContent ( $biz_content );
$response = $this->aopclientRequestExecute ($request);
$response = $response->alipay_trade_query_response;
return $response;
}
/**
* 验签方法
* @param $arr 验签支付宝返回的信息,使用支付宝公钥。
* @return boolean
*/
function check($arr){
$aop = new AopClient();
$aop->alipayrsaPublicKey = $this->alipay_public_key;
$result = $aop->rsaCheckV1($arr, $this->alipay_public_key, $this->signtype);
if ($result === false) {
return $result;
}
// cht
// 1.app_id
// 2.seller_id
// 3.查询数据库,验证订单信息,订单号
// 4.订单金额
$verify_ret = true;
if ($arr['app_id'] !== $this->appid) {
$verify_ret = false;
}
if ($arr['seller_id'] !== $this->seller_id) {
$verify_ret = false;
}
log_message('error',"\r\n\r\n Check result: ".$result);
return $verify_ret;
}
// cht
function return_url()
{
$arr = $this->input->get();
$result = $this->check($arr);
if ($result) {
$this->load->view("iPayLinks_rep",array("code"=>0,"msg"=>""));
// echo "付款成功!支付宝交易号:".$arr["trade_no"];
} else {
echo "付款失败!";
}
return;
}
// cht
function notify_url()
{
$arr = $this->input->post();
$result = $this->check($arr);
log_message('error',"\r\n\r\n Notify:\r\n".var_export($arr,true));
if ($result) {
/*!
* Do NOT touch
* 不要改这里,必须返回这5个字符
*/
echo "success";
} else {
echo "fail";
}
return;
}
}