国际支付-paypal配置入门指南

2024-03-15 21:50

本文主要是介绍国际支付-paypal配置入门指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

paypal官方文档

一、PayPal的api文档:

https://developer.paypal.com/dashboard/
https://developer.paypal.com/docs/integration/direct/payments/paypal-payments/#search-payment-details

常用结账方式
在这里插入图片描述

PayPal配置密钥

配置webhook

Sandbox Webhooks
分别对应:事件回调地址 webhooks的id 触发事件类型

PayPal

  1. 开发者账号
    https://developer.paypal.com/developer/accounts?event=createSuccess
  2. 查找商户账户密码
  3. 再登陆https://www.sandbox.paypal.com/mep/dashboard
  4. 在浏览器输入:https://www.sandbox.paypal.com/,点击登录,输入创建好的 ”商家账号“ 的邮箱和密码

API流程

Integration steps 集成步骤

  1. Required 必填 Set up your development environment
    .
    设置开发环境。
  2. Required 必填 Create PayPal payment
    . 创建PayPal付款。
  3. Required 必填 Get payment approval
    . 获得付款批准。
  4. Required 必填 Execute payment
    . 执行付款。
  5. Optional 自选 Search payment details
    . 搜索付款详细信息。

Create PayPal payment 创建PayPal付款
After you collect the payment details from the customer, specify the payment details in a /payment call.
从客户收集付款详细信息后,在 /payment 呼叫中指定付款详细信息。
In the request URI, set the .
在请求 URI 中,设置 .
In the JSON request body, set the intent to sale, the redirect URLs, the payment_method to paypal, and the transaction information in the transactions array, which contains one or more transaction objects:

在 JSON 请求正文中,在 transactions 包含一个或多个 transaction 对象的数组中设置 to、重定向 URL、 intent payment_method to paypal sale 和事务信息:

curl -v -X POST https://api-m.sandbox.paypal.com/v1/payments/payment \-H "Content-Type: application/json" \-H "Authorization: Bearer <Access-Token>" \-d '{"intent": "sale","payer": {"payment_method": "paypal"},"transactions": [{"amount": {"total": "30.11","currency": "USD","details": {"subtotal": "30.00","tax": "0.07","shipping": "0.03","handling_fee": "1.00","shipping_discount": "-1.00","insurance": "0.01"}},"description": "This is the payment transaction description.","custom": "EBAY_EMS_90048630024435","invoice_number": "48787589673","payment_options": {"allowed_payment_method": "INSTANT_FUNDING_SOURCE"},"soft_descriptor": "ECHI5786786","item_list": {"items": [{"name": "hat","description": "Brown color hat","quantity": "5","price": "3","tax": "0.01","sku": "1","currency": "USD"}, {"name": "handbag","description": "Black color hand bag","quantity": "1","price": "15","tax": "0.02","sku": "product34","currency": "USD"}],"shipping_address": {"recipient_name": "Hello World","line1": "4thFloor","line2": "unit#34","city": "SAn Jose","country_code": "US","postal_code": "95131","phone": "011862212345678","state": "CA"}}}],"note_to_payer": "Contact us for any questions on your order.","redirect_urls": {"return_url": "https://example.com","cancel_url": "https://example.com"}
}'

A successful call returns confirmation of the transaction, with the created state and a payment ID that you can use in subsequent calls:

Execute payment 支付执行完
To execute the payment after the customer’s approval, make a /payment/execute/ call. In the JSON request body, use the payerID value that was passed to your site. In the header, use the access token that you used when you created the payment.

curl -v -X POST https://api-m.sandbox.paypal.com/v1/payments/payment/PAY-34629814WL663112AKEE3AWQ/execute \-H "Content-Type: application/json" \-H "Authorization: Bearer <Access-Token>" \-d '{"payer_id": "RRCYJUTFJGZTA"
}'
The execute payment call returns a paymentobject with transaction details:{"id": "PAY-4N746561P0587231SKQQK6MY","create_time": "2014-09-22T23:22:27Z","update_time": "2014-09-22T23:31:13Z","state": "approved","intent": "sale","payer": {"payment_method": "paypal","payer_info": {"email": "npurayil-uspr-60@paypal.com","first_name": "Brian","last_name": "Robinson","payer_id": "JMKDKJ4D7DG7G","shipping_address": {"line1": "4thFloor","line2": "unit#34","city": "SAn Jose","state": "CA","postal_code": "95131","country_code": "US","phone": "011862212345678","recipient_name": "HelloWorld"}}},"transactions": [{"amount": {"total": "30.11","currency": "USD","details": {"subtotal": "30.00","tax": "0.07","shipping": "0.03","handling_fee": "1.00","insurance": "0.01","shipping_discount": "-1.00"}},"description": "This is the payment transaction description.","item_list": {"items": [{"name": "hat","sku": "1","price": "3.00","currency": "USD","quantity": "5","description": "Brown color hat","tax": "0.01"},{"name": "handbag","sku": "product34","price": "15.00","currency": "USD","quantity": "1","description": "Black color handbag","tax": "0.02"}],"shipping_address": {"recipient_name": "HelloWorld","line1": "4thFloor","line2": "unit#34","city": "SAn Jose","state": "CA","phone": "011862212345678","postal_code": "95131","country_code": "US"}},"related_resources": [{"sale": {"id": "4XP56210M0797192Y","create_time": "2014-09-22T23:22:27Z","update_time": "2014-09-22T23:31:13Z","amount": {"total": "30.11","currency": "USD"},"payment_mode": "INSTANT_TRANSFER","state": "completed","protection_eligibility": "ELIGIBLE","protection_eligibility_type": "ITEM_NOT_RECEIVED_ELIGIBLE","transaction_fee": {"value": "1.75","currency": "USD"},"parent_payment": "PAY-4N746561P0587231SKQQK6MY","links": [{"href": "https://api-m.paypal.com/v1/payments/sale/4XP56210M0797192Y","rel": "self","method": "GET"},{"href": "https://api-m.paypal.com/v1/payments/sale/4XP56210M0797192Y/refund","rel": "refund","method": "POST"},{"href": "https://api-m.paypal.com/v1/payments/payment/PAY-4N746561P0587231SKQQK6MY","rel": "parent_payment","method": "GET"}]}}]}],"links": [{"href": "https://api-m.paypal.com/v1/payments/payment/PAY-4N746561P0587231SKQQK6MY","rel": "self","method": "GET"}]
}

refund 退款

curl -v https://api-m.sandbox.paypal.com/v1/payments/sale/67D22837NN7279935/refund \-H "Content-Type: application/json" \-H "Authorization: Bearer <Access-Token>" \-d '{}'

二、实战代码

标准结账支付

yaml配置

paypal配置
paypal.mode=sandbox/live
paypal.client.app = xxx
paypal.client.secret = xxxxxxxxxxxxxxxx
paypal.client.webhook_id =xxx
maven sdk引入
 <dependency><groupId>com.paypal.sdk</groupId><artifactId>rest-api-sdk</artifactId><version>1.14.0</version><exclusions><exclusion><artifactId>gson</artifactId><groupId>com.google.code.gson</groupId></exclusion></exclusions></dependency>

Create PayPal payment 创建PayPal付款

/*** 创建订单** @param moneyBigDecimal* @return* @throws PayPalRESTException*/@Overridepublic Payment createPayment(BigDecimal moneyBigDecimal) throws PayPalRESTException {log.info("订单金额:{}", moneyBigDecimal.doubleValue());// 成功与失败回调地址String cancelUrl = PAYPAL_CANCEL_URL.replace("{Redirect_Url}",redirectUrl).replace("{contextPath}", contextPath);String successUrl = PAYPAL_SUCCESS_URL.replace("{Redirect_Url}",redirectUrl).replace("{contextPath}", contextPath);Payment payment = this.createPayment(moneyBigDecimal.doubleValue(),"USD",PayPalPaymentMethod.paypal,PayPalPaymentIntent.sale,"payment description",cancelUrl,successUrl);if (ObjectUtils.isNotEmpty(payment)) {log.info("Payment created successfully");} else {log.error("Payment Fail");return null;}return payment;}
 @Resourceprivate APIContext apiContext;
/*** 支付方法** @param total       交易金额* @param currency    货币类型* @param method      枚举-作用* @param intent      枚举-意图* @param description 交易描述* @param cancelUrl   交易取消后跳转url* @param successUrl  交易成功后跳转url* @return Payment* @throws PayPalRESTException*/@Overridepublic 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);//设置返回的urlRedirectUrls redirectUrls = new RedirectUrls();redirectUrls.setCancelUrl(cancelUrl);redirectUrls.setReturnUrl(successUrl);//加入反馈对象payment.setRedirectUrls(redirectUrls);//加入认证并创建交易return payment.create(apiContext);}
支付完成回调//获取PayPal支付信息 入参:paymentIdPayment payment = Payment.get(apiContext, paymentId);String payerId;if (payment.getPayer() != null) {payerId = payment.getPayer().getPayerInfo().getPayerId();log.info("payerId:{}", payerId);//执行回调Payment executePayment = this.executePayment(paymentId, payerId);log.info("执行回调executePayment:{}", executePayment);if (!org.springframework.util.ObjectUtils.isEmpty(executePayment)) {log.info("Payment execute successfully");log.info(executePayment.toJSON());}String saleId = null;Sale sale = executePayment.getTransactions().get(0).getRelatedResources().get(0).getSale();if (org.springframework.util.ObjectUtils.isEmpty(sale) && "completed".equals(sale.getState())) {saleId = sale.getId();}
 /*** 并执行交易(相当于提交事务)** @param paymentId* @param payerId* @return* @throws PayPalRESTException*/@Overridepublic 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);}

参考文档:

https://www.pianshen.com/article/3031768019/

这篇关于国际支付-paypal配置入门指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/813376

相关文章

Linux中压缩、网络传输与系统监控工具的使用完整指南

《Linux中压缩、网络传输与系统监控工具的使用完整指南》在Linux系统管理中,压缩与传输工具是数据备份和远程协作的桥梁,而系统监控工具则是保障服务器稳定运行的眼睛,下面小编就来和大家详细介绍一下它... 目录引言一、压缩与解压:数据存储与传输的优化核心1. zip/unzip:通用压缩格式的便捷操作2.

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

Linux中SSH服务配置的全面指南

《Linux中SSH服务配置的全面指南》作为网络安全工程师,SSH(SecureShell)服务的安全配置是我们日常工作中不可忽视的重要环节,本文将从基础配置到高级安全加固,全面解析SSH服务的各项参... 目录概述基础配置详解端口与监听设置主机密钥配置认证机制强化禁用密码认证禁止root直接登录实现双因素

嵌入式数据库SQLite 3配置使用讲解

《嵌入式数据库SQLite3配置使用讲解》本文强调嵌入式项目中SQLite3数据库的重要性,因其零配置、轻量级、跨平台及事务处理特性,可保障数据溯源与责任明确,详细讲解安装配置、基础语法及SQLit... 目录0、惨痛教训1、SQLite3环境配置(1)、下载安装SQLite库(2)、解压下载的文件(3)、

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

深度解析Spring Boot拦截器Interceptor与过滤器Filter的区别与实战指南

《深度解析SpringBoot拦截器Interceptor与过滤器Filter的区别与实战指南》本文深度解析SpringBoot中拦截器与过滤器的区别,涵盖执行顺序、依赖关系、异常处理等核心差异,并... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现

MySQL追踪数据库表更新操作来源的全面指南

《MySQL追踪数据库表更新操作来源的全面指南》本文将以一个具体问题为例,如何监测哪个IP来源对数据库表statistics_test进行了UPDATE操作,文内探讨了多种方法,并提供了详细的代码... 目录引言1. 为什么需要监控数据库更新操作2. 方法1:启用数据库审计日志(1)mysql/mariad

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

SpringBoot集成LiteFlow工作流引擎的完整指南

《SpringBoot集成LiteFlow工作流引擎的完整指南》LiteFlow作为一款国产轻量级规则引擎/流程引擎,以其零学习成本、高可扩展性和极致性能成为微服务架构下的理想选择,本文将详细讲解Sp... 目录一、LiteFlow核心优势二、SpringBoot集成实战三、高级特性应用1. 异步并行执行2