做全球性的支付,选用paypal!为什么选择paypal? 因为paypal是目前全球最大的在线支付工具,就像国内的支付宝一样,是一个基于买卖双方的第三方平台。买家只需知道你的paypal账号,即可在线直接把钱汇入你的账户,即时到账,简单方便快捷。

在集成paypal支付接口之前,首先要有一系列的准备,开发者账号啊、sdk、测试环境等等先要有,然后再码代码。集成的步骤如下:

一、环境准备

  • 注册paypal账号

  • 注册paypal开发者账号

  • 创建两个测试用户

  • 创建应用,生成用于测试的clientID 和 密钥

二、代码集成

  • springboot环境

  • pom引进paypal-sdk的jar包

  • 码代码

  • 测试

  • 后言

现在开始

  • 注册paypal账号

(1)在浏览器输入“https://www.paypal.com” 跳转到如下界面,点击右上角的注册

(2)选择,”创建商家用户”,根据要求填写信息,一分钟的事,注册完得去邮箱激活

  • 注册paypal开发者账号

    (1)在浏览器输入“https://developer.paypal.com”,点击右上角的“Log into Dashboard”,用上一步创建好的账号登录

  • 创建两个测试用户

(1)登录成功后,在左边的导航栏中点击 Sandbox 下的 Accounts

(2)进入Acccouts界面后,可以看到系统有两个已经生成好的测试账号,但是我们不要用系统给的测试账号,很卡的,自己创建两个

(3)点击右上角的“Create Account”,创建测试用户

<1> 先创建一个“ PERSONAL”类型的用户,国家一定要选“China”,账户余额自己填写

<2> 接着创建一个“BUSINESS”类型的用户,国家一定要选“China”,账户余额自己填写

<3>创建好之后可以点击测试账号下的”Profile“,可以查看信息,如果没加载出来,刷新

<4>用测试账号登录测试网站查看,注意!这跟paypal官网不同!不是同一个地址,在浏览器输入:https://www.sandbox.paypal.com 在这里登陆测试账户

  • 创建应用,生成用于测试的clientID 和 密钥

    (1)点击左边导航栏Dashboard下的My Apps & Credentials,创建一个Live账号,下图是我已经创建好的

(2)然后再到下边创建App

这是我创建好的“Test”App

(3)点击刚刚创建好的App“Test”,注意看到”ClientID“ 和”Secret“(Secret如果没显示,点击下面的show就会看到,点击后show变为hide)

  • springboot环境搭建

    (1)新建几个包,和目录,项目结构如下

  • pom引进paypal-sdk的jar包

    (1)pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.masasdani</groupId>
<artifactId>paypal-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>paypal-springboot</name> <repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone</url>
</repository>
<repository>
<id>jcenter-snapshots</id>
<name>jcenter</name>
<url>https://jcenter.bintray.com/</url>
</repository>
</repositories> <pluginRepositories>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>http://repo.spring.io/libs-milestone</url>
</pluginRepository>
</pluginRepositories> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.0.RELEASE</version>
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.4.2</version>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
  • 码代码

    (1)Application.java

package com.masasdani.paypal;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @EnableAutoConfiguration
@Configuration
@ComponentScan
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

(2)PaypalConfig.java

package com.masasdani.paypal.config;

import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.OAuthTokenCredential;
import com.paypal.base.rest.PayPalRESTException; @Configuration
public class PaypalConfig { @Value("${paypal.client.app}")
private String clientId;
@Value("${paypal.client.secret}")
private String clientSecret;
@Value("${paypal.mode}")
private String mode; @Bean
public Map<String, String> paypalSdkConfig(){
Map<String, String> sdkConfig = new HashMap<>();
sdkConfig.put("mode", mode);
return sdkConfig;
} @Bean
public OAuthTokenCredential authTokenCredential(){
return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig());
} @Bean
public APIContext apiContext() throws PayPalRESTException{
APIContext apiContext = new APIContext(authTokenCredential().getAccessToken());
apiContext.setConfigurationMap(paypalSdkConfig());
return apiContext;
}
}

(3)PaypalPaymentIntent.java

package com.masasdani.paypal.config;

public enum PaypalPaymentIntent {

    sale, authorize, order

}

(4)PaypalPaymentMethod.java

package com.masasdani.paypal.config;

public enum PaypalPaymentMethod {

    credit_card, paypal

}

(5)PaymentController.java

package com.masasdani.paypal.controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam; import com.masasdani.paypal.config.PaypalPaymentIntent;
import com.masasdani.paypal.config.PaypalPaymentMethod;
import com.masasdani.paypal.service.PaypalService;
import com.masasdani.paypal.util.URLUtils;
import com.paypal.api.payments.Links;
import com.paypal.api.payments.Payment;
import com.paypal.base.rest.PayPalRESTException; @Controller
@RequestMapping("/")
public class PaymentController { public static final String PAYPAL_SUCCESS_URL = "pay/success";
public static final String PAYPAL_CANCEL_URL = "pay/cancel"; private Logger log = LoggerFactory.getLogger(getClass()); @Autowired
private PaypalService paypalService; @RequestMapping(method = RequestMethod.GET)
public String index(){
return "index";
} @RequestMapping(method = RequestMethod.POST, value = "pay")
public String pay(HttpServletRequest request){
String cancelUrl = URLUtils.getBaseURl(request) + "/" + PAYPAL_CANCEL_URL;
String successUrl = URLUtils.getBaseURl(request) + "/" + PAYPAL_SUCCESS_URL;
try {
Payment payment = paypalService.createPayment(
500.00,
"USD",
PaypalPaymentMethod.paypal,
PaypalPaymentIntent.sale,
"payment description",
cancelUrl,
successUrl);
for(Links links : payment.getLinks()){
if(links.getRel().equals("approval_url")){
return "redirect:" + links.getHref();
}
}
} catch (PayPalRESTException e) {
log.error(e.getMessage());
}
return "redirect:/";
} @RequestMapping(method = RequestMethod.GET, value = PAYPAL_CANCEL_URL)
public String cancelPay(){
return "cancel";
} @RequestMapping(method = RequestMethod.GET, value = PAYPAL_SUCCESS_URL)
public String successPay(@RequestParam("paymentId") String paymentId, @RequestParam("PayerID") String payerId){
try {
Payment payment = paypalService.executePayment(paymentId, payerId);
if(payment.getState().equals("approved")){
return "success";
}
} catch (PayPalRESTException e) {
log.error(e.getMessage());
}
return "redirect:/";
} }

(6)PaypalService.java

package com.masasdani.paypal.service;

import java.util.ArrayList;
import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.masasdani.paypal.config.PaypalPaymentIntent;
import com.masasdani.paypal.config.PaypalPaymentMethod;
import com.paypal.api.payments.Amount;
import com.paypal.api.payments.Payer;
import com.paypal.api.payments.Payment;
import com.paypal.api.payments.PaymentExecution;
import com.paypal.api.payments.RedirectUrls;
import com.paypal.api.payments.Transaction;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException; @Service
public class PaypalService { @Autowired
private APIContext apiContext; public Payment createPayment(
Double total,
String currency,
PaypalPaymentMethod method,
PaypalPaymentIntent intent,
String description,
String cancelUrl,
String successUrl) throws PayPalRESTException{
Amount amount = new Amount();
amount.setCurrency(currency);
amount.setTotal(String.format("%.2f", total)); Transaction transaction = new Transaction();
transaction.setDescription(description);
transaction.setAmount(amount); List<Transaction> transactions = new ArrayList<>();
transactions.add(transaction); Payer payer = new Payer();
payer.setPaymentMethod(method.toString()); Payment payment = new Payment();
payment.setIntent(intent.toString());
payment.setPayer(payer);
payment.setTransactions(transactions);
RedirectUrls redirectUrls = new RedirectUrls();
redirectUrls.setCancelUrl(cancelUrl);
redirectUrls.setReturnUrl(successUrl);
payment.setRedirectUrls(redirectUrls); return payment.create(apiContext);
} public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException{
Payment payment = new Payment();
payment.setId(paymentId);
PaymentExecution paymentExecute = new PaymentExecution();
paymentExecute.setPayerId(payerId);
return payment.execute(apiContext, paymentExecute);
}
}

(7)URLUtils.java

package com.masasdani.paypal.util;

import javax.servlet.http.HttpServletRequest;

public class URLUtils {

    public static String getBaseURl(HttpServletRequest request) {
String scheme = request.getScheme();
String serverName = request.getServerName();
int serverPort = request.getServerPort();
String contextPath = request.getContextPath();
StringBuffer url = new StringBuffer();
url.append(scheme).append("://").append(serverName);
if ((serverPort != 80) && (serverPort != 443)) {
url.append(":").append(serverPort);
}
url.append(contextPath);
if(url.toString().endsWith("/")){
url.append("/");
}
return url.toString();
} }

(8)cancel.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1>Canceled by user</h1>
</body>
</html>

(9)index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<form method="post" th:action="@{/pay}">
<button type="submit"><img src="data:images/paypal.jpg" width="100px;" height="30px;"/></button>
</form>
</body>
</html>

(10)success.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Insert title here</title>
</head>
<body>
<h1>Payment Success</h1>
</body>
</html>

(11)最重要的!application.properties,paypal.client.app是App的CilentID, paypal.client.secret是Secret

server.port: 8088
spring.thymeleaf.cache=false paypal.mode=sandbox
paypal.client.app=AeVqmY_pxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxKYniPwzfL1jGR
paypal.client.secret=ELibZhExxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxUsWOA
  • 测试

    (1)启动项目

(2)在浏览器输入localhost:8088

(3)点击paypal后,会跳到paypal的登录界面,登录测试账号(PRESONAL)后点击继续即可扣费,扣500$(具体数额可在controller中自定义)

Payment payment = paypalService.createPayment(
500.00,
"USD",
PaypalPaymentMethod.paypal,
PaypalPaymentIntent.sale,
"payment description",
cancelUrl,
successUrl);

(4)到https://www.sandbox.paypal.com 登录测试账号看看余额有没有变化

http://blog.csdn.net/change_on/article/details/73881791

最详细的 paypal 支付接口开发--Java版的更多相关文章

  1. JAVA微信支付接口开发——支付

    微信支付接口开发--支付 这几天在做支付服务,系统接入了支付宝.微信.银联三方支付接口.个人感觉支付宝的接口开发较为简单,并且易于测试. 关于数据传输,微信是用xml,所以需要对xml进行解析. 1. ...

  2. php支付宝在线支付接口开发教程【转】

    php支付宝在线支付接口开发教程 这篇文章主要为大家详细介绍了php支付宝在线支付接口开发教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下   1.什么是第三方支付 所谓第三方支付,就是一些和各 ...

  3. 支付宝WAP支付接口开发(Node/Coffee语言)

    此博客不更新很久了, 更新的文档在这, 有兴趣到这里围观: http://neutra.github.io/2013/%E6%94%AF%E4%BB%98%E5%AE%9DWAP%E6%94%AF%E ...

  4. 【Java EE 学习 21 下】【 使用易宝支付接口实现java网上支付功能】

    一.网上支付分为两种情况,一种方法是使用直接和银行的支付接口,另外一种方法是使用第三方支付平台和银行对接完成支付. 1.直接和银行对接. 2.使用第三方支付平台 3.常见的第三方支付平台 二.使用易宝 ...

  5. php微信支付接口开发程序

    php微信支付接口开发程序讲解 微信支付接口现在也慢慢的像支付宝一个可以利用api接口来实现第三方网站或应用进行支付了, 下文整理了一个php微信支付接口开发程序并且己测试,有兴趣的朋友可进入参考. ...

  6. 支付宝WAP支付接口开发

    支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...

  7. php微信支付接口开发程序(流程已通)

    php微信支付接口开发程序(流程已通) 来源:未知    时间:2014-12-11 17:11   阅读数:11843   作者:xxadmin [导读] 微信支付接口现在也慢慢的像支付宝一个可以利 ...

  8. 【转】支付宝WAP支付接口开发

    支付宝WAP支付接口开发 因项目需要,要增加支付宝手机网站支付功能,找了支付宝的样例代码和接口说明,折腾两天搞定,谨以此文作为这两天摸索的总结.由于公司有自己的支付接口,并不直接使用这个接口,所以晚些 ...

  9. windows下shopex农行支付接口开发笔记

    1.首先是配置Java和tomcat 农行文档里的是linux下的说明.window下我们要按照以下在setclasspath.bat里设置JAVA_HOME,JRE_HOME(红色字体部分).设置这 ...

随机推荐

  1. 关于Unity中物体分别在本地和世界坐标系对应方向的移动

    方向 Vector3可以定义以世界坐标轴为参考的三维矢量,Vector3.forward,Vector3.up,Vector3.right方别对应物体世界坐标系的Z,Y,X轴方向的单位向量,或者叫三维 ...

  2. 对世界首款“人工智能”ERP系统HUE的初步了解 - AI ERP - WAP - 万革始

    偶然的机会,刚好在查找机器人王国[奇妙的餐厅]的时候,看到开发[光的王国],[奇妙的宾馆],[奇妙的餐厅]等豪斯登堡集团在3月17日采用了这个传说中的世界首款人工智能ERP系统AI WORKS的下面4 ...

  3. badgeview

    https://github.com/AlexLiuSheng/BadgeView include: compile 'com.allenliu.badgeview:library:1.1.1'(ne ...

  4. HTTP Headers 信息

    点开一个条目,可以查看请求的详细信息: General —— 通用信息 Request URL:请求的URL     Request Method:请求的方法     Status Code:响应状态 ...

  5. Oracle 11gR2(11.2.0.4)安装包(7个)作用说明

    在之前使用Oracle10G的时候,官网下载的数据库安装包只有两个文件,解压合并后为完整的安装包. 后来因为检查出多个Oracle漏洞,需要现场Oracle数据库版本需要升级到11.2.0.4,下载的 ...

  6. ux.form.field.Month 只能选年、月的时间扩展

    效果如图,亲测6.2.1版本可用,用法同时间选择控件 //月弹窗扩展 //只选月 Ext.define('ux.picker.Month', { extend: 'Ext.picker.Month', ...

  7. memcached解压报错gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now的解决方法

    最近在部署环境,在安装memcached的过程中解压时, 解压命令:tar -zvxf memcached-1.4.34.tar.gz 遇到了一个问题, gzip: stdin: not in gzi ...

  8. vue.js学习资料

    vue.js学习VuejsAPI教程 https://vuejs.org.cn/guide/Vuejs自己的构建工具 http://www.jianshu.com/p/f8e21d87a572如何用v ...

  9. Linux ReviewBoard安装与配置

    目录 0. 引言 1. 安装步骤 2. 配置站点 2.1 创建数据库 2.2 开始安装 2.3 修改文件访问权限 2.4 Web服务器配置 2.5 修改django相关配置 正文 回到顶部 0. 引言 ...

  10. iOS Icon Size 快速得到三种大小的图标

    在iOS开发中,按钮图标可以放三种大小,常见的是22x22,44x44,66x66,一般来说我们可以在PS中做好图片后,再分别导出三种大小的图标,但是每次要修改图片的大小,操作比较繁琐.这里博主推荐一 ...