@@ -5,7 +5,6 @@ package com.egzosn.pay.demo.controller;
import com.alibaba.fastjson.JSONObject ;
import com.egzosn.pay.ali.api.AliPayService ;
import com.egzosn.pay.ali.bean.AliTransactionType ;
import com.egzosn.pay.common.api.Callback ;
import com.egzosn.pay.common.api.PayConfigStorage ;
import com.egzosn.pay.common.api.PayMessageInterceptor ;
import com.egzosn.pay.common.api.PayService ;
@@ -18,16 +17,11 @@ import com.egzosn.pay.demo.entity.PayType;
import com.egzosn.pay.demo.request.QueryOrder ;
import com.egzosn.pay.demo.service.ApyAccountService ;
import com.egzosn.pay.demo.service.PayResponse ;
import com.egzosn.pay.payoneer.api.PayoneerPayService ;
import com.egzosn.pay.wx.bean.WxTransactionType ;
import com.fasterxml.jackson.databind.ObjectMapper ;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder ;
import org.springframework.web.bind.annotation.PathVariable ;
import org.springframework.web.bind.annotation.RequestMapping ;
import org.springframework.web.bind.annotation.RestController ;
import org.springframework.web.servlet.ModelAndView ;
import org.springframework.web.servlet.view.RedirectView ;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView ;
import javax.annotation.Resource ;
import javax.imageio.ImageIO ;
@@ -35,9 +29,7 @@ import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream ;
import java.io.IOException ;
import java.math.BigDecimal ;
import java.net.URLEncoder ;
import java.util.HashMap ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import java.util.UUID ;
@@ -58,13 +50,11 @@ public class PayController {
private ApyAccountService service ;
@RequestMapping ( " / " )
public ModelAndView index ( ) {
public ModelAndView index ( ) {
return new ModelAndView ( " /index.html " ) ;
}
/**
* 这里模拟账户信息增加
*
@@ -89,11 +79,11 @@ public class PayController {
* @param payId 账户id
* @param transactionType 交易类型, 这个针对于每一个 支付类型的对应的几种交易方式
* @param bankType 针对刷卡支付,卡的类型,类型值
* @param price 金额
* @param price 金额
* @return 跳到支付页面
*/
@RequestMapping ( value = " toPay.html " , produces = " text/html;charset=UTF-8 " )
public String toPay ( HttpServletRequest request , Integer payId , String transactionType , String bankType , BigDecimal price ) {
public String toPay ( HttpServletRequest request , Integer payId , String transactionType , String bankType , BigDecimal price ) {
//获取对应的支付账户操作工具( 可根据账户id)
PayResponse payResponse = service . getPayResponse ( payId ) ;
@@ -102,7 +92,7 @@ public class PayController {
order . setSpbillCreateIp ( request . getHeader ( " X-Real-IP " ) ) ;
StringBuffer requestURL = request . getRequestURL ( ) ;
//设置网页地址
order . setWapUrl ( requestURL . substring ( 0 , requestURL . indexOf ( " / " ) > 0 ? requestURL . indexOf ( " / " ) : requestURL . length ( ) ) ) ;
order . setWapUrl ( requestURL . substring ( 0 , requestURL . indexOf ( " / " ) > 0 ? requestURL . indexOf ( " / " ) : requestURL . length ( ) ) ) ;
//设置网页名称
order . setWapName ( " 在线充值 " ) ;
// ------ 微信H5使用----
@@ -124,6 +114,7 @@ public class PayController {
/**
* 跳到支付页面
* 针对实时支付,即时付款
*
* @return 跳到支付页面
*/
@RequestMapping ( value = " toWxPay.html " , produces = " text/html;charset=UTF-8 " )
@@ -131,29 +122,29 @@ public class PayController {
//获取对应的支付账户操作工具( 可根据账户id)
PayResponse payResponse = service . getPayResponse ( 2 ) ;
PayOrder order = new PayOrder ( " 订单title " , " 摘要 " , new BigDecimal ( 0 . 01 ) , UUID . randomUUID ( ) . toString ( ) . replace ( " - " , " " ) , WxTransactionType . MWEB ) ;
PayOrder order = new PayOrder ( " 订单title " , " 摘要 " , new BigDecimal ( 0 . 01 ) , UUID . randomUUID ( ) . toString ( ) . replace ( " - " , " " ) , WxTransactionType . MWEB ) ;
order . setSpbillCreateIp ( request . getHeader ( " X-Real-IP " ) ) ;
StringBuffer requestURL = request . getRequestURL ( ) ;
//设置网页地址
order . setWapUrl ( requestURL . substring ( 0 , requestURL . indexOf ( " / " ) > 0 ? requestURL . indexOf ( " / " ) : requestURL . length ( ) ) ) ;
order . setWapUrl ( requestURL . substring ( 0 , requestURL . indexOf ( " / " ) > 0 ? requestURL . indexOf ( " / " ) : requestURL . length ( ) ) ) ;
//设置网页名称
order . setWapName ( " 在线充值 " ) ;
Map orderInfo = payResponse. getService( ) . orderInfo ( order ) ;
return payResponse. getService( ) . buildRequest( orderInfo, MethodType. POST ) ;
// Map orderInfo = payResponse. getService().orderInfo(order) ;
// return payResponse. getService(). buildRequest( orderInfo, MethodType.POST) ;
return payResponse . getService ( ) . toPay ( order ) ;
}
/**
* 公众号支付
*
*
* @param payId 账户id
* @param payId 账户id
* @param openid openid
* @param price 金额
* @param price 金额
* @return 返回jsapi所需参数
*/
@RequestMapping ( value = " jsapi " )
@RequestMapping ( value = " jsapi " )
public Map toPay ( Integer payId , String openid , BigDecimal price ) {
//获取对应的支付账户操作工具( 可根据账户id)
PayResponse payResponse = service . getPayResponse ( payId ) ;
@@ -164,16 +155,17 @@ public class PayController {
Map orderInfo = payResponse . getService ( ) . orderInfo ( order ) ;
orderInfo . put ( " code " , 0 ) ;
return orderInfo ;
return orderInfo ;
}
/**
* 刷卡付,pos主动扫码付款(条码付)
*
* @param payId 账户id
* @param transactionType 交易类型, 这个针对于每一个 支付类型的对应的几种交易方式
* @param authCode 授权码,条码等
* @param price 金额
* @param price 金额
* @return 支付结果
*/
@RequestMapping ( value = " microPay " )
@@ -200,9 +192,10 @@ public class PayController {
/**
* 获取二维码图像
* 二维码支付
*
* @param payId 账户id
* @param transactionType 交易类型, 这个针对于每一个 支付类型的对应的几种交易方式
* @param price 金额
* @param price 金额
* @return 二维码图像
*/
@RequestMapping ( value = " toQrPay.jpg " , produces = " image/jpeg;charset=UTF-8 " )
@@ -211,30 +204,31 @@ public class PayController {
PayResponse payResponse = service . getPayResponse ( payId ) ;
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
ImageIO . write ( payResponse . getService ( ) . genQrPay ( new PayOrder ( " 订单title " , " 摘要 " , null = = price ? new BigDecimal ( 0 . 01 ) : price , System . currentTimeMillis ( ) + " " , PayType . valueOf ( payResponse . getStorage ( ) . getPayType ( ) ) . getTransactionType ( transactionType ) ) ) , " JPEG " , baos ) ;
ImageIO . write ( payResponse . getService ( ) . genQrPay ( new PayOrder ( " 订单title " , " 摘要 " , null = = price ? new BigDecimal ( 0 . 01 ) : price , System . currentTimeMillis ( ) + " " , PayType . valueOf ( payResponse . getStorage ( ) . getPayType ( ) ) . getTransactionType ( transactionType ) ) ) , " JPEG " , baos ) ;
return baos . toByteArray ( ) ;
}
/**
* 获取一码付二维码图像
* 二维码支付
* @param wxPayId 微信账户id
* @param ali PayId 支付宝 id
* @param price 金额
*
* @param wx PayId 微信账户 id
* @param aliPayId 支付宝id
* @param price 金额
* @return 二维码图像
*/
@RequestMapping ( value = " toWxAliQrPay.jpg " , produces = " image/jpeg;charset=UTF-8 " )
public byte [ ] toWxAliQrPay ( Integer wxPayId , Integer aliPayId , BigDecimal price , HttpServletRequest request ) throws IOException {
public byte [ ] toWxAliQrPay ( Integer wxPayId , Integer aliPayId , BigDecimal price , HttpServletRequest request ) throws IOException {
//获取对应的支付账户操作工具( 可根据账户id)
ByteArrayOutputStream baos = new ByteArrayOutputStream ( ) ;
//这里为需要生成二维码的地址
StringBuffer url = request . getRequestURL ( ) ;
url = new StringBuffer ( url . substring ( 0 , url . lastIndexOf ( request . getRequestURI ( ) ) ) ) ;
url . append ( " /toWxAliPay.html? " ) ;
if ( null ! = wxPayId ) {
url . append ( " /toWxAliPay.html? " ) ;
if ( null ! = wxPayId ) {
url . append ( " wxPayId= " ) . append ( wxPayId ) . append ( " & " ) ;
}
if ( null ! = aliPayId ) {
if ( null ! = aliPayId ) {
url . append ( " aliPayId= " ) . append ( aliPayId ) . append ( " & " ) ;
}
url . append ( " price= " ) . append ( price ) ;
@@ -244,40 +238,39 @@ public class PayController {
}
/**
*
* 支付宝与微信平台的判断 并进行支付的转跳
* @param wxPayId 微信账户id
* @param ali PayId 支付宝 id
* @param price 金额
*
* @param wx PayId 微信账户 id
* @param aliPayId 支付宝id
* @param price 金额
* @return 支付宝与微信平台的判断
*/
@RequestMapping ( value = " toWxAliPay.html " , produces = " text/html;charset=UTF-8 " )
public String toWxAliPay ( Integer wxPayId , Integer aliPayId , BigDecimal price , HttpServletRequest request ) throws IOException {
public String toWxAliPay ( Integer wxPayId , Integer aliPayId , BigDecimal price , HttpServletRequest request ) throws IOException {
StringBuilder html = new StringBuilder ( ) ;
//订单
PayOrder payOrder = new PayOrder ( " 订单title " , " 摘要 " , null = = price ? new BigDecimal ( 0 . 01 ) : price , System . currentTimeMillis ( ) + " " ) ;
String ua = request . getHeader ( " user-agent " ) ;
if ( ua . contains ( " MicroMessenger " ) ) {
if ( ua . contains ( " MicroMessenger " ) ) {
payOrder . setTransactionType ( WxTransactionType . NATIVE ) ;
PayService service = this . service . getPayResponse ( wxPayId ) . getService ( ) ;
return String . format ( " <script type= \" text/javascript \" >location.href= \" %s \" </script> " , ( String ) service . orderInfo ( payOrder ) . get ( " code_url " ) ) ;
return String . format ( " <script type= \" text/javascript \" >location.href= \" %s \" </script> " , ( String ) service . orderInfo ( payOrder ) . get ( " code_url " ) ) ;
}
if ( ua . contains ( " AlipayClient " ) ) {
if ( ua . contains ( " AlipayClient " ) ) {
payOrder . setTransactionType ( AliTransactionType . SWEEPPAY ) ;
AliPayService service = ( AliPayService ) this . service . getPayResponse ( aliPayId ) . getService ( ) ;
AliPayService service = ( AliPayService ) this . service . getPayResponse ( aliPayId ) . getService ( ) ;
JSONObject result = service . getHttpRequestTemplate ( ) . postForObject ( service . getReqUrl ( ) + " ? " + UriVariables . getMapToParameters ( service . orderInfo ( payOrder ) ) , null , JSONObject . class ) ;
result = result . getJSONObject ( " alipay_trade_precreate_response " ) ;
result = result . getJSONObject ( " alipay_trade_precreate_response " ) ;
return String . format ( " <script type= \" text/javascript \" >location.href= \" %s \" </script> " , result . getString ( " qr_code " ) ) ;
}
return String . format ( " <script type= \" text/javascript \" >alert( \" 请使用微信或者支付宝App扫码%s \" );window.close();</script> " , ua ) ;
return String . format ( " <script type= \" text/javascript \" >alert( \" 请使用微信或者支付宝App扫码%s \" );window.close();</script> " , ua ) ;
}
/**
* 获取支付预订单信息
*
@@ -299,15 +292,12 @@ public class PayController {
/**
* 支付回调地址 方式一
*
* <p>
* 方式二,{@link #payBack(HttpServletRequest, Integer)} 是属于简化方式, 试用与简单的业务场景
*
*
* @param request
* @param payId
* @return 支付是否成功
*
*
*/
@RequestMapping ( value = " payBackOne{payId}.json " )
public String payBackOne ( HttpServletRequest request , @PathVariable Integer payId ) throws IOException {
@@ -333,22 +323,16 @@ public class PayController {
}
/**
* 支付回调地址
* 方式二
*
* @param request
*
* @return
*
* 拦截器相关增加, 详情查看{@link com.egzosn.pay.common.api.PayService#addPayMessageInterceptor(PayMessageInterceptor)}
* @return 拦截器相关增加, 详情查看{@link com.egzosn.pay.common.api.PayService#addPayMessageInterceptor(PayMessageInterceptor)}
* <p></p>
* 业务处理在对应的PayMessageHandler里面处理, 在哪里设置PayMessageHandler, 详情查看{@link com.egzosn.pay.common.api.PayService#setPayMessageHandler(com.egzosn.pay.common.api.PayMessageHandler)}
*
* <p>
* 如果未设置 {@link com.egzosn.pay.common.api.PayMessageHandler} 那么会使用默认的 {@link com.egzosn.pay.common.api.DefaultPayMessageHandler}
*
*/
@RequestMapping ( value = " payBack{payId}.json " )
public String payBack ( HttpServletRequest request , @PathVariable Integer payId ) throws IOException {
@@ -402,7 +386,7 @@ public class PayController {
* @return 返回支付方申请退款后的结果
*/
@RequestMapping ( " refund " )
public Map < String , Object > refund ( Integer payId , RefundOrder order ) {
public Map < String , Object > refund ( Integer payId , RefundOrder order ) {
PayResponse payResponse = service . getPayResponse ( payId ) ;
// return payResponse.getService().refund(order.getTradeNo(), order.getOutTradeNo(), order.getRefundAmount(), order.getTotalAmount());
@@ -453,7 +437,6 @@ public class PayController {
* 转账
*
* @param order 转账订单
*
* @return 对应的转账结果
*/
@RequestMapping ( " transfer " )
@@ -467,7 +450,6 @@ public class PayController {
*
* @param outNo 商户转账订单号
* @param tradeNo 支付平台转账订单号
*
* @return 对应的转账订单
*/
@RequestMapping ( " transferQuery " )