国际支付-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

相关文章

Python FastAPI实现JWT校验的完整指南

《PythonFastAPI实现JWT校验的完整指南》在现代Web开发中,构建安全的API接口是开发者必须面对的核心挑战之一,本文将深入探讨如何基于FastAPI实现JWT(JSONWebToken... 目录一、JWT认证的核心原理二、项目初始化与环境配置三、安全密码处理机制四、JWT令牌的生成与验证五、

JavaScript实战:智能密码生成器开发指南

本文通过JavaScript实战开发智能密码生成器,详解如何运用crypto.getRandomValues实现加密级随机密码生成,包含多字符组合、安全强度可视化、易混淆字符排除等企业级功能。学习密码强度检测算法与信息熵计算原理,获取可直接嵌入项目的完整代码,提升Web应用的安全开发能力 目录

Linux网络配置之网桥和虚拟网络的配置指南

《Linux网络配置之网桥和虚拟网络的配置指南》这篇文章主要为大家详细介绍了Linux中配置网桥和虚拟网络的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 一、网桥的配置在linux系统中配置一个新的网桥主要涉及以下几个步骤:1.为yum仓库做准备,安装组件epel-re

Python Pandas高效处理Excel数据完整指南

《PythonPandas高效处理Excel数据完整指南》在数据驱动的时代,Excel仍是大量企业存储核心数据的工具,Python的Pandas库凭借其向量化计算、内存优化和丰富的数据处理接口,成为... 目录一、环境搭建与数据读取1.1 基础环境配置1.2 数据高效载入技巧二、数据清洗核心战术2.1 缺失

NGINX 配置内网访问的实现步骤

《NGINX配置内网访问的实现步骤》本文主要介绍了NGINX配置内网访问的实现步骤,Nginx的geo模块限制域名访问权限,仅允许内网/办公室IP访问,具有一定的参考价值,感兴趣的可以了解一下... 目录需求1. geo 模块配置2. 访问控制判断3. 错误页面配置4. 一个完整的配置参考文档需求我们有一

Python使用Reflex构建现代Web应用的完全指南

《Python使用Reflex构建现代Web应用的完全指南》这篇文章为大家深入介绍了Reflex框架的设计理念,技术特性,项目结构,核心API,实际开发流程以及与其他框架的对比和部署建议,感兴趣的小伙... 目录什么是 ReFlex?为什么选择 Reflex?安装与环境配置构建你的第一个应用核心概念解析组件

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

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

Vue 2 项目中配置 Tailwind CSS 和 Font Awesome 的最佳实践举例

《Vue2项目中配置TailwindCSS和FontAwesome的最佳实践举例》:本文主要介绍Vue2项目中配置TailwindCSS和FontAwesome的最... 目录vue 2 项目中配置 Tailwind css 和 Font Awesome 的最佳实践一、Tailwind CSS 配置1. 安

MyBatis分页插件PageHelper深度解析与实践指南

《MyBatis分页插件PageHelper深度解析与实践指南》在数据库操作中,分页查询是最常见的需求之一,传统的分页方式通常有两种内存分页和SQL分页,MyBatis作为优秀的ORM框架,本身并未提... 目录1. 为什么需要分页插件?2. PageHelper简介3. PageHelper集成与配置3.

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF