支付宝-支付功能DEMO

Posted by BY Devin on May 19, 2017

前言

开发者文档:https://openhome.alipay.com/developmentDocument.htm

测试环境:沙箱环境 - 沙箱应用 - 下载(APP)根据提供的沙箱账号测试支付

首先:

下载服务端SDK(SDK通过应用使用蚂蚁金服的功能,相当于接口)资源包URL:

https://doc.open.alipay.com/doc2/detail?treeId=54&articleId=103419&docType=1

然后:

下载的SDK根目录创建 config.php 文件用于方便各功能调用初始化参数;

config.php

<?php
//构造参数
$aop = new AopClient ();
//支付宝网关
$aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';//这是用沙箱环境的url(正式环境请改换标准)
//应用ID,您的APPID         
$aop->appId = '2016080700189231';                             //这是用沙箱环境的appId(正式环境请改换标准)
//商户私钥
$aop->rsaPrivateKey = '通过蚂蚁金服提供工具生成的密钥';
//支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
$aop->alipayrsaPublicKey ='通过蚂蚁金服提供工具生成的公钥';
//版本
$aop->apiVersion = '1.0';
//签名方式
$aop->signType = 'RSA2';
//编码格式
$aop->postCharset= 'utf-8';
//传输格式
$aop->format='json';

电脑端网站支付功能(PHP版本)pc-index.php

1.下载的文件的根目录创建pc-index.php,代码如下:

注意:appId是应用的唯一ID;密钥公钥则按照蚂蚁金服提供的工具生成(2048类型);gatewayUrl为支付宝入口(固定url)

<?php

require_once("AopSdk.php");
require_once("config.php");    //调用上面初始化参数

//使用当前时间 + 6位随机数生成唯一购物订单ID
//(start)
function get_code($len){
    $CHAR_ARR = array('1','2','3','4','5','6','7','8','9','0');
    $CHAR_ARR_LEN = count($CHAR_ARR) - 1;
    $code = '';
    while(--$len > 0){ $code .= $CHAR_ARR[rand(0,$CHAR_ARR_LEN)]; }
    return $code;
}
$date = date('YmdHis');
$buyID=str_pad($date,19,get_code(6),STR_PAD_RIGHT);
//(end)

//定义参数
$subject      = 'Iphone6 16G';
$total_amount = '88.88';
$body         = 'Iphone6 16G';

//电脑端支付方法;
//(start)
$request = new AlipayTradePagePayRequest ();
$request->setReturnUrl('https://www.partsam.com/test/return_url.php');
$request->setNotifyUrl('https://www.partsam.com/test/notify_url.php');
$request->setBizContent('{"product_code":"FAST_INSTANT_TRADE_PAY","out_trade_no":'.$buyID.',"subject":"'. $subject .'","total_amount":"'. $total_amount .'","body":"'. $body  .'"}');
 
//请求
$result = $aop->pageExecute ($request); 
 
//输出
echo $result;
//(end)

?>

2.下载沙箱测试应用用于手机支付测试;(蚂蚁金服内含二维码提供下载)

url:https://openhome.alipay.com/platform/appDaily.htm?tab=info

手机端网站支付功能(PHP版本)mobile-index.php

1.下载的文件的根目录创建md-index.php,代码如下:

<?php

require_once("AopSdk.php");
require_once("config.php");    //调用上面初始化参数

//使用MD + 当前时间 + 6位随机数生成唯一购物订单ID
//(start)
function get_code($len){
    $CHAR_ARR = array('1','2','3','4','5','6','7','8','9','0');
    $CHAR_ARR_LEN = count($CHAR_ARR) - 1;
    $code = '';
    while(--$len > 0){ $code .= $CHAR_ARR[rand(0,$CHAR_ARR_LEN)]; }
    return $code;
}
$date = date('YmdHis');
$buyID='MD'.str_pad($date,20,get_code(6),STR_PAD_RIGHT);
//(end)

//定义参数
$arrBody      =  array('1.手机','2.衣服','3.家电');
$body         =  implode(";", $arrBody);
$subject      =  '大乐透';
$total_amount =  '9.00';

//移动端网站支付方法;
//(start)
$request = new AlipayTradeWapPayRequest ();
$request->setReturnUrl('https://www.partsam.com/return_url.php');  //必须是线上服务端URL
$request->setNotifyUrl('https://www.partsam.com/notify_url.php');  //必须是线上服务端URL
$request->setBizContent("{" .
"    \"body\":\"". $body ."\"," .
"    \"subject\":\"". $subject ."\"," .
"    \"out_trade_no\":\"". $buyID ."\"," .
"    \"timeout_express\":\"90m\"," .
"    \"total_amount\":". $total_amount ."," .
"    \"product_code\":\"QUICK_WAP_PAY\"" .
"  }");

$result = $aop->pageExecute ($request); 
echo $result;
//(end)

2.根目录创建 notify.data 空文件;

3.异步返回数据API:https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.q2krgF&treeId=270&articleId=105902&docType=1#s1 (注意:这里的异步返回是指淘宝返回过来。并非我们异步返过去。对象是不一样的;返回的POST数据不能通过$_POST获取,而是通过file_put_contents( );将返回数据写入文本中)

下面把POST返回的数据保存到notify.data中;

<?php
    file_put_contents(__DIR__.'/notify.data', $_POST); //__DIR__代表当前执行PHP代码的路径
?>