欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

支付宝扫码支付模型 附OpenSSL工具windows编译版本

程序员文章站 2022-05-28 15:10:28
...
支付宝扫码支付模型 附OpenSSL工具windows编译版本 ThinkPHP3.2.3
代码只是实现了功能,不是太严谨,比如接收异步通知,没有验证通知是否来自支付宝
支付宝扫码支付 namespace Common\Model;
use Think\Model;

/*支付宝扫码支付模型
*author zheng.wang(4995952@qq.com)
*date 2015-12-15
*/

// +----------------------------------------
// | 打发士大夫的
// +----------------------------------------
// | 大法师
// +----------------------------------------
class PayModel extends Model {
private $ali_app_id = '20151210000000000'; //APPID
private $ali_notify_url = 'http://www.19981.com/Home/Notify/aliNotify'; //支付宝结果异步通知
private $wx_notify_url = 'http://www.19981.com/Home/Notify/aliNotify'; //支付宝结果异步通知


/*支付宝扫码支付
*param $data=array('out_trade_no'=>'订单号','total_amount'=>'商品金额','subject'=>'商品名称','time_expire'=>'支付过期时间');
*author zheng.wang(4995952@qq.com)
*date 2015-12-15
*/
public function aliPay($data){
$url = "https://openapi.alipay.com/gateway.do"; //支付宝网关
$params['app_id'] = $this->ali_app_id ;
//biz_content 业务参数需要使用json格式
$biz_content['out_trade_no'] = $data['out_trade_no']; //网站订单号
$biz_content['total_amount'] = $data['total_amount']; //订单金额
$biz_content['subject'] = $data['subject']; //商品名称
$biz_content['time_expire'] = date('Y-m-d H:i:s',$data['time_expire']); //订单过期时间
$params['biz_content'] = json_encode($biz_content);
// $time_expire = date('Y-m-d H:i:s',$data['time_expire']); //订单过期时间
// $params['biz_content']="{\"out_trade_no\":\"" . $data['out_trade_no'] . "\",";
// $params['biz_content'].="\"total_amount\":\"" . $data['total_amount']. "\",";
// $params['biz_content'].="\"subject\":\"" . $data['subject'] . "\",";
// $params['biz_content'].="\"time_expire\":\"" . $time_expire . "\"}";
$params['charset'] = 'utf-8' ;
$params['method'] = 'alipay.trade.precreate' ;
$params['notify_url'] = $this->ali_notify_url ;
$params['sign_type'] = 'RSA' ;
$params['timestamp'] = date('Y-m-d H:i:s',time()) ;
$params['version'] = '1.0' ;
//生成签名 A-Z排序
// $rsaString = 'app_id='.$params['app_id'].'&biz_content='.$params['biz_content'].'&charset='.$params['charset'].'&method='.$params['method'].'¬ify_url='.$params['notify_url'].
// '&sign_type='.$params['sign_type'].'×tamp='.$params['timestamp'].'&version='.$params['version'];

$rsaString = $this->getSignContent($params);
$params['sign'] = $this->rsa_sign($rsaString);
//支付请求
$callback = self::sendPostRequst($url, $params);
$callback = json_decode(iconv("GBK", "UTF-8", "$callback"),true); //编码转为utf-8
//返回结果
if($callback['alipay_trade_precreate_response']['code'] == '10000'){
$msg['code'] = 1;
$msg['message'] = $callback['alipay_trade_precreate_response']['msg'];
$msg['qr_code'] = $callback['alipay_trade_precreate_response']['qr_code'];
}else{
$msg['code'] = 2;
$msg['message'] = $callback['alipay_trade_precreate_response']['sub_msg'];
}
return $msg;
}

/*签名排序*/
public function getSignContent($params) {
ksort ( $params );

$stringToBeSigned = "";
$i = 0;
foreach ( $params as $k => $v ) {
if (false === $this->checkEmpty ( $v ) && "@" != substr ( $v, 0, 1 )) {
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i ++;
}
}
unset ( $k, $v );
return $stringToBeSigned;
}

/*校验$value是否非空*/
public function checkEmpty($value) {
if (! isset ( $value ))
return true;
if ($value === null)
return true;
if (trim ( $value ) === "")
return true;

return false;
}

/*支付宝rsa签名方法*/
public function rsa_sign($data) {
$private_key = VENDOR_PATH.'Alipay/Key/rsa_private_key.pem'; //私钥路径*********************
$priKey = file_get_contents ($private_key);
$res = openssl_get_privatekey ( $priKey );
openssl_sign ( $data, $sign, $res );
openssl_free_key ( $res );
$sign = base64_encode ( $sign );
return $sign;
}

/*支付宝发送请求*/
public static function sendPostRequst($url, $data) {
$postdata = http_build_query ( $data );
//print_r($postdata);
$opts = array (
'http' => array (
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);


$context = stream_context_create ( $opts );

$result = file_get_contents ( $url, false, $context );
return $result;
}


/*支付宝退款
¶m $pay_sn(支付宝流水号),$price(退款金额),$batch_no(退款订单号)
*author zheng.wang(4995952@qq.com)
*date 2015-12-15
*/
public function aliRefund($pay_sn,$price,$batch_no){
$url = "https://openapi.alipay.com/gateway.do";
$params['app_id'] = $this->ali_app_id ;
//biz_content 业务参数需要使用json格式
// $biz_content['trade_no'] = $pay_sn; //支付宝订单号
// $biz_content['total_amount'] = $price; //退款金额
// $biz_content['out_request_no'] = $batch_no; //退款订单号
$params['biz_content'] = $biz_content = "{\"trade_no\":\"". $pay_sn . "\",\"refund_amount\":\""
. $price
. "\",\"out_request_no\":\""
. $batch_no
. "\",\"refund_reason\":\"reason\",\"store_id\":\"store001\",\"terminal_id\":\"terminal001\"}";
$params['charset'] = 'utf-8' ;
$params['method'] = 'alipay.trade.refund';
$params['sign_type'] = 'RSA' ;
$params['timestamp'] = date('Y-m-d H:i:s',time()) ;
$params['version'] = '1.0' ;
//生成签名
$data = 'app_id='.$params['app_id'].'&biz_content='.$params['biz_content'].'&charset='.$params['charset'].'&method='.$params['method'].
'&sign_type='.$params['sign_type'].'×tamp='.$params['timestamp'].'&version='.$params['version'];

$params['sign'] = $this->rsa_sign($data);
//支付请求
$callback = self::sendPostRequst($url, $params);
$callback = json_decode(iconv("GBK", "UTF-8", "$callback"),true);
//退款成功返回结果给
//return $callback;
var_dump($callback);
}

}
支付宝异步通知
没有写通知验证,可以自己写哈 /**
* Created by zheng.wang.
* Date: 2015-10-26
* Time: 15:29
*/

namespace Home\Controller;
use Think\Controller;

class NotifyController extends Controller{

/**
* 支付宝异步通知
* @param unknown $order_sn 订单号
* @param unknown $type 支付类型 ALI/WEIXIN
* @param unknown $result 支付结果 SUCCESS/FAIL
* @param array $data
* 支付成功时 $data['pay_time'] 支付完成时间
* 支付成功时 $data['pay_sn'] 支付流水号
*
* 支付失败时 $data['pay_time'] 支付完成时间
* 支付失败时 $data['pay_sn'] 支付流水号
*/
public function aliNotify(){
//接收支付异步通知post数据
$callBackData = $_POST;
//订单记录日志
file_put_contents('./Application/Runtime/Logs/xs_pay_alipay_notify.log', json_encode($callBackData), FILE_APPEND );
if($_POST['trade_status'] == 'TRADE_SUCCESS'){
//支付成功
$data['pay_time'] = strtotime($_POST['gmt_payment']);
$data['pay_sn'] = $_POST['trade_no'];
$res = D('Common/XsOrder')->payCallback($_POST['out_trade_no'], 'ALI', 'SUCCESS', $data);
if($res !== false){
//处理完完成后通知支付宝
echo "success";
}
}
}


}

支付宝扫码支付模型 附OpenSSL工具windows编译版本 thinkphp支付宝扫码支付.zip ( 1.27 MB 下载:45 次 )

AD:真正免费,域名+虚机+企业邮箱=0元