做全球性的支付,选用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;@Beanpublic Map<String, String> paypalSdkConfig(){Map<String, String> sdkConfig = new HashMap<>();sdkConfig.put("mode", mode);return sdkConfig;}@Beanpublic OAuthTokenCredential authTokenCredential(){return new OAuthTokenCredential(clientId, clientSecret, paypalSdkConfig());}@Beanpublic 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());@Autowiredprivate 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 {@Autowiredprivate 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=falsepaypal.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 登录测试账号看看余额有没有变化

  • 后言

最详细的 paypal 支付接口开发--Java版相关推荐

  1. python paypal支付接口开发

    (第一次接触支付,理解可能也不是很深,欢迎批评指正) paypal是目前全球最大的在线支付工具,就像国内的支付宝一样,是一个基于买卖双方的第三方平台. PayPal API是基于HTTP的restfu ...

  2. php支付接口验签,银联支付接口开发php版

    官方文档:https://open.unionpay.com/ajweb/help/file/techFile?productId=1 api辅助工具:https://open.unionpay.co ...

  3. paypal支付接口开发笔记--Java版

    接入流程文档见:https://blog.csdn.net/change_on/article/details/73881791 支付回调文档见:https://www.freesion.com/ar ...

  4. ASP做paypal支付接口详细代码和实例

    ASP做贝宝(paypal)支付接口 !! 今天网速很慢,想想整理一下前些天做的接口吧! 整理 ! 整理一下 ! 事实上,paypal可以说分两个 : 国际www.paypal.com 中国www.p ...

  5. java支付接口开发原理_java对接微信支付接口开发的过程是什么?

    java对接微信支付接口开发的过程是什么?以下是小编整理的java对接微信支付接口实现的方法和过程,有需要的朋友可以学习以下的java对接微信支付接口内容. java对接微信支付接口开发的过程是什么? ...

  6. [chatgpt] golang集成paypal支付接口

    go 语言集成paypal支付接口 PayPal 是一种常用的在线支付服务.如果要在 Go 语言项目中使用 PayPal 支付接口,您需要使用 PayPal 提供的 API.下面是 Go 语言中如何集 ...

  7. 公众号php支付接口开发,公众号支付接口的开发

    这次给大家带来公众号支付接口的开发,公众号支付接口开发的注意事项有哪些,下面就是实战案例,一起来看一下. 公众号支付就是在微信里面的H5页面唤起微信支付,不用扫码即可付款的功能.做这个功能首先要明确的 ...

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

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

  9. php银联支付接口 demo,php版银联支付接口开发简单实例详解

    这篇文章主要介绍了php版银联支付接口开发的方法,结合实例形式分析了php银联支付接口开发的具体流程与相关操作技巧,需要的朋友可以参考下 支付接口现在有第三方的支付接口也有银行的支付接口.这里就来介绍 ...

  10. PHP银联在线支付接口开发日志

    银联在线支付接口开发日志 1. 登录银联自助化测试平台(登陆地址:open.unionpay.com),登录后,点击我的产品,如下:点击右方需要测试的接口,本例以 手机网页支付(WAP支付)为例. 2 ...

最新文章

  1. 微信小程序实现图书管理系统
  2. mysql的三大引擎是什么_MySQL常用三大存储引擎
  3. [k8s]elk架构设计-k8s集群里搭建
  4. 阿里云AIoT正式发布IoT安全中心和IoT Studio 3.0,进一步巩固AIoT云网边端基础能力
  5. python 清屏_Python学前准备如果你知道要去哪,那么全世界都给你让路
  6. greenplum gpfdist应用
  7. Linux OpenSSL获取证书指纹值(443、MD5、SHA1、SHA256)
  8. Hibrenate实现根据实体类自动创建表或添加字段
  9. ffmpeg -视频旋转和高清转码示例
  10. HTML学习总结(2)——标题/水平线/注释/段落/折行/文本格式化
  11. 51单片机对直流电机的控制
  12. Keil uVision4 安装包及破解程序
  13. 基金暴跌年轻人为什么躲不过被割?
  14. 使用 Byzanz 录制 Gif 动画或 Ogv 视频
  15. BUUctf刷题第三天
  16. 如何基于 APISIX 迭代数字智联平台
  17. 计算机网络第七版4-46题答案,计算机软考网络管理员考试题及答案(44-46)
  18. 创维YS代工E900V21E/TY1608-S905l3B-8822CS及7668无线通刷线刷包
  19. SIGGRAPH中海洋的研究学习
  20. 如何推动乡村振兴的落地

热门文章

  1. CrossApp 0.3.8 发布,跨平台移动App开发引擎
  2. 几何实体图形保存成stl格式的ascII和二进制文。用Vc++语言读入文件,给三角网格坐标值乘以2,并保存到另一stl文件。输出完成工作所用的执行时间
  3. Hbase数据库安装
  4. mt管理器主题修改教程_QQ主题+微博主题
  5. JAVA实现微信公众号推送消息
  6. JSESSIONID是什么
  7. android输入法横向,Android输入法横向评测—手写输入篇
  8. Android如何实现汉字手写输入法(带中文自动识别提示)
  9. MATLAB中使用XLSREAD无法找到文件的一种解决方法
  10. CAN通讯程序C语言,基于单片机的CANBUS程序(C语言)