生活随笔
收集整理的這篇文章主要介紹了
支付宝接口的调用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
支付流程圖
對(duì)接支付寶的準(zhǔn)備工作
一、申請(qǐng)條件
1.企業(yè)或個(gè)體工商戶可申請(qǐng);
2. 提供真實(shí)有效的營(yíng)業(yè)執(zhí)照,且支付寶賬戶名稱需與營(yíng)業(yè)執(zhí)照主體一致;
3. 網(wǎng)站能正常訪問且頁(yè)面信息有完整商品內(nèi)容;
4. 網(wǎng)站必須通過ICP備案,個(gè)體戶備案需與賬戶主體一致。
(團(tuán)購(gòu)類網(wǎng)站不支持個(gè)體工商戶簽約)
alipay.properties
#支付寶的接口地址
alipay_url
=https
://openapi
.alipay
.com
/gateway
.do
#企業(yè)與支付寶合作伙伴Id
app_id
=2018020102122556
#企業(yè)自己的私鑰
app_private_key
=MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCdQeknhM2rhiGAH6V0ljxn3rAWIdzduTEQuteTfwjnZtvMhQPuuN1b
/88D5yMuaZhZNFeUdWb
+SmtP9DAzAWWgnT13T0YhJcxP6txm7JBRrjadCRt
+LOFxPiPQk5t9fH7yXjw9i4uMDsNJeTncrVZ
/AZYrk0ESC9anJR8XeuBc3HE8T4fqlKKl35jlumIWrPbPNQhKGXaGcOnpiaXO9qYYUSP
/tnrjNYXHOso0yBs4YTl
+LLX2TJ12p3n
/oX6HnL4zQgtN5k4QasHP7CIig1ngcVQGfWsMm4djI9KXNXvGLQPfMQEmyb71mM5OCdl1MtAc6OaIAymhSv2hOLNIuyodAgMBAAECggEAe05
/P5mGm4QlKI2n8u8KlneqovASe1kG
/BNFjkYB
+VBR8OAr4TfbepPvAyRuFap
+5xN
/yMz14VcBJkRWtufVhEdHNxJV7w
/wUIncIGhGEYYFFMVbZWhTrbQH6TiUp6TC9dCmc6vD1CKPRkFj
+YGBXT0lPy3LzBa0TYNyCbszyhthrgkpuFYbB0R93IPvvBh5NJFXQytwNb2oVopC9AQWviqnZUZcT0eJ087dQ1WLPa6blBD8DP1PUq0Ldr6pgKfObFxIj8
+87DlJznRfdEsbqZlS7jagdw5tLr71WJpctIGPqKpgvajfePP
/lj3eY82BKQB
+aTw0zmAiB05Yes4LgQKBgQDq3EiQR8J1MEN2rpiLt1WvDYYvKVUgOY7Od
#支付寶公鑰
alipay_public_key
=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhkZi6W0wn
/prX
+NIIF9ATb5Z8ReKK4hFYtBrweDfGHD1mNW7YIZY4G5hE7S2Sry8eFXlFgSlBWlJ4fVnDaK9MkVThpwE2H65ooVlK
/wLuyPqovIVpMt
/utva5Ayuzv7eQOWK45FdLDNDlK8QLoBko6SS
+YbnWnf7a
+mrf4NAS4UFClpfe8Byqe8XIraO2Cg4Ko5Y5schX39rOAH8GlLdgqQRYVQ2dCnkIQ
+L
+I4Cy9Mvw3rIkTwt3MBU
+AqREXY4r5Bn6cmmX
/9MAJbFqrofGiUAqG
+qbjTcZAzgNPfuiD0zXgt
/YYjMQMzck75BOmwnYOam2ajODUSQn8Xybsa7wQIDAQAB# 同步回調(diào)地址 重定向地址本地瀏覽器
return_payment_url
=http
://payment
.gmall
.com
:8087/alipay
/callback
/return# 異步通知地址 公網(wǎng)接口
(webService
)
notify_payment_url
=http
://60.205.215.91/alipay
/callback
/notifyreturn_order_url
=http
://order
.gmall
.com
:8086/orderList
類AlipayConfig整合到項(xiàng)目
@Configuration
@PropertySource("classpath:alipay.properties")
public class AlipayConfig {@Value("${alipay_url}")private String alipay_url
;@Value("${app_private_key}")private String app_private_key
;@Value("${app_id}")private String app_id
;public final static String format
="json";public final static String charset
="utf-8";public final static String sign_type
="RSA2";public static String return_payment_url
;public static String notify_payment_url
;public static String return_order_url
;public static String alipay_public_key
;@Value("${alipay_public_key}")public void setAlipay_public_key(String alipay_public_key
) {AlipayConfig
.alipay_public_key
= alipay_public_key
;}@Value("${return_payment_url}")public void setReturn_url(String return_payment_url
) {AlipayConfig
.return_payment_url
= return_payment_url
;}@Value("${notify_payment_url}")public void setNotify_url(String notify_payment_url
) {AlipayConfig
.notify_payment_url
= notify_payment_url
;}@Value("${return_order_url}")public void setReturn_order_url(String return_order_url
) {AlipayConfig
.return_order_url
= return_order_url
;}@Beanpublic AlipayClient
alipayClient(){AlipayClient alipayClient
=new DefaultAlipayClient(alipay_url
,app_id
,app_private_key
,format
,charset
, alipay_public_key
,sign_type
);return alipayClient
;}
PaymentMapper.java
public interface PaymentMapper extends Mapper<PaymentInfo>{
}
PaymentServiceImpl.java
@Service
public class PaymentServiceImpl implements PaymentService{@AutowiredPaymentMapper paymentMapper
;@AutowiredActiveMQUtil activeMQUtil
;@AutowiredAlipayClient alipayClient
;@Overridepublic void savePaymentInfo(PaymentInfo paymentInfo
) {paymentMapper
.insert(paymentInfo
);}@Overridepublic void updatePayment(PaymentInfo paymentInfo
) {PaymentInfo paymentInfoParam
= new PaymentInfo();paymentInfoParam
.setOrderSn(paymentInfo
.getOrderSn());PaymentInfo paymentInfoResult
=paymentMapper
.selectOne(paymentInfoParam
);if(StringUtils
.isNotBlank(paymentInfoResult
.getPaymentStatus())&&paymentInfoResult
.getPaymentStatus().equals("已支付")){return;}else{String orderSn
= paymentInfo
.getOrderSn();Example example
=new Example(PaymentInfo
.class);example
.createCriteria().andEqualTo("orderSn",orderSn
);ConnectionFactory connectionFactory
= activeMQUtil
.getConnectionFactory();Session session
=null
;Connection connection
=null
;try {connection
= connectionFactory
.createConnection();session
= connection
.createSession(true, Session
.SESSION_TRANSACTED
);} catch (JMSException e
) {e
.printStackTrace();}try{paymentMapper
.updateByExampleSelective(paymentInfo
,example
);Queue payment_success_queue
= session
.createQueue("PAYMENT_SUCCESS_QUEUE");MessageProducer producer
= session
.createProducer(payment_success_queue
);MapMessage mapMessage
=new ActiveMQMapMessage();mapMessage
.setString("out_trade_no",paymentInfo
.getOrderSn());producer
.send(mapMessage
);session
.commit();}catch (Exception e
){try {session
.rollback();} catch (JMSException e1
) {e1
.printStackTrace();}}finally {try {connection
.close();} catch (JMSException e
) {e
.printStackTrace();}}}}@Overridepublic void sendDelayPaymentResultCheckQueue(String outTrade
,int count
) {ConnectionFactory connectionFactory
= activeMQUtil
.getConnectionFactory();Session session
=null
;Connection connection
=null
;try {connection
= connectionFactory
.createConnection();session
= connection
.createSession(true, Session
.SESSION_TRANSACTED
);} catch (JMSException e
) {e
.printStackTrace();}try{Queue payment_success_queue
= session
.createQueue("PAYMENT_CHECK_QUEUE");MessageProducer producer
= session
.createProducer(payment_success_queue
);MapMessage mapMessage
=new ActiveMQMapMessage();mapMessage
.setString("out_trade_no",outTrade
);mapMessage
.setInt("count",count
);mapMessage
.setLongProperty(ScheduledMessage
.AMQ_SCHEDULED_DELAY
,1000*60);producer
.send(mapMessage
);session
.commit();}catch (Exception e
){try {session
.rollback();} catch (JMSException e1
) {e1
.printStackTrace();}}finally {try {connection
.close();} catch (JMSException e
) {e
.printStackTrace();}}}@Overridepublic Map
<String, Object> checkAlipayPayment(String out_trade_no
) {AlipayTradeQueryRequest request
= new AlipayTradeQueryRequest();HashMap
<String, Object> resultMap
= new HashMap<>();resultMap
.put("out_trade_no",out_trade_no
);request
.setBizContent(JSON
.toJSONString(resultMap
));AlipayTradeQueryResponse response
= null
;try {response
= alipayClient
.execute(request
);} catch (AlipayApiException e
) {e
.printStackTrace();}if(response
.isSuccess()){System
.out
.println("交易創(chuàng)建已創(chuàng)建,調(diào)用成功");resultMap
.put("out_trade_no",response
.getOutTradeNo());resultMap
.put("trade_no",response
.getTradeNo());resultMap
.put("trade_status",response
.getTradeStatus());resultMap
.put("call_back_content",response
.getMsg());} else {System
.out
.println("有可能交易創(chuàng)建未創(chuàng)建,調(diào)用失敗");}return resultMap
;}
}
8.PaymentController.java
@Controller
public class PaymentController {@AutowiredAlipayClient alipayClient
;@AutowiredPaymentService paymentService
;@ReferenceOrderService orderService
;@RequestMapping("alipay/callback/return")@LoginRequired(loginSuccess
= true)public String
aliPaycallBackReturn(HttpServletRequest request
, ModelMap modelMap
){String sign
= request
.getParameter("sign");String trade_no
=request
.getParameter("trade_no");String out_trade_no
= request
.getParameter("out_trade_no");String trade_status
= request
.getParameter("trade_status");String total_amount
= request
.getParameter("total_amount");String subject
= request
.getParameter("subject");String call_back_content
= request
.getQueryString();if(StringUtils
.isNotBlank(sign
)){PaymentInfo paymentInfo
= new PaymentInfo();paymentInfo
.setOrderSn(out_trade_no
);paymentInfo
.setPaymentStatus("已支付");paymentInfo
.setAlipayTradeNo(trade_no
);paymentInfo
.setCallbackTime(new Date());paymentInfo
.setCallbackContent(call_back_content
);paymentService
.updatePayment(paymentInfo
);}return "finish";}@RequestMapping("wx/submit")@LoginRequired(loginSuccess
= true)public String
wxPay(String outTrade
, BigDecimal totalAmount
, HttpServletRequest request
, ModelMap modelMap
){return "";}@RequestMapping("alipay/submit")@LoginRequired(loginSuccess
= true)@ResponseBodypublic String
aliPay(String outTrade
, BigDecimal totalAmount
, HttpServletRequest request
, ModelMap modelMap
){String form
="";AlipayTradePagePayRequest alipayRequest
= new AlipayTradePagePayRequest();alipayRequest
.setReturnUrl(AlipayConfig
.return_payment_url
);alipayRequest
.setNotifyUrl(AlipayConfig
.notify_payment_url
);Map
<String,Object> map
=new HashMap<>();map
.put("out_trade_no",outTrade
);map
.put("product_code","FAST_INSTANT_TRADE_PAY");map
.put("total_amount",0.01);map
.put("subject","JD Apple 10 phone");String param
= JSON
.toJSONString(map
);alipayRequest
.setBizContent(param
);try {form
= alipayClient
.pageExecute(alipayRequest
).getBody();} catch (AlipayApiException e
) {e
.printStackTrace();}OmsOrder omsOrder
=orderService
.getOrderByOutTradeNo(outTrade
);PaymentInfo paymentInfo
= new PaymentInfo();paymentInfo
.setCreateTime(new Date());paymentInfo
.setOrderId(omsOrder
.getId());paymentInfo
.setOrderSn(outTrade
);paymentInfo
.setPaymentStatus("未付款");paymentInfo
.setSubject("JD商品");paymentInfo
.setTotalAmount(totalAmount
);paymentService
.savePaymentInfo(paymentInfo
);paymentService
.sendDelayPaymentResultCheckQueue(outTrade
,5);return form
;}@RequestMapping("index")@LoginRequired(loginSuccess
= true)public String
index(String outTrade
, BigDecimal totalAmount
, HttpServletRequest request
, ModelMap modelMap
){String memberId
= (String
)request
.getAttribute("memberId");String nickName
= (String
) request
.getAttribute("nickName");modelMap
.put("memberId",memberId
);modelMap
.put("outTrade",outTrade
);modelMap
.put("nickName",nickName
);modelMap
.put("totalAmount",totalAmount
);return "index";}
}
總結(jié)
以上是生活随笔為你收集整理的支付宝接口的调用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。