海外读书

This commit is contained in:
wuchunlei
2024-11-26 15:13:38 +08:00
parent 3fc7a80580
commit a0c42ecea0
37 changed files with 1234 additions and 9 deletions

View File

@@ -0,0 +1,183 @@
package com.peanut.modules.pay.paypal;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
@Component
@Slf4j
public class PaypalConfig {
//沙盒
public static final String modeUrl = "https://api-m.sandbox.paypal.com";
public static final String CLIENID ="Ab8SeEuhkLGp6Fts9V3Cti0UcXQhITRWZkiHDM3U1fDY9YrrRc5IOcYHPfV6qROhmh0hvgysqrfOCSUr";
public static final String SECRET ="EF63FGWI9fd4q07Ndvc_h8P7jxiZcGOWn8Ul_y1_EKpluKJNFHOW8BP62Kf1wBDQ3XYIeqF8_kVrDF7C";
public static final String WEBSCR ="https://www.sandbox.paypal.com/cgi-bin/webscr";//回调验证地址
//正式
// public static final String modeUrl = "https://api-m.paypal.com";
// public static final String CLIENID ="";
// public static final String SECRET ="";
// public static final String WEBSCR ="https://www.paypal.com/cgi-bin/webscr";
public static final String accessTokenURL = "/v1/oauth2/token";
public static final String createOrderURL = "/v2/checkout/orders";
public static final String captureURL = "/v2/checkout/orders/%1s/capture";
public static final String orderInfoURL = "/v2/checkout/orders/";
public static final String refundURL = "/v2/payments/captures/%1s/refund";
public static final String refundInfoURL = "/v2/payments/refunds/";
//回调验证
public String receiveVerify(String verifyData) throws Exception{
URL url = new URL(WEBSCR);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(verifyData);
writer.flush();
writer.close();
connection.getOutputStream().close();
InputStream responseStream = connection.getResponseCode() / 100 == 2
? connection.getInputStream()
: connection.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
return response;
}
//退款查询
public JSONObject refundInfo(String refundId) throws Exception{
URL url = new URL(modeUrl+refundInfoURL+refundId);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Authorization", "Bearer " + getAccessToken().get("access_token"));
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
return JSONObject.parseObject(response);
}
//退款
public JSONObject refund(String captureId) throws Exception{
URL url = new URL(String.format(modeUrl+refundURL,captureId));
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Authorization", "Bearer " + getAccessToken().get("access_token"));
httpConn.setRequestProperty("Prefer", "return=representation");
httpConn.setDoOutput(true);
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
return JSONObject.parseObject(response);
}
//订单查询
public JSONObject orderInfo(String paypalOrderId) throws Exception{
URL url = new URL(modeUrl+orderInfoURL+paypalOrderId);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setRequestProperty("Authorization", "Bearer " + getAccessToken().get("access_token"));
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
return JSONObject.parseObject(response);
}
//用户授权支付成功,进行扣款操作
public JSONObject capture(String paypalOrderId) throws Exception{
URL url = new URL(String.format(modeUrl+captureURL,paypalOrderId));
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("Content-Type", "application/json");
httpConn.setRequestProperty("Authorization", "Bearer " + getAccessToken().get("access_token"));
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
return JSONObject.parseObject(response);
}
//下单
public JSONObject createOrder(JSONObject jsonObject){
try{
URL u = new URL(modeUrl+createOrderURL);
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + getAccessToken().get("access_token"));
connection.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(jsonObject.toString());
writer.flush();
writer.close();
connection.getOutputStream().close();
InputStream responseStream = connection.getResponseCode() / 100 == 2
? connection.getInputStream()
: connection.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
return JSONObject.parseObject(response);
}catch (Exception e) {
log.error(">>>>>>>>>>:PayPal创建订单网络连接失败 reason = {}", e.getMessage());
e.printStackTrace();
return JSONObject.parseObject("{'error':'PayPal创建订单网络连接失败请联系管理员'}");
}
}
public JSONObject getAccessToken(){
try {
String credentials = CLIENID + ":" + SECRET;
String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes());
URL url = new URL(modeUrl+accessTokenURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Authorization", "Basic " + encodedCredentials);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setDoOutput(true);
String postParameters = "grant_type=client_credentials";
try (OutputStream os = connection.getOutputStream()) {
byte[] postParametersBytes = postParameters.getBytes();
os.write(postParametersBytes);
os.flush();
}
InputStream responseStream = connection.getResponseCode() / 100 == 2
? connection.getInputStream()
: connection.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
return JSONObject.parseObject(response);
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}