Javapaypal如何实现付费订阅(循环扣费)

更新时间:2023-06-27 19:27:28 阅读: 评论:0

Javapaypal如何实现付费订阅(循环扣费)
背景
业务需求要集成Paypal,实现循环扣款功能,初识PayPal开发⽂档,搞得⼈⼀脸懵逼,终于完整实现所有功能,这⾥对如何使⽤Paypal的⽀付接⼝做下总结。
账号申请
api阅读
订阅流程
创建商品
创建计划
创建订阅
接收回调
注:
这⾥着重介绍⼀下计划,计划⾥⾯包含了收费规则,安装费⽤(或者说使⽤费),重试机制,税率等。设置完成后,创建订阅时,传⼊计划ID即可
上代码
1、获取token
本⽂使⽤纯API交互,远程⼯具使⽤的是httpClient,但是PayPal与其他不同,需要登录才能使⽤,所以本⽂使⽤的hutool的httpclient⼯具类,需要设置类型为basicAuth,且输⼊⽤户密码(clientId,cret),话不多说,上代码:
/**
* 获取token
* @return
* @throws IOException
*/
public String getToken(){
String body = HttpRequest.post(BASE_URL +"/v1/oauth2/token")
.basicAuth(urname, password)
.form("grant_type","client_credentials")
.execute().body();
JSONObject jsonObject = JSONObject.parObject(body);
("access_token").toString();
}
2、创建商品
/**
* 创建商品
* @return
* @throws JsonProcessingException
*/
public String createProduct()throws JsonProcessingException {
背影图Product product =new Product();
product.tName("tc-004");
//商品种类,枚举
product.tCategory("BOOKS_PERIODICALS_AND_NEWSPAPERS");
product.tDescription("test product 4");
product.tHomeUrl("xxxx");
product.tImageUrl("xxxx");
//类型枚举
product.tType("SERVICE");
Map<String,String> map =new HashMap<>(4);
map.put("Content-Type","application/json");
map.put("Authorization",getToken());
// 请求ID,如果保持⼀致,可以进⾏多次请求,结果⼀致
//        map.put("PayPal-Request-Id","PLAN-18062019-001");
String string =new ObjectMapper().writeValueAsString(product);
String body = HttpRequest.post(BASE_URL +"/v1/catalogs/products")
.addHeaders(map)
.basicAuth(urname,password)
.body(string)
.execute().body();
//        System.out.println(body);
return body;
}
3、创建计划
/**
* 创建计划
* @param productId
* @return
* @throws JsonProcessingException
*/
public String createPlan(String productId)throws JsonProcessingException {
String dateTime = w().toString();
String token =getToken();
Map<String,String> map =new HashMap<>(4);
培根map.put("Content-Type","application/json");
map.put("Authorization",token);
/
/        map.put("PayPal-Request-Id","PLAN-18062019-001");
PayPalSubscriptionPlanParam planParam =new PayPalSubscriptionPlanParam();        List<BillingCycles> list =new ArrayList<>();
BillingCycles billingCycles =new BillingCycles();
billingCycles.tTenureType("REGULAR");
billingCycles.tSequence(1);
billingCycles.tTotalCycles(12);
//计费周期
Frequency frequency =new Frequency();
frequency.tIntervalUnit("DAY");
frequency.tIntervalCount(1);
PricingScheme pricingScheme =new PricingScheme();
pricingScheme.tVersion(1);
pricingScheme.tCreateTime(dateTime);
心好痛//定价
FixedPrice fixedPrice =new FixedPrice();
fixedPrice.tCurrencyCode("USD");
fixedPrice.tValue("100");
pricingScheme.tFixedPrice(fixedPrice);
billingCycles.tFrequency(frequency);
billingCycles.tPricingScheme(pricingScheme);
list.add(billingCycles);
// 付款偏好
PaymentPreferences paymentPreferences =new PaymentPreferences();
paymentPreferences.tAutoBillOutstanding(true);
paymentPreferences.tPaymentFailureThreshold(3);
paymentPreferences.tSetupFeeFailureAction("CANCEL");
SetupFee tupFee =new SetupFee();
tupFee.tCurrencyCode("USD");
tupFee.tValue("0");
paymentPreferences.tSetupFee(tupFee);
// 税率
Taxes taxes =new Taxes();
taxes.tInclusive(true);
taxes.tPercentage("0");
planParam.tTaxes(taxes);
planParam.tPaymentPreferences(paymentPreferences);
planParam.tBillingCycles(list);
planParam.tProductId(productId);
planParam.tName("test plan");
planParam.tStatus("ACTIVE");
planParam.tDescription("test");
planParam.tCreateTime(dateTime);
planParam.tUpdateTime(dateTime);
String string =new ObjectMapper().writeValueAsString(planParam);
System.out.println(string);
//        System.out.println();
String body = HttpRequest.post(BASE_URL +"/v1/billing/plans")
.addHeaders(map)
.basicAuth(urname,password)
.body(string)
.execute().body();
return body;
}
}
4、创建订阅
/
**
* 创建订阅
* @param planId
* @return
*/
public String createSubscription(String planId){
计算机时代Map<String,String> map =new HashMap<>(4);
map.put("Content-Type","application/json");
map.put("Authorization",getToken());
// P-7EG815794T029494CMFR77TA
//        String planId = "P-4ND94871NA4029913MFSTSXI";
String string =handlerSubsParam(planId);
String body = HttpRequest.post(BASE_URL +"/v1/billing/subscriptions")
.addHeaders(map)
.basicAuth(urname,password)
.body(string)
.execute().body();
JSONObject jsonObject = JSONObject.parObject(body);
List<Links> links = JSONObject.("links").toString(), Links.class);        Links links1 = links.stream().filter(o ->"approve".Rel())).findFirst() .orElThrow(()->new RuntimeException("sadsadsada"));
System.out.JSONString(links));
System.out.Href());
return body;
}
/**
* 处理订阅参数
*
* @param planId
* @return
*/
private String handlerSubsParam(String planId){
SubscriptionDTO subscriptionDTO =new SubscriptionDTO();
subscriptionDTO.tPlanId(planId);
/
/        subscriptionDTO.tQuantity("10");
//        subscriptionDTO.tShippingAmount("10");
//        subscriptionDTO.tStartTime(new Date());
//        subscriptionDTO.tQuantity("10");
Subscriber subscriber =new Subscriber();
中国鲎SubscriberName subscriberName =new SubscriberName();
subscriberName.tGiven_name("lijing");
subscriberName.tSurname("xiong");
subscriber.tName(subscriberName);
subscriber.tEmailAddress("xiong.");
ShippingAddress shippingAddress =new ShippingAddress();
/
哭的成语/注意最后⼀个字段,必须严格参照枚举字典,中国是C2,不是CN
shippingAddress.tAddress(new Address("XXXX","","","","","C2"));
shippingAddress.tName(new Name("xiong lijing"));
subscriber.tShippingAddress(shippingAddress);
subscriptionDTO.tSubscriber(subscriber);
ApplicationContext applicationContext =new ApplicationContext();
//        applicationContext.tBrandName("");
applicationContext.tCancelUrl("");
//        applicationContext.tShippingPreference("");
//        applicationContext.tUrAction("");
applicationContext.tReturnUrl("");
applicationContext.tPaymentMethod(new PaymentMethod());
subscriptionDTO.tApplicationContext(applicationContext);
String string ="";
try{
string =new ObjectMapper().writeValueAsString(subscriptionDTO);
//            System.out.println(string);
//            System.out.println(string);
}catch(JsonProcessingException e){
e.printStackTrace();
}
return string;
}
PayPal订阅与PayPal单笔⽀付类似,最后会⼀个最终处理URL,点击进⼊即可登陆账号,付款等,所以处理结果即可。本⽂需要引⼊依赖:
<dependency>
<groupId>s</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
成人高考时间<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.13</version>
</dependency>
<dependency>
<groupId>com.</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.4</version>
</dependency>
衣服颜色<!--paypal-->
<dependency>
<groupId>com.braintreepayments.gateway</groupId>
<artifactId>braintree-java</artifactId>
<version>2.87.0</version>
</dependency>
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>checkout-sdk</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.paypal.sdk</groupId>
<artifactId>rest-api-sdk</artifactId>
<version>1.14.0</version>
</dependency>
⽂章代码已上传⾄github,源码有需要可以⾃⾏下载,实体类太多,可以从源码直接clone下来。
有问题可以QQ联系:114424977

本文发布于:2023-06-27 19:27:28,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1057643.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:订阅   需要   创建
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图