springboot整合pi支付开发

pi支付流程图:

  1. 使用Pi SDK功能发起支付
  2. 由 Pi SDK 自动调用的回调函数(让您的应用服务器知道它需要发出批准 API 请求)
  3. 从您的应用程序服务器到 Pi 服务器的 API 请求以批准付款(让 Pi 服务器知道您知道此付款)
  4. Pi浏览器向用户显示付款详细信息页面,我们正在等待用户签署交易
  5. 由 Pi SDK 自动调用的回调函数(让您的应用服务器知道它需要发出完整的 API 请求)
  6. 从您的应用服务器到 Pi 服务器的 API 请求以完成付款(让 Pi 服务器知道您已完成此付款)

支付流程

 引入依赖

  <dependency><groupId>com.squareup.okhttp3</groupId><artifactId>okhttp</artifactId><version>4.10.0-RC1</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.0.M4</version></dependency><dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency>

配置api密钥

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;/*** 服务器端key* @author ThinkPad*/
@Configuration
@Data
public class CommonConfig {@Value("${sdk.serverAccessKey}")private String serverAccessKey;
}

 接收和返回数据对象

loginVO接收pi中心来的用户信息

import lombok.Data;/*** @author wzx* 登录数据封装类*/
@Data
public class LoginVO {private String userId;private String userName;private String accessToken;
}

paymentVO接收支付授权的信息

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.math.BigDecimal;/*** @author ThinkPad*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class PaymentVO {private String paymentId;// 交易金额private BigDecimal amount;// 名片对应的用户数据private String shopUserId;// 商品idprivate String shopId;// 当前账号用户的idprivate String userId;}

completeVO接收支付完成的信息

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author ThinkPad*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CompleteVO {// PI支付IDprivate String paymentId;// txIdprivate String txId;// 订单ID【余额支付参数】private String orderId;// 支付方式:0:PI钱包 1:余额支付private String payType;
}

incompleteVO接收未完成订单的信息

/*** @author ThinkPad*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class IncompleteVO {private String identifier;private TransactionVO transaction;
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author ThinkPad*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TransactionVO {private String txid;private String _link;
}

工具类

发起http请求工具类

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;/*** @author Ashy.Cheung* @http 请求工具类* @date 2017.11.10*/
public class HttpClientUtil {public static String sendGet(String url) {CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet(url);CloseableHttpResponse response = null;try {response = httpclient.execute(httpget);} catch (IOException e1) {e1.printStackTrace();}String result = null;try {HttpEntity entity = response.getEntity();if (entity != null) {result = EntityUtils.toString(entity);}} catch (ParseException | IOException e) {e.printStackTrace();} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return result;}/**** @param url* @param charsetName 返回字符集* @return*/public static String sendGet(String url, String charsetName) {InputStream inputStream = null;HttpURLConnection urlConnection = null;try {URL url1 = new URL(url);urlConnection = (HttpURLConnection) url1.openConnection();// 将返回的输入流转换成字符串inputStream = urlConnection.getInputStream();// 指定编码格式if (StringUtils.isBlank(charsetName)) {charsetName = "UTF-8";}InputStreamReader inputStreamReader = new InputStreamReader(inputStream, charsetName);BufferedReader in = new BufferedReader(inputStreamReader);String jsonUserStr = in.readLine();return jsonUserStr;} catch (Exception e) {throw new RuntimeException(e);} finally {try {if (null != inputStream) {inputStream.close();}urlConnection.disconnect();} catch (Exception e) {}try {if (null != urlConnection) {urlConnection.disconnect();}} catch (Exception e) {}}}/*** 发送HttpPost请求,参数为String* 接收端以流形式接收*/public static String sendPost(String url, String param) {CloseableHttpClient httpclient = HttpClients.createDefault();StringEntity strEntity = null;try {strEntity = new StringEntity(param, "UTF-8");strEntity.setContentType("application/json");} catch (Exception e1) {e1.printStackTrace();}HttpPost httppost = new HttpPost(url);httppost.setEntity(strEntity);CloseableHttpResponse response = null;String result = null;try {response = httpclient.execute(httppost);HttpEntity entity1 = response.getEntity();result = EntityUtils.toString(entity1);} catch (IOException e) {//  e.printStackTrace();} finally {try {response.close();} catch (Exception e) {}}return result;}/*** 发送不带参数的HttpPost请求*/public static String sendPost(String url) {CloseableHttpClient httpclient = HttpClients.createDefault();HttpPost httppost = new HttpPost(url);CloseableHttpResponse response = null;try {response = httpclient.execute(httppost);} catch (IOException e) {e.printStackTrace();}HttpEntity entity = response.getEntity();String result = null;try {result = EntityUtils.toString(entity);} catch (ParseException | IOException e) {e.printStackTrace();} finally {try {response.close();} catch (Exception e) {}}return result;}}

 分布式锁工具类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.stereotype.Component;import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
@Component
public class RedisLockUtil {private static final Logger log = LoggerFactory.getLogger(RedisLockUtil.class);@ResourceRedisTemplate<String, Object> redisTemplate;/*** 释放锁脚本,原子操作,lua脚本*/private static final String UNLOCK_LUA;static {StringBuilder sb = new StringBuilder();sb.append("if redis.call(\"get\",KEYS[1]) == ARGV[1] ");sb.append("then ");sb.append("    return redis.call(\"del\",KEYS[1]) ");sb.append("else ");sb.append("    return 0 ");sb.append("end ");UNLOCK_LUA = sb.toString();}/*** 获取分布式锁,原子操作** @param lockKey   锁* @param lockValue 唯一ID* @param leaseTime 过期时间 秒* @return 是否枷锁成功*/public  boolean tryLock(String lockKey, String lockValue, long leaseTime) {try {RedisCallback<Boolean> callback = (connection) -> connection.set(lockKey.getBytes(StandardCharsets.UTF_8),lockValue.getBytes(StandardCharsets.UTF_8), Expiration.seconds(leaseTime),RedisStringCommands.SetOption.SET_IF_ABSENT);return redisTemplate.execute(callback);} catch (Exception e) {log.error("redis lock error ,lock key: {}, value : {}, error info : {}", lockKey, lockValue, e);}return false;}/*** 释放锁** @param lockKey   锁* @param lockValue 唯一ID* @return 执行结果*/public  boolean unlock(String lockKey, String lockValue) {RedisCallback<Boolean> callback = (connection) -> connection.eval(UNLOCK_LUA.getBytes(), ReturnType.BOOLEAN, 1, lockKey.getBytes(StandardCharsets.UTF_8), lockValue.getBytes(StandardCharsets.UTF_8));return redisTemplate.execute(callback);}/*** 获取分布式锁,该方法不再使用** @param lockKey   锁* @param lockValue 唯一ID* @param waitTime  等待时间 秒* @param leaseTime 过期时间 秒* @return 是否枷锁成功*/@Deprecatedpublic   boolean tryLock(String lockKey, String lockValue, long waitTime, long leaseTime) {try {RedisCallback<Boolean> callback = (connection) -> connection.set(lockKey.getBytes(StandardCharsets.UTF_8),lockValue.getBytes(StandardCharsets.UTF_8), Expiration.seconds(leaseTime),RedisStringCommands.SetOption.SET_IF_ABSENT);return redisTemplate.execute(callback);} catch (Exception e) {log.error("redis lock error ,lock key: {}, value : {}, error info : {}", lockKey, lockValue, e);}return false;}
}

生成uuid工具类

import java.text.SimpleDateFormat;
import java.util.Date;public class UUID {public static String randomUUID() {SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd'T'HH'H'mm'M'ss'S'SSS");String id = sdf.format(new Date()) + (int) ((Math.random() * 9 + 1) * 100000000) + (int) ((Math.random() * 9 + 1) * 10);return id;}public static String randomQr() {String id = (int) ((Math.random() * 9 + 1) * 1000) + "-" + (int) ((Math.random() * 9 + 1) * 1000) + "-" + (int) ((Math.random() * 9 + 1) * 1000);return id;}}

信息返回的枚举

import lombok.AllArgsConstructor;import lombok.Getter;/*** @author ThinkPad*/@Getter
@AllArgsConstructor
public enum PaymentEnum {PAYMENT_ENUM_1(1, "订单不存在","失败"),PAYMENT_ENUM_2(2,"订单不是待支付状态","失败"),PAYMENT_ENUM_3(3,"支付金额少于订单金额","失败"),PAYMENT_ENUM_4(4,"调用太快","失败"),PAYMENT_ENUM_5(5,"余额不足,请前往充值","失败"),PAYMENT_ENUM_6(6,"支付成功","成功"),PAYMENT_ENUM_7(7,"处理成功","失败"),PAYMENT_ENUM_8(8,"处理失败","失败");private final Integer code;private final String msg;private final String status;public static String getMsgByCode(Integer code) {for (PaymentEnum value : PaymentEnum.values()) {if (value.getCode().equals(code)) {return value.getMsg();}}return null;}public static String getStatusByCode(Integer code) {for (PaymentEnum value : PaymentEnum.values()) {if (value.getCode().equals(code)) {return value.getStatus() ;}}return null;}}

支付的controller层

 /*** 处理未完成的订单 (这部十分重要,会影响到后面的操作)*/@PostMapping("payOrder/incomplete")@ApiOperation("处理未完成的订单")@ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")public ResponseVO incomplete(@RequestBody IncompleteVO incompleteVO) {try {return orderInfoService.incomplete(incompleteVO);} catch (Exception e) {log.error("报错如下:{}", e.getMessage());throw new BusinessException("支付失败,请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());}}/*** 前端请求支付授权,在本地订单创建后调*/@PostMapping("payOrder/approve")@ApiOperation("前端请求支付授权,在本地订单创建后调")@ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")public ResponseVO<String> approve(@RequestBody PaymentVO paymentVO) {try {String orderId = orderInfoService.approve(paymentVO);return ResponseVO.getSuccessResponseVo(orderId);} catch (Exception e) {log.error("报错如下:{}", e.getMessage());throw new BusinessException("支付失败,请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());}}/*** 前端支付完成,余额支付直接调用此方法*/@PostMapping("payOrder/complete")@ApiOperation("前端支付完成,余额支付直接调用此方法")@ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")public ResponseVO complete(@RequestBody CompleteVO completeVO) {try {return orderInfoService.complete(completeVO);} catch (Exception e) {log.error("报错如下:{}", e.getMessage());throw new BusinessException("支付失败,请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());}}/*** 取消支付,订单关闭*/@PostMapping("payOrder/cancelled")@ApiOperation("取消支付,订单关闭")@ApiImplicitParam(name = "Authorization", value = "传入你的令牌",required = true, dataType = "String",paramType="header")public ResponseVO<String> cancelled(@RequestBody String orderId) {try {Boolean order = orderInfoService.cancelled(orderId);
//            if (!order){throw  new BusinessException("取消订单失败");}return ResponseVO.getSuccessResponseVo("取消订单成功");} catch (Exception e) {log.error("报错如下:{}", e.getMessage());throw new BusinessException("取消失败,请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());}}

支付的service层

    /*** 请求支付授权,创建order。返回orderId* @param paymentVO* @return*/@Override@Transactional(rollbackFor = Exception.class)public String approve(PaymentVO paymentVO) {log.error("approve-------------------------------------------------------------");OrderInfo orderInfo;log.error("paymentVO----------------------------------"+paymentVO);//获取付款信息OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url("https://api.minepi.com/v2/payments/" + paymentVO.getPaymentId()).addHeader("Authorization", "Key " + commonConfig.getServerAccessKey()).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {String string = response.body().string();JSONObject jsonObject1 = JSON.parseObject(string);log.error("!response-------------------------------------------------------------"+commonConfig.getServerAccessKey());throw new RuntimeException("payments error " + jsonObject1.getString("error_message"));}String string = response.body().string();log.error("response-------------------------------------------------------------"+string);JSONObject jsonObject1 = JSON.parseObject(string);//校验实际支付金额BigDecimal userFinalPrice = paymentVO.getAmount();if (userFinalPrice.compareTo(jsonObject1.getBigDecimal("amount")) < 0) {log.error(userFinalPrice+"response-------------------------------------------------------------"+jsonObject1.getBigDecimal("amount"));throw new RuntimeException("支付金额少于订单金额");}} catch (Exception e) {throw  new BusinessException("支付失败,请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());}OkHttpClient client1 = new OkHttpClient();//信息真实,通知PI我准备好了,可以付款了Request request1 = new Request.Builder().url("https://api.minepi.com/v2/payments/" + paymentVO.getPaymentId() + "/approve").addHeader("Content-Type", "application/json").addHeader("Access-Control-Allow-Origin", "*").addHeader("Authorization", "Key " + commonConfig.getServerAccessKey()).post(RequestBody.create("", MediaType.parse("application/json"))).build();try (Response response1 = client1.newCall(request1).execute()) {if (!response1.isSuccessful()) {throw new RuntimeException("approve error: ");}log.error("response1-------------------------------------------------------------");//更新支付报文
//                    tMerStoreGoodsOrderEntity.setPayOrderId(paymentDto.getPaymentId());
//                    tMerStoreGoodsOrderEntity.setPayStatusType("10007002");//支付中
//                    itMerStoreGoodsOrderService.updateById(tMerStoreGoodsOrderEntity);log.error("return-------------------------------------------------------------");} catch (RuntimeException | IOException e) {log.error("error-------------------------------------------------------------");e.printStackTrace();}// 生成订单orderInfo = new OrderInfo();orderInfo.setOrderId(StringUtil.generateShortId());orderInfo.setShopId(paymentVO.getShopId());orderInfo.setUserId(paymentVO.getUserId());orderInfo.setShopUserId(paymentVO.getShopUserId());orderInfo.setAmount(paymentVO.getAmount());orderInfoMapper.insert(orderInfo);log.error("生成订单-------------------------------------------------------------");return orderInfo.getOrderId();}/*** 前端支付完成,余额支付直接调用此方法*/@Override@Transactional(rollbackFor = Exception.class)public ResponseVO complete(CompleteVO completeVO) {String payType = completeVO.getPayType();log.error("complete------------------------------------------------------------"+completeVO);if ("1".equals(payType)) {//余额支付String orderId = completeVO.getOrderId();String lockName = "access:lock:complete:" + orderId;String lockId = UUID.randomUUID();if (!redisLockUtil.tryLock(lockName, lockId, 20L)) {// 调用太快return new ResponseVO(PaymentEnum.getStatusByCode(4),4,PaymentEnum.getMsgByCode(4));}// 获取订单信息OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", orderId));if ((orderInfo.getOrderStatus() != 0)) {// 订单不是待支付状态return new ResponseVO(PaymentEnum.getStatusByCode(2),2,PaymentEnum.getMsgByCode(2));}String userId = orderInfo.getUserId();AccountInfo accountInfo = accountInfoMapper.selectOne(new QueryWrapper<AccountInfo>().eq("user_id", userId));BigDecimal balance = accountInfo.getPiBalance();if (balance.compareTo(orderInfo.getAmount()) < 0) {// 余额不足,请前往充值return new ResponseVO(PaymentEnum.getStatusByCode(5),5,PaymentEnum.getMsgByCode(5));}int update = orderInfoMapper.update(null,new UpdateWrapper<OrderInfo>().eq("order_id",orderId).set("order_status",1));balance=balance.subtract(orderInfo.getAmount());int update1 = accountInfoMapper.update(null, new UpdateWrapper<AccountInfo>().eq("user_id", userId).set("pi_balance", balance));// 支付成功return new ResponseVO(PaymentEnum.getStatusByCode(6),6,PaymentEnum.getMsgByCode(6));}//PI钱包支付String paymentId = completeVO.getPaymentId();//PI订单号String lockName = "access:lock:complete:" + paymentId;String lockId = UUID.randomUUID();log.error(paymentId+"-----------------"+lockName+"---------------------"+lockId);if (!redisLockUtil.tryLock(lockName, lockId, 20L)) {// 调用太快log.error("!RedisLockUtil---------------------------------------------------------调用太快");return new ResponseVO(PaymentEnum.getStatusByCode(4),4,PaymentEnum.getMsgByCode(4));}OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", completeVO.getOrderId()));log.error("orderId--------------------------------------------------------------"+orderInfo);if (null == orderInfo) {// 订单不存在log.error("!orderinfo--------------------------------------------------------不存在");return new ResponseVO(PaymentEnum.getStatusByCode(1),1,PaymentEnum.getMsgByCode(1));}log.error("orderinfo------------------------------------------------------------------"+orderInfo);if (orderInfo.getOrderStatus() != 0) {// 订单不是待支付状态log.error("!order---------------------------------------------------------pay");return  new ResponseVO(PaymentEnum.getStatusByCode(2),2,PaymentEnum.getMsgByCode(2));}//通知PI完成交易JSONObject jsonObject = new JSONObject();jsonObject.put("txid", completeVO.getTxId());Map<String, String> heads = new HashMap<>();heads.put("Content-Type", "application/json;charset=UTF-8");heads.put("Authorization", "Key " + commonConfig.getServerAccessKey());log.error("pi-----------------------------------------"+jsonObject);try {HttpResponse response = HttpRequest.post("https://api.minepi.com/v2/payments/" + paymentId + "/complete").headerMap(heads, false).body(String.valueOf(jsonObject)).timeout(5 * 60 * 1000).execute();String body = response.body();JSONObject jsonObject1 = JSON.parseObject(body);String error = jsonObject1.getString("error");if (!StringUtils.isEmpty(error)) {log.error("!strinutils-----------------------------"+body);throw new RuntimeException("订单完成异常!");}orderInfo.setOrderStatus(1);// 更新订单orderInfoMapper.updateById(orderInfo);log.error("支付成功------------------------------------------------------");// 支付成功return  new ResponseVO(PaymentEnum.getStatusByCode(6),6,PaymentEnum.getMsgByCode(6));} catch (Exception e) {throw e;}}@Override@Transactional(rollbackFor = Exception.class)public Boolean cancelled(String orderId) {int update = orderInfoMapper.update(null, new UpdateWrapper<OrderInfo>().eq("order_id", orderId).set("order_status", 3));return update > 0;}@Overridepublic ResponseVO incomplete(IncompleteVO incompleteVO) {log.error("incomplete--------------------------------");try {//先处理未完成的订单String oldpaymentId = incompleteVO.getIdentifier();TransactionVO transaction = incompleteVO.getTransaction();log.error("?transation--------------------"+transaction);log.error("?oldpaymentId------------------"+oldpaymentId);if (null != transaction) {log.error("transation--------------------"+transaction);log.error("oldpaymentId------------------"+oldpaymentId);String txid = transaction.getTxid();String txURL = transaction.get_link();//                OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", oldpaymentId));
//                if (null == orderInfo) {
//                    log.error("order-----------------null");
//                    throw new RuntimeException("旧订单不存在");
//                }
//
//                if (orderInfo.getOrderStatus()==1) {
//                    log.error("orderStatus---------------------"+orderInfo.getOrderStatus());
//                    throw new RuntimeException("订单是已支付状态");
//                }String get = HttpClientUtil.sendGet(txURL);JSONObject jsonObject1 = JSON.parseObject(get);String piOrderId = jsonObject1.getString("memo");//我方订单IDlog.error("memo---------------------"+piOrderId);JSONObject jsonObject = new JSONObject();jsonObject.put("txid", txid);Map<String, String> heads = new HashMap<>();heads.put("Content-Type", "application/json;charset=UTF-8");heads.put("Authorization", "Key " + commonConfig.getServerAccessKey());try {HttpResponse response = HttpRequest.post("https://api.minepi.com/v2/payments/" + piOrderId + "/complete").headerMap(heads, false).body(String.valueOf(jsonObject)).timeout(5 * 60 * 1000).execute();String body = response.body();JSONObject jsonObject2 = JSON.parseObject(body);String error = jsonObject2.getString("error");if (!StringUtils.isEmpty(error)) {log.error("!response------------------"+error);throw new RuntimeException("订单完成异常!");}return new ResponseVO(PaymentEnum.getStatusByCode(7),7,PaymentEnum.getMsgByCode(7));} catch (Exception e) {throw e;}}} catch (Exception e) {return new ResponseVO(PaymentEnum.getStatusByCode(8),8,PaymentEnum.getMsgByCode(8));}return new ResponseVO(PaymentEnum.getStatusByCode(8),8,PaymentEnum.getMsgByCode(8));}

 前端代码

前端路由 

API
// 授权
import request from "@/api/http";
import axios from "axios";// 支付授权
export function payAuth(data){return request({url: "/api/order/payOrder/approve",method: "post",data,});
}//未完成订单
export function payIncomplete(data){return request({url: "/api/order/payOrder/incomplete",method: "post",data,});
}//支付成功
export function payDone(data){return request({url: "/api/order/payOrder/complete",method: "post",data,});
}
//支付取消
export function payCancel(data){return request({url: "/api/order/payOrder/cancelled",method: "post",data,})
}

支付前端代码

 test(){const pay_message = this.pay_messagelet orderid = ''console.log({amount: pay_message.amount,shopUserId: pay_message.shopUserId,shopId: pay_message.shopId,userId: pay_message.userId})Pi.createPayment({// Amount of π to be paid:amount: 3.14,// An explanation of the payment - will be shown to the user:memo: "购买特殊数据", // e.g: "Digital kitten #1234",// An arbitrary developer-provided metadata object - for your own usage:metadata: { productID : 'apple_pie_1'  }, // e.g: { kittenId: 1234 }}, {// Callbacks you need to implement - read more about those in the detailed docs linked below:// 授权async onReadyForServerApproval(paymentId) {console.log('paymentId',paymentId)payAuth({paymentId: paymentId,amount: pay_message.amount,shopUserId: pay_message.shopUserId,shopId: pay_message.shopId,userId: pay_message.userId}).then((data) => {orderid = data.dataconsole.log('orderId',orderid)}).catch(error => {console.log(error)})},//支付成功onReadyForServerCompletion: function(paymentId, txid) {alert(1111)console.log(paymentId, 'paymentId', 'txid', txid,'orderid',orderid )payDone({paymentId: paymentId, txId: txid, orderId: orderid,payType:'0'}).then(res => {console.log(res)// if (res && res.code === 0) {//   this.payDoneJump();// }})},//支付取消onCancel: function(orderid) {console.log('onCancel' + orderid)payCancel(orderid).then((data) => {console.log(data)})},//支付失败onError: function(error, payment) {console.log('error:',error);console.log('payment:',payment)}});},

登录自动调用未支付订单,这个十分重要因为会影响支付授权。 

const loginFun = () => {Pi.init({ version: "2.0", sandbox: true });const scopes = ["payments", "username", "wallet_address"];function onIncompletePaymentFound(payment) {alert(1111111)console.log("payment", payment);return payIncomplete({identifier:payment.identifier,transaction:{_link:payment.transaction._link,txid:res.transaction.txid}})}Pi.authenticate(scopes, onIncompletePaymentFound).then(function (auth) {console.log("auth", auth);let userInfo = {accessToken: auth.accessToken,userId: auth.user.uid,userName: auth.user.username,};// userGetPush().then((data) => {//   console.log(data);//   userStore().userGetPush = data.data;// });Login(userInfo).then((data) => {console.log(data);if (data.status == "success") {// 将用户信息存入piniauserStore().userInfoChange(data.data);// 发布消息到socket Login() 存入userId// this.$socket.emit("login", data.data.userInfo.userId);router.push("/home");}});}).catch(function (error) {console.error(error);});
};

详细流程

使用Pi-SDK功能发起支付

 由Pi SDK自动调用的回调函数,发出支付批准请求

 

 路由到后端的支付授权接口

 后端服务器向Pi服务器发起支付授权

 @Override@Transactional(rollbackFor = Exception.class)public String approve(PaymentVO paymentVO) {log.error("approve-------------------------------------------------------------");OrderInfo orderInfo;log.error("paymentVO----------------------------------"+paymentVO);//获取付款信息OkHttpClient client = new OkHttpClient();Request request = new Request.Builder().url("https://api.minepi.com/v2/payments/" + paymentVO.getPaymentId()).addHeader("Authorization", "Key " + commonConfig.getServerAccessKey()).build();try (Response response = client.newCall(request).execute()) {if (!response.isSuccessful()) {String string = response.body().string();JSONObject jsonObject1 = JSON.parseObject(string);log.error("!response-------------------------------------------------------------"+commonConfig.getServerAccessKey());throw new RuntimeException("payments error " + jsonObject1.getString("error_message"));}String string = response.body().string();log.error("response-------------------------------------------------------------"+string);JSONObject jsonObject1 = JSON.parseObject(string);//校验实际支付金额BigDecimal userFinalPrice = paymentVO.getAmount();if (userFinalPrice.compareTo(jsonObject1.getBigDecimal("amount")) < 0) {log.error(userFinalPrice+"response-------------------------------------------------------------"+jsonObject1.getBigDecimal("amount"));throw new RuntimeException("支付金额少于订单金额");}} catch (Exception e) {throw  new BusinessException("支付失败,请联系后台人员"+e.getLocalizedMessage()+e.toString()+e.getCause().toString());}OkHttpClient client1 = new OkHttpClient();//信息真实,通知PI我准备好了,可以付款了Request request1 = new Request.Builder().url("https://api.minepi.com/v2/payments/" + paymentVO.getPaymentId() + "/approve").addHeader("Content-Type", "application/json").addHeader("Access-Control-Allow-Origin", "*").addHeader("Authorization", "Key " + commonConfig.getServerAccessKey()).post(RequestBody.create("", MediaType.parse("application/json"))).build();try (Response response1 = client1.newCall(request1).execute()) {if (!response1.isSuccessful()) {throw new RuntimeException("approve error: ");}log.error("response1-------------------------------------------------------------");//更新支付报文
//                    tMerStoreGoodsOrderEntity.setPayOrderId(paymentDto.getPaymentId());
//                    tMerStoreGoodsOrderEntity.setPayStatusType("10007002");//支付中
//                    itMerStoreGoodsOrderService.updateById(tMerStoreGoodsOrderEntity);log.error("return-------------------------------------------------------------");} catch (RuntimeException | IOException e) {log.error("error-------------------------------------------------------------");e.printStackTrace();}// 生成订单orderInfo = new OrderInfo();orderInfo.setOrderId(StringUtil.generateShortId());orderInfo.setShopId(paymentVO.getShopId());orderInfo.setUserId(paymentVO.getUserId());orderInfo.setShopUserId(paymentVO.getShopUserId());orderInfo.setAmount(paymentVO.getAmount());orderInfoMapper.insert(orderInfo);log.error("生成订单-------------------------------------------------------------");return orderInfo.getOrderId();}

PI游览器向用户显示付款详细信息页面,我们等待用户签署交易

 由Pi SDK自动调用完成的回调函数

 从你的后端服务器到Pi服务器的API请求以完成付款(让pi服务器知道你完成此付款)

 /*** 前端支付完成,余额支付直接调用此方法*/@Override@Transactional(rollbackFor = Exception.class)public ResponseVO complete(CompleteVO completeVO) {String payType = completeVO.getPayType();log.error("complete------------------------------------------------------------"+completeVO);if ("1".equals(payType)) {//余额支付String orderId = completeVO.getOrderId();String lockName = "access:lock:complete:" + orderId;String lockId = UUID.randomUUID();if (!redisLockUtil.tryLock(lockName, lockId, 20L)) {// 调用太快return new ResponseVO(PaymentEnum.getStatusByCode(4),4,PaymentEnum.getMsgByCode(4));}// 获取订单信息OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", orderId));if ((orderInfo.getOrderStatus() != 0)) {// 订单不是待支付状态return new ResponseVO(PaymentEnum.getStatusByCode(2),2,PaymentEnum.getMsgByCode(2));}String userId = orderInfo.getUserId();AccountInfo accountInfo = accountInfoMapper.selectOne(new QueryWrapper<AccountInfo>().eq("user_id", userId));BigDecimal balance = accountInfo.getPiBalance();if (balance.compareTo(orderInfo.getAmount()) < 0) {// 余额不足,请前往充值return new ResponseVO(PaymentEnum.getStatusByCode(5),5,PaymentEnum.getMsgByCode(5));}int update = orderInfoMapper.update(null,new UpdateWrapper<OrderInfo>().eq("order_id",orderId).set("order_status",1));balance=balance.subtract(orderInfo.getAmount());int update1 = accountInfoMapper.update(null, new UpdateWrapper<AccountInfo>().eq("user_id", userId).set("pi_balance", balance));// 支付成功return new ResponseVO(PaymentEnum.getStatusByCode(6),6,PaymentEnum.getMsgByCode(6));}//PI钱包支付String paymentId = completeVO.getPaymentId();//PI订单号String lockName = "access:lock:complete:" + paymentId;String lockId = UUID.randomUUID();log.error(paymentId+"-----------------"+lockName+"---------------------"+lockId);if (!redisLockUtil.tryLock(lockName, lockId, 20L)) {// 调用太快log.error("!RedisLockUtil---------------------------------------------------------调用太快");return new ResponseVO(PaymentEnum.getStatusByCode(4),4,PaymentEnum.getMsgByCode(4));}OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", completeVO.getOrderId()));log.error("orderId--------------------------------------------------------------"+orderInfo);if (null == orderInfo) {// 订单不存在log.error("!orderinfo--------------------------------------------------------不存在");return new ResponseVO(PaymentEnum.getStatusByCode(1),1,PaymentEnum.getMsgByCode(1));}log.error("orderinfo------------------------------------------------------------------"+orderInfo);if (orderInfo.getOrderStatus() != 0) {// 订单不是待支付状态log.error("!order---------------------------------------------------------pay");return  new ResponseVO(PaymentEnum.getStatusByCode(2),2,PaymentEnum.getMsgByCode(2));}//通知PI完成交易JSONObject jsonObject = new JSONObject();jsonObject.put("txid", completeVO.getTxId());Map<String, String> heads = new HashMap<>();heads.put("Content-Type", "application/json;charset=UTF-8");heads.put("Authorization", "Key " + commonConfig.getServerAccessKey());log.error("pi-----------------------------------------"+jsonObject);try {HttpResponse response = HttpRequest.post("https://api.minepi.com/v2/payments/" + paymentId + "/complete").headerMap(heads, false).body(String.valueOf(jsonObject)).timeout(5 * 60 * 1000).execute();String body = response.body();JSONObject jsonObject1 = JSON.parseObject(body);String error = jsonObject1.getString("error");if (!StringUtils.isEmpty(error)) {log.error("!strinutils-----------------------------"+body);throw new RuntimeException("订单完成异常!");}orderInfo.setOrderStatus(1);// 更新订单orderInfoMapper.updateById(orderInfo);log.error("支付成功------------------------------------------------------");// 支付成功return  new ResponseVO(PaymentEnum.getStatusByCode(6),6,PaymentEnum.getMsgByCode(6));} catch (Exception e) {throw e;}}

注意,如果用户有未处理的订单,会导致用户重新创建支付失败,需要有个接口去处理未完成的订单

前端一初始化就去执行处理未完成的订单

路由到后端的接口 

 

 后端接口代码

@Overridepublic ResponseVO incomplete(IncompleteVO incompleteVO) {log.error("incomplete--------------------------------");try {//先处理未完成的订单String oldpaymentId = incompleteVO.getIdentifier();TransactionVO transaction = incompleteVO.getTransaction();log.error("?transation--------------------"+transaction);log.error("?oldpaymentId------------------"+oldpaymentId);if (null != transaction) {log.error("transation--------------------"+transaction);log.error("oldpaymentId------------------"+oldpaymentId);String txid = transaction.getTxid();String txURL = transaction.get_link();//                OrderInfo orderInfo = orderInfoMapper.selectOne(new QueryWrapper<OrderInfo>().eq("order_id", oldpaymentId));
//                if (null == orderInfo) {
//                    log.error("order-----------------null");
//                    throw new RuntimeException("旧订单不存在");
//                }
//
//                if (orderInfo.getOrderStatus()==1) {
//                    log.error("orderStatus---------------------"+orderInfo.getOrderStatus());
//                    throw new RuntimeException("订单是已支付状态");
//                }String get = HttpClientUtil.sendGet(txURL);JSONObject jsonObject1 = JSON.parseObject(get);String piOrderId = jsonObject1.getString("memo");//我方订单IDlog.error("memo---------------------"+piOrderId);JSONObject jsonObject = new JSONObject();jsonObject.put("txid", txid);Map<String, String> heads = new HashMap<>();heads.put("Content-Type", "application/json;charset=UTF-8");heads.put("Authorization", "Key " + commonConfig.getServerAccessKey());try {HttpResponse response = HttpRequest.post("https://api.minepi.com/v2/payments/" + piOrderId + "/complete").headerMap(heads, false).body(String.valueOf(jsonObject)).timeout(5 * 60 * 1000).execute();String body = response.body();JSONObject jsonObject2 = JSON.parseObject(body);String error = jsonObject2.getString("error");if (!StringUtils.isEmpty(error)) {log.error("!response------------------"+error);throw new RuntimeException("订单完成异常!");}return new ResponseVO(PaymentEnum.getStatusByCode(7),7,PaymentEnum.getMsgByCode(7));} catch (Exception e) {throw e;}}} catch (Exception e) {return new ResponseVO(PaymentEnum.getStatusByCode(8),8,PaymentEnum.getMsgByCode(8));}return new ResponseVO(PaymentEnum.getStatusByCode(8),8,PaymentEnum.getMsgByCode(8));}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/153025.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Java 进阶篇】CSS语法格式详解

在前端开发中&#xff0c;CSS&#xff08;层叠样式表&#xff09;用于控制网页的样式和布局。了解CSS的语法格式是学习如何设计和美化网页的关键。本文将深入解释CSS的语法格式&#xff0c;包括选择器、属性和值等基本概念&#xff0c;同时提供示例代码以帮助初学者更好地理解。…

微信小程序点单左右联动的效果实现

微信小程序点单左右联动的效果实现 原理解析&#xff1a;   点击左边标签会跳到右边相应位置&#xff1a;点击改变rightCur值&#xff0c;转跳相应位置滑动右边&#xff0c;左边标签会跳到相应的位置&#xff1a;监听并且设置每个右边元素的top和bottom&#xff0c;再判断当…

【Amazon】基于AWS云实例(CentOS 7.9系统)使用kubeadm方式搭建部署Kubernetes集群1.25.4版本

文章目录 前言实验架构介绍K8S集群部署方式说明使用CloudFormation部署EC2实例集群环境准备修改主机名并配置域名解析&#xff08;ALL节点&#xff09;禁用防火墙禁用SELinux加载br_netfilter模块安装ipvs安装 ipset 软件包同步服务器时间关闭swap分区安装Containerd 初始化集群…

Linux: alsa-lib 插件简介

文章目录 1. 前言2. 分析背景3. Linux ALSA 框架4. alsa 声卡设备5. alsa-lib 简介5.1 alsa-lib 插件5.1.1 alsa-lib 插件概览5.1.2 alsa-lib 插件工作细节5.1.2.1 插件对象的创建和初始化5.1.2.2 插件对象处理数据的过程 5.1.3 alsa-lib 内置插件代码组织5.1.4 自定义 alsa-li…

js中的原型链

编写思路&#xff1a; 简单介绍构造函数介绍原型对象原型对象、实例的关系&#xff0c;从而引出原型链的基本概念 原型链基本思想是利用原型让一个引用类型继承另一个引用类型的属性和方法。 1. 什么是构造函数 构造函数本身跟普通函数一样&#xff0c;也不存在定义构造函数…

图神经网络 GNN

之前经常看到图神经网络的内容&#xff0c;但是一直都觉得很难&#xff0c;就没有继续了解&#xff0c;现在抽空学习了一下&#xff0c;简单了解GNN是个什么东西&#xff0c;还没有进行代码实践&#xff0c;随着后续的学习&#xff0c;会继续更新代码的内容&#xff0c;这里先记…

Linux动态链接库.so文件

一、动态库和静态库的区别 库是一个二进制文件&#xff0c;包含的代码可以被程序调用&#xff0c;如标准库、线程库。Windows 和 Linux下的库文件格式不兼容。 Windows环境&#xff1a;静态库是 .lib 文件&#xff0c;共享库是 .dll 文件 Linux环境&#xff1a;静态库是 .a 文…

数据结构与算法(八):排序算法

参考引用 Hello 算法 Github&#xff1a;hello-algo 1. 选择排序 选择排序的工作原理非常直接&#xff1a;开启一个循环&#xff0c;每轮从未排序区间选择最小的元素&#xff0c;将其放到已排序区间的末尾&#xff0c;设数组的长度为 n 初始状态下&#xff0c;所有元素未排序&…

HTTP协议的请求协议和响应协议的组成,HTTP常见的状态信息

HTTP协议 什么是协议 协议实际上是某些人或组织提前制定好的一套规范,大家只要都按照这个规范来就可以做到沟通无障碍 HTTP协议是W3C(万维网联盟组织)制定的一种超文本传输通信协议(发送消息的模板和数据的格式),除了传送字符串,还有声音、视频、图片等流媒体等超文本信息 …

伦敦银最新走势不利怎么办

跟其他的投资品种一样&#xff0c;伦敦银的价格走势在不停的变化&#xff0c;而且由于本身产品具有较高的资金杠杆&#xff0c;所以万一行情走势变得不利&#xff0c;在很短的时间之内就会对投资者的账户造成严重损失&#xff0c;所以投资者应该对此作好充分的准备。 伦敦银的最…

LabVIEW利用以太网开发智能液位检测仪

LabVIEW利用以太网开发智能液位检测仪 目前&#xff0c;工业以太网接口在国内外的发展已经达到了相当深入的程度&#xff0c;特别是在自动化控制和工业控制领域有着非常广泛的应用。在工业生产过程中&#xff0c;钢厂的连铸机是前后的连接环节&#xff0c;其中钢水从大钢包进入…

Spring Boot如何配置CORS支持

Spring Boot如何配置CORS支持 CORS&#xff08;跨源资源共享&#xff09;是一种Web浏览器的安全性功能&#xff0c;用于控制网页上的脚本文件从不同的源加载其他网页资源。在开发现代Web应用程序时&#xff0c;通常需要跨域请求不同的资源&#xff0c;如API服务或其他Web应用程…

一个tomcat下如何部署多个项目?

1、不修改端口&#xff0c;部署多个项目 清楚tomcat目录结构的应该都知道&#xff0c;项目包是放在webapps目录下的&#xff0c;那能否在同一个tomcat的webapps目录下运行多个不同项目呢&#xff1f; 答案是可以的。 1、将多个项目包放入webapps文件夹下 2、修改conf下的serv…

reactjs开发环境搭建

Reactjs是一个前端web页面应用开发框架工具集&#xff0c;其支持前端构建页面以及后端构建页面两种常用的开发场景&#xff0c;其中&#xff0c;支持reactjs的开发框架包括next.js、remix、gatsby以及其他&#xff0c;本文主要描述next.js开发环境的搭建&#xff0c;next.js是一…

Verilog HDL阻塞赋值和非阻塞赋值笔记

1. module test( input wire clk, input wire b, output reg a, output reg c ); always(posedge clk) begin ab; ca; end endmodule 上面的代码在vivado中综合后的电路为&#xff1a; 2. module test( input wire clk, input wire b, outp…

springcloud之项目实战环境准备

写在前面 为了更好的学习springcloud&#xff0c;我们来一起开发一个实战项目&#xff0c;加深理解。 1&#xff1a;项目介绍 在开始项目实战之前先来做一个整体的项目介绍&#xff0c;从而能够让对项目的整体架构和模板有一个比较清晰的认知。 大家都知道双11&#xff0c;…

JS进阶-原型

原型 原型就是一个对象&#xff0c;也称为原型对象 构造函数通过原型分配的函数是所有对象所共享的 JavaScript规定&#xff0c;每一个构造函数都有一个prototype属性&#xff0c;指向另一个对象&#xff0c;所以我们也称为原型对象 这个对象可以挂载函数&#xff0c;对象实…

64.最小路径和

法&#xff1a;动态规划 第一行的元素&#xff0c;只有通过左侧右移才能到达&#xff1b;第一列的元素&#xff0c;只有通过上方的下移才能到达。其他位置元素&#xff1a;比较从上方元素向下移动的路径和和左侧元素向右移动的路径和的较小值dp[i][j]&#xff1a;到达(i,j)位置…

中国移动咪咕、阿里云、华为“秀肌肉”,这届亚运会的“高光”不止比赛

文 | 智能相对论 作者 | 青月 竞技体育的发展&#xff0c;其实也可以看作是一部“技术进化史”。 在1924年的巴黎&#xff0c;广播首次进入奥运会&#xff0c;人们第一次可以通过报纸以外的方式了解奥运会。 1928年&#xff0c;在荷兰申办的阿姆斯特丹奥运会&#xff0c;高…

【jvm--方法区】

文章目录 1. 栈、堆、方法区的交互关系2. 方法区的内部结构3. 运行时常量池4. 方法区的演进细节5. 方法区的垃圾回收 1. 栈、堆、方法区的交互关系 方法区的基本理解&#xff1a; 方法区&#xff08;Method Area&#xff09;与 Java 堆一样&#xff0c;是各个线程共享的内存区…