Browse Source

支付宝生成支付二维码Demo

小柒2012 7 years ago
parent
commit
8d7d3057e4

+ 13 - 0
pom.xml

@@ -67,6 +67,19 @@
 		    <artifactId>commons-configuration</artifactId>
 		    <version>1.10</version>
 		</dependency>
+		<!--gson -->
+		<dependency>
+		    <groupId>com.google.code.gson</groupId>
+		    <artifactId>gson</artifactId>
+		</dependency>
+		<!-- zxing -->
+		<dependency>
+		    <groupId>com.google.zxing</groupId>
+		    <artifactId>core</artifactId>
+		    <version>3.2.1</version>
+		</dependency>
+		
+		
 		
 	</dependencies>
 	<build>

+ 44 - 0
src/main/java/com/itstyle/common/constants/PayType.java

@@ -0,0 +1,44 @@
+package com.itstyle.common.constants;
+/**
+ * 支付类型
+ * 创建者	张志朋
+ * 创建时间	2017年1月22日
+ *
+ */
+public enum PayType {
+	/**支付类型*/
+	ALI("支付宝",(short)1),WECHAT("微信",(short)2),UNION("银联",(short)3);
+	
+	private Short code;
+	private String name;
+	
+	private PayType(String name, Short code) {
+		this.name = name;
+		this.code = code;
+	}
+
+	public static String getName(Short code,String name) {
+		for (PayType c : PayType.values()) {
+			if (c.getCode() == code) {
+				return c.name;
+			}
+		}
+		return null;
+	}
+	
+	public String getName() {
+		return name;
+	}
+
+	public void setName(String name) {
+		this.name = name;
+	}
+
+	public short getCode() {
+		return code;
+	}
+
+	public void setCode(short code) {
+		this.code = code;
+	}
+}

+ 6 - 4
src/main/java/com/itstyle/modules/alipay/service/impl/AliPayServiceImpl.java

@@ -38,7 +38,9 @@ public class AliPayServiceImpl implements IAliPayService {
 	public String aliPay(Product product) {
 		logger.info(product.getAttach()+"(订单号:"+product.getOutTradeNo()+"生成支付宝支付码)");
 		String  message = Constants.SUCCESS;
-		String imgPath= Constants.SF_FILE_SEPARATOR+"alipay_"+product.getOutTradeNo()+".png";
+		//String imgPath= Constants.SF_FILE_SEPARATOR+"alipay_"+product.getOutTradeNo()+".png";
+		//二维码存放路径 自行定义
+		String imgPath= "D:\\"+product.getOutTradeNo()+".png";
 		String outTradeNo = product.getOutTradeNo();
 		String subject = product.getBody();
 		String totalAmount =  CommonUtil.divide(product.getTotalFee(), "100").toString();
@@ -50,11 +52,11 @@ public class AliPayServiceImpl implements IAliPayService {
 		ExtendParams extendParams = new ExtendParams();
 		extendParams.setSysServiceProviderId("2088100200300400500");
 		// 订单描述,可以对交易或商品进行一个详细地描述,比如填写"购买商品2件共15.00元"
-		String body = "购买商品2件共15.00元";
+		String body = product.getBody();
 		// 支付超时,定义为120分钟
 		String timeoutExpress = "120m";
+		String notifyUrl  =product.getFrontUrl();
 		// 创建扫码支付请求builder,设置请求参数
-		String notifyUrl  = Constants.PAY_URL.get("alipay_notify_url");
 		AlipayTradePrecreateRequestBuilder builder = new AlipayTradePrecreateRequestBuilder()
 		.setSubject(subject)
 		.setTotalAmount(totalAmount)
@@ -218,7 +220,7 @@ public class AliPayServiceImpl implements IAliPayService {
 		logger.info("支付宝手机支付下单");
 		String  totalFee =  CommonUtil.divide(product.getTotalFee(), "100").toString();
 		AlipayTradeWapPayRequest alipayRequest = new AlipayTradeWapPayRequest();
-		String subject = "我去年买个苹果而已";
+		String subject = product.getBody();
 		String returnUrl = "回调地址 http 自定义";
 		alipayRequest.setReturnUrl(returnUrl);//前台通知
 		String notifyUrl  = Constants.PAY_URL.get("alipay_notify_url");

+ 32 - 11
src/test/java/com/itstyle/test/AliPayTest.java

@@ -1,22 +1,43 @@
 package com.itstyle.test;
 
-import org.junit.Test;
-import org.junit.runner.RunWith;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.test.context.SpringBootTest;
-import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.boot.CommandLineRunner;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.context.annotation.ComponentScan;
 
+import com.alipay.demo.trade.config.Configs;
+import com.itstyle.common.constants.PayType;
+import com.itstyle.common.constants.PayWay;
 import com.itstyle.common.model.Product;
 import com.itstyle.modules.alipay.service.IAliPayService;
 
-@RunWith(SpringRunner.class)
-@SpringBootTest
-public class AliPayTest {
+@SpringBootApplication
+@ComponentScan(basePackages={"com.itstyle"})
+public class AliPayTest implements CommandLineRunner {
 	@Autowired
 	private IAliPayService aliPayService;
-	@Test
-	public void hello() {
-		Product product = new Product();
-		aliPayService.aliPay(product);
+
+	public static void main(String[] args) {
+		SpringApplication.run(AliPayTest.class, args);
+	}
+
+	@Override
+	public void run(String... args) throws Exception {
+		try {
+			Configs.init("zfbinfo.properties");
+			Product product = new Product();
+			product.setAttach("测试");
+			product.setBody("两个苹果八毛钱");
+			product.setFrontUrl("https");
+			product.setOutTradeNo("20170730");
+			product.setPayType(PayType.ALI.getCode());
+			product.setPayWay(PayWay.PC.getCode());
+			product.setProductId("111111");
+			product.setTotalFee("10");
+			aliPayService.aliPay(product);
+		} catch (Exception e) {
+			e.printStackTrace();
+		}
 	}
 }