php支付宝支付

<?php

 

namespace lib;

use think\Loader;

/**
* Description of aliyunsms
*
* @author lsf
*/
class Alipay {

/**
*
* @param type $option ordersn单号,title订单名称,money订单金额,desc订单描述,app_id支付宝APPID,merchant_private_key商户私钥,您的原始格式RSA私钥,notify_url异步通知方法必须带http,return_url支付成功跳转,alipay_public_key支付宝公钥
* @return type
*/
public function alipayPayment($option = array()) {
Loader::import('alipay/wappay/service/AlipayTradeService', EXTEND_PATH);
Loader::import('alipay/wappay/buildermodel/AlipayTradeWapPayContentBuilder', EXTEND_PATH);
//商户订单号,商户网站订单系统中唯一订单号,必填
$out_trade_no = $option ["ordersn"];
//订单名称,必填
$subject = $option ["title"];
//付款金额,必填
$total_amount = $option ["money"];
//商品描述,可空
$body = $option ["desc"];
$product_code = "QUICK_WAP_WAY";
//超时时间
$timeout_express = "1m";
$payRequestBuilder = new \AlipayTradeWapPayContentBuilder();
$payRequestBuilder->setbizContent($product_code);
$payRequestBuilder->setBody($body);
$payRequestBuilder->setSubject($subject);
$payRequestBuilder->setOutTradeNo($out_trade_no);
$payRequestBuilder->setTotalAmount($total_amount);
$payRequestBuilder->setTimeExpress($timeout_express);
$config = $this->alipayConfig();
$payResponse = new \AlipayTradeService($config);
return $payResponse->wapPay($payRequestBuilder, $config['return_url'], $config['notify_url']);
}

public function alipayConfig($option) {
$config = array(
//应用ID,您的APPID。
'app_id' => $option ["app_id"],
//商户私钥,您的原始格式RSA私钥
'merchant_private_key' => $option ["merchant_private_key"],
//异步通知地址
'notify_url' => noticeAlipayUrl(),
//同步跳转
'return_url' => $option ["return_url"],
//编码格式
'charset' => "UTF-8",
//签名方式
'sign_type' => "RSA2",
//支付宝网关
'gatewayUrl' => "https://openapi.alipaydev.com/gateway.do",
"product_code" => "QUICK_WAP_WAY",
//支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
'alipay_public_key' => $option ["alipay_public_key"]
);
return $config;
}

public function alipayNotify($option, $data) {
Loader::import('alipay/wappay/service/AlipayTradeService', EXTEND_PATH);
$alipaySevice = new \AlipayTradeService($this->alipayConfig($option));
return $alipaySevice->check($data);
}

/**
* 支付APP支付
* @param type $option ordersn单号,title订单名称,money订单金额,app_id支付宝APPID,merchant_private_key商户私钥,您的原始格式RSA私钥,notify_url异步通知方法必须带http,alipay_public_key支付宝公钥
* @return type
*/
public function appAlipayPayment($option = array()) {

Loader::import('alipay/aop/AopClient', EXTEND_PATH);
$aopClient = new \AopClient();
$content['subject'] = $option ["title"];
$content['out_trade_no'] = $option ["ordersn"];
$content['total_amount'] = number_format($option ["money"], 2, ".", "");
$content['product_code'] = "QUICK_MSECURITY_PAY";
$con = json_encode($content);
$param = array();
$param['app_id'] = $option ["app_id"];
$param['method'] = 'alipay.trade.app.pay';
$param['charset'] = 'utf-8';
$param['sign_type'] = 'RSA2';
$param['timestamp'] = date("Y-m-d H:i:s");
$param['version'] = '1.0';
$param['notify_url'] = noticeAlipayUrl();
$param['biz_content'] = $con;
$paramStr = $aopClient->getSignContent($param);
$sign = $aopClient->alonersaSign($paramStr, $option["merchant_private_key"], 'RSA2');
$param['sign'] = $sign;
return $aopClient->getSignContentUrlencode($param);
}

public function appAlipayNotify($option, $data) {
Loader::import('alipay/aop/AopClient', EXTEND_PATH);
$aopClient = new \AopClient();
$aopClient->appId = $option["app_id"];
$aopClient->rsaPrivateKey = $option["merchant_private_key"];
$aopClient->signType = $data['sign_type'];
$aopClient->alipayrsaPublicKey = $option["alipay_public_key"];
return $aopClient->rsaCheckV1($data, "", $data['sign_type']);
}

}

上一篇:支付宝退款接口对接流程PHP语言


下一篇:java+支付宝转账+证书(一秒接入)