抖音小程序支付宝的接口怎么写?

抖音对接支付宝的流程

先要通过服务端获取抖音提供的SP订单号;

再把SP单号等一些数据发送给支付宝。

再把支付宝返回的信息传回抖音端这样支付就对接好了。

抖音小程序支付宝的接口怎么写?

include 'alipay.class.php';
include 'AopSdk.php';
$risk_info=json_encode(array('ip'=>$_SERVER['REMOTE_ADDR']));
$array=array(
'app_id'=>'800117472149',
'method'=>'tp.trade.create',
'charset'=>'utf-8',
'sign_type'=>'MD5',
'timestamp'=>time(),
'version'=>'1.0',
'out_order_no'=>'20190727000',
'uid'=>'4ybyfXwNMulvUhBT',
'merchant_id'=>'1900011759',
'total_amount'=>100,
'currency'=>'CNY',
'subject'=>'商户订单名称',
'body'=>'商户订单详情',
'trade_time'=>time(),
'valid_time'=>'20',
'notify_url'=>'https://www.juxunapp.com/',
'risk_info'=>$risk_info
);

$aplipay = new alipay($array);

$res=$aplipay->pay();
app_id = isset($args['app_id']) ? $args['app_id'] : null;
$this->method = isset($args['method']) ? $args['method'] : null;
$this->charset = isset($args['charset']) ? $args['charset'] : null;
$this->sign_type = isset($args['sign_type']) ? $args['sign_type'] : null;
$this->timestamp = isset($args['timestamp']) ? $args['timestamp'] : null;
$this->version = isset($args['version']) ? $args['version'] : null;
$this->out_order_no = isset($args['out_order_no']) ? $args['out_order_no'] : null;
$this->uid = isset($args['uid']) ? $args['uid'] : null;
$this->merchant_id = isset($args['merchant_id']) ? $args['merchant_id'] : null;
$this->total_amount = isset($args['total_amount']) ? $args['total_amount'] : null;
$this->currency = isset($args['currency']) ? $args['currency'] : null;
$this->subject = isset($args['subject']) ? $args['subject'] : null;
$this->body = isset($args['body']) ? $args['body'] : null;
$this->trade_time = isset($args['trade_time']) ? $args['trade_time'] : null;
$this->valid_time = isset($args['valid_time']) ? $args['valid_time'] : null;
$this->notify_url = isset($args['notify_url']) ? $args['notify_url'] : null;
$this->risk_info = isset($args['risk_info']) ? $args['risk_info'] : null;

$biz_content = json_encode(array(
'out_order_no'=>$this->out_order_no,
'uid'=>$this->uid,
'merchant_id'=>$this->merchant_id,
'total_amount'=>$this->total_amount,
'currency'=>$this->currency,
'subject'=>$this->subject,
'body'=>$this->body,
'trade_time'=>$this->trade_time ,
'valid_time'=>$this->valid_time,
'notify_url'=>$this->notify_url,
'risk_info'=>$this->risk_info
));

$this->biz_content=$biz_content;

$sign['app_id']=$this->app_id;
$sign['method']=$this->method;
$sign['charset']=$this->charset;
$sign['sign_type']=$this->sign_type;
$sign['timestamp']=$this->timestamp;
$sign['version']=$this->version;
$sign['biz_content']=$this->biz_content;

$this->sign = md5($this->getSignContent($sign,'utf-8','gx9j1saqpjkjgp024kzm6ypncfufruaxk76xk12b'));

}

public function pay(){
$url="https://tp-pay.snssdk.com/gateway";
$data=array('app_id'=>$this->app_id,'method'=>$this->method,'charset'=>$this->charset,'sign_type'=>$this->sign_type,'sign'=>$this->sign,'timestamp'=>$this->timestamp,'version'=>$this->version,'biz_content'=>$this->biz_content);
$res=json_decode($this->send_post($url,$data),true);
//在这里是要将数据返回给前端,通过前端拉起支付宝收银台

$ali_data=array(
'body'=>$this->body,
'subject'=>$this->subject,
'body'=>$this->body,
'fee'=>$this->total_amount
);
$ali_data2=array(
'trade_no'=>$res['response']['trade_no']
);

$config=array(
'ali_app_app_id'=>'2019121069845600',
'ali_app_rsa_pri_key'=>'MIFEowIBAAKCAQEAxAfJcO3atT9LofwrDJuZP09nzUgNW5VKdZmSxHnXLqjzo+w2AIbBVmURTLtyDFAJAB19QZ2BXNtll2pb8rXonqQHZFi+5+cyB2/pLKRRNdaqGlQTbLShpbFhPrVNUFgg/zb2O5XiL1PXH3abNY45fVTiyseDJbIlGq1SeH12A5f5UZTA+/bsUTe2HLgupGltTNA3/OwBsw2u3zgr/aYjsj9mf029+HbzZtVMpJlQ2If80tcE9k8U75u+21H/ujDjKYThVbAKQPDSoUmLWlFPasExqRsKffIZdxbANNsKnXpJAP00NA0Nk06d3xmjfYdZiQtkpMo0myKF5JWUGRz7wwIDAQABAoIBAA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T9J953lJdGUdj7rQI87mg8NamNrAhcfdg5HuXSDlfLhlnW9rT16pc2yCi3n5qZDCpydAmBB2MS91vteRvg05p17tzRIghxQuFSyl+djeEK1KtHcvMcblFL56G7YEAoBhRlkWeiVr0B',
'ali_app_pay_rsa_pub_key'=>'MIIAIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxAfJcO3atT9LofwrDJuZP09nzUgNW5VKdZmSxHnXLqjzo+w2AIbBVmURTLtyDFAJAB19QZ2BXNtll2pb8rXonqQHZFi+5+cyB2/pLKRRNdaqGlQTbLShpbFhPrVNUFgg/zb2O5XiL1PXH3abNY45fVTiyseDJbIlGq1SeH12A5f5UZTA+/bsUTe2HLgupGltTNA3/OwBsw2u3zgr/aYjsj9mf029+HbzZtVMpJlQ2If80tcE9k8U75u+21H/ujDjKYThVbAKQPDSoUmLWlFPasExqRsKffIZdxbANNsKnXpJAP00NA0Nk06d3xmjfYdZiQtkpMo0myKF5JWUGRz7wwIDAQAB',
);

$website="https://www.juxunapp.com/";
$data2 = [];
$data2['trade_no'] = $res['response']['trade_no']; //刚刚获取到的trade_no订单号
$data2['app_id'] = $this->app_id; //头条支付分配给业务方的ID
$data2['sign_type'] = $this->sign_type; //头条采用的MD5
$data2['timestamp'] = (string)$this->timestamp; //发送请求的时间
$data2['merchant_id'] = $this->merchant_id; //头条支付分配给业务方的商户号
$data2['uid'] = $this->uid; //唯一标识用户open_id(这个是头条的)
$data2['total_amount'] = $this->total_amount; //金额,分为单位,应传整型
$data2['params'] = json_encode(['url' => $this->aliapppaytest($ali_data , $biz_content , $ali_data2 , $website , $config)]); //这里是支付宝APP支付的请求参数,我在这里使用了支付宝的SDK

$stringToBeSigned = $this->getSignContent($data2, $this->charset, 'gx9j1saqpjkjgp024kzm6ypncfufruaxk76xk12b'); //将以上参数再次进行签名处理
$data2["sign"] = md5($stringToBeSigned);

$data2['pay_channel'] = "ALIPAY_NO_SIGN";
$data2["pay_type"] = "ALIPAY_APP";
$data2['method'] = 'tp.trade.confirm';
$data2['risk_info'] = $this->risk_info;
return ['error'=>0 , 'msg'=>'success' , 'data'=>$data2]; // 将这些参数,返回给前端处理就可以了
//return $res;
//return json_encode($this->alipayData($res));
}

public function aliapppaytest($data , $biz_content , $data2 , $website , $config){
require 'AopSdk.php';
$aop = new \AopClient();
$aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
$aop->appId = $config['ali_app_app_id']; //支付宝APPID
$aop->rsaPrivateKey = $config['ali_app_rsa_pri_key']; //支付宝秘钥
$aop->format = "json";
$aop->charset = "UTF-8";
$aop->signType = "RSA2";
$aop->alipayrsaPublicKey = $config['ali_app_pay_rsa_pub_key']; //支付宝公钥

//实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
$request = new AlipayTradeAppPayRequest(); //SDK已经封装掉了公共参数,这里只需要传入业务参数
$data2 = [
'body' => $data['body'],
'subject' => $data['subject'],
'out_trade_no' => $data2['trade_no'],
'timeout_express' => '30m',
'total_amount' => $data['fee']/1000,
'product_code' => 'QUICK_MSECURITY_PAY',
];
$postdata = json_encode($data2);
$request->setNotifyUrl($website . '/payment/toutiao/notify.php'); //服务器异步通知http地址);
$request->setBizContent($postdata);
//$response = $aop->sdkExecute($request); //这里和普通的接口调用不同,使用的是sdkExecute

$response = $aop->sdkExecute ( $request);

//htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
// return htmlspecialchars($str);//就是orderString 可以直接给客户端请求,无需再做处理。
return $response;//就是orderString 可以直接给客户端请求,无需再做处理。
}

public function alipayData($res){

$biz_content=json_encode(array(
'subject'=>$this->subject,
'out_trade_no'=>$this->out_order_no,
'total_amount'=>$this->total_amount,
'product_code'=>"QUICK_MSECURITY_PAY"

));

$url=array(
'app_id'=>$this->app_id,
'biz_content'=>$biz_content,
'charset'=>$this->charset,
'method'=>'alipay.trade.app.pay',
'notify_url'=>$this->notify_url,
'sign'=>$res['sign'],
'sign_type'=>'RSA2',
'timestamp'=>time(),
'version'=>$this->version
);

$url = $this->getSignContent($url,'utf-8','');

$data=array(
'app_id'=>$this->app_id,
'sign_type'=>'MD5',
'timestamp'=>(string)$this->timestamp,
'trade_no'=>$res['response']['trade_no'],
'merchant_id'=>'1900011747',
'uid'=>'4ybyfXwNMulvUhBT',
'total_amount'=>100,
'params'=>json_encode(array('url'=>$url)),
'sign'=>$this->sign,
'method'=>'tp.trade.confirm',
'pay_channel'=>'ALIPAY_NO_SIGN',
'pay_type'=>'ALIPAY_APP',
'risk_info'=> $this->risk_info
);

return $data;
}

public function getSignContent($params , $charset,$app_secret) {
ksort($params);
$stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
// 转换成目标字符集
$v = $this->characet($v, $charset);
if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
}
$stringToBeSigned = $stringToBeSigned.$app_secret;

unset ($k, $v);
return $stringToBeSigned;
}
public function checkEmpty($value) {
if (!isset($value))
return true;
if ($value === null)
return true;
if (trim($value) === "")
return true;
return false;
}

public function characet($data, $targetCharset) {
if (!empty($data)) {
$fileType = "UTF-8";
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
}
}
return $data;
}

public function send_post($url, $post_data) {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => 'POST',
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;

}

}

?>

Page({
data: {},
onLoad: function () {

},
pay: function () {
tt.request({
url: 'https://www.juxunapp.com/alipay2/aop/test/index.php', // 目标服务器url
success: function (res) {
console.log(res.data.data)
tt.requestPayment({
data:res.data.data,
success(res) {
console.log(res);
},
fail(res) {
console.log(res);
}
})
}
});

}
});

您还未添加分享代码,请到主题选项中,添加百度分享代码!

您可以选择一种方式赞助本站

支付宝转账赞助

支付宝扫一扫赞助

微信钱包扫描赞助

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

图片 表情