支付宝支付接口的调用

1.准备工作

第一步就是先要注册一个支付宝的账号(注册这里不说,不是重点),然后登入官方首页,去到应用列表里面找到沙箱应用
基本信息的APPID很重要,后续配置文件要配置的app_id就是这个。
在这里插入图片描述
系统密钥,查看公钥模式,merchant_private_key就是应用私钥;alipay_public_key支付宝公钥;支付宝网关地址https://openapi.alipaydev.com/gateway.do因为是沙箱测试环境,这里是dev标识加以区分;签名方式这里是RSA2

沙箱账号有商家和买家的账号支付密码和余额,后续付款会用得到的
在这里插入图片描述

2.获取Demo (JAVA版)

支付宝文档中心支付应用SDK&&DEMO下载代码demo,用来在本地加以调试就行。
在这里插入图片描述

3.导入本地运行测试

本地环境是Eclipse+JDK1.8 及以上+Tomcat8.0,导入本地。
在这里插入图片描述
本地下载一个tomcat服务器,本地配置好添加进去,启动tomcat就可以正常启动访问项目。
在这里插入图片描述

AlipayConfig文件主要是配置,APPID,私钥等这些信息,具体配置参考下文

package com.alipay.config;

import java.io.FileWriter;
import java.io.IOException;

/* *
 *类名:AlipayConfig
 *功能:基础配置类
 *详细:设置帐户有关信息及返回路径
 *修改日期:2017-04-05
 *说明:
 *以下代码只是为了方便商户测试而提供的样例代码,商户可以根据自己网站的需要,按照技术文档编写,并非一定要使用该代码。
 *该代码仅供学习和研究支付宝接口使用,只是提供一个参考。
 */

public class AlipayConfig {
	
//↓↓↓↓↓↓↓↓↓↓请在这里配置您的基本信息↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

	// 应用ID,您的APPID,收款账号既是您的APPID对应支付宝账号 https://open.alipay.com/develop/sandbox/app 这个链接查找
	public static String app_id = "XXX";
	
	// 商户私钥,您的PKCS8格式RSA2私钥
    public static String merchant_private_key = "XXX";
	
	// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    public static String alipay_public_key = "XXX";

	// 服务器异步通知页面路径  需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String notify_url = "http://127.0.0.1:8080/alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";

	// 页面跳转同步通知页面路径 需http://格式的完整路径,不能加?id=123这类自定义参数,必须外网可以正常访问
	public static String return_url = "http://127.0.0.1:8080/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";

	// 签名方式
	public static String sign_type = "RSA2";
	
	// 字符编码格式
	public static String charset = "utf-8";
	
	// 支付宝网关 https://openapi.alipaydev.com/gateway.do
	public static String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";
	
	// 支付宝网关
	public static String log_path = "C:\";


//↑↑↑↑↑↑↑↑↑↑请在这里配置您的基本信息↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑

    /** 
     * 写日志,方便测试(看网站需求,也可以改成把记录存入数据库)
     * @param sWord 要写入日志里的文本内容
     */
    public static void logResult(String sWord) {
        FileWriter writer = null;
        try {
            writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
            writer.write(sWord);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}


index.jsp是支付宝电脑网站支付体验入口页,访问链接http://127.0.0.1:8080/alipay.trade.page.pay-JAVA-UTF-8/index.jsp
在这里插入图片描述
点击付款触发访问alipay.trade.page.pay.jsp在这个jsp页面调用支付接口
在这里插入图片描述

点击付款按钮跳转到付款页面,这个页面不是本地的页面在这里插入图片描述

输入用户名和支付密码点击下一步完成付款,点击确认付款
在这里插入图片描述
付款成功页面,这个页面显示之后等待几秒会跳转到return_url页面,展示商家设定的页面
在这里插入图片描述
http://127.0.0.1:8080/alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp
在这里插入图片描述

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>