22
33import com .github .binarywang .utils .qrcode .QrcodeUtils ;
44import com .github .binarywang .wxpay .bean .coupon .*;
5- import com .github .binarywang .wxpay .bean .notify .WxPayOrderNotifyResult ;
6- import com .github .binarywang .wxpay .bean .notify .WxPayOrderNotifyResultTest ;
5+ import com .github .binarywang .wxpay .bean .notify .*;
76import com .github .binarywang .wxpay .bean .order .WxPayAppOrderResult ;
87import com .github .binarywang .wxpay .bean .order .WxPayMpOrderResult ;
98import com .github .binarywang .wxpay .bean .order .WxPayNativeOrderResult ;
109import com .github .binarywang .wxpay .bean .request .*;
1110import com .github .binarywang .wxpay .bean .result .*;
1211import com .github .binarywang .wxpay .bean .result .enums .TradeTypeEnum ;
1312import com .github .binarywang .wxpay .config .WxPayConfig ;
13+ import com .github .binarywang .wxpay .constant .WxPayConstants ;
1414import com .github .binarywang .wxpay .constant .WxPayConstants .AccountType ;
1515import com .github .binarywang .wxpay .constant .WxPayConstants .BillType ;
1616import com .github .binarywang .wxpay .constant .WxPayConstants .SignType ;
1919import com .github .binarywang .wxpay .service .WxPayService ;
2020import com .github .binarywang .wxpay .testbase .ApiTestModule ;
2121import com .github .binarywang .wxpay .testbase .XmlWxPayConfig ;
22+ import com .github .binarywang .wxpay .util .RequestUtils ;
2223import com .github .binarywang .wxpay .util .XmlConfig ;
2324import com .google .gson .Gson ;
2425import com .google .gson .GsonBuilder ;
2930import org .testng .annotations .Guice ;
3031import org .testng .annotations .Test ;
3132
33+ import javax .servlet .http .HttpServletRequest ;
34+ import javax .servlet .http .HttpServletResponse ;
35+ import java .math .BigDecimal ;
3236import java .nio .file .Files ;
3337import java .nio .file .Path ;
3438import java .util .Calendar ;
3539import java .util .Date ;
40+ import java .util .Optional ;
3641
3742import static com .github .binarywang .wxpay .constant .WxPayConstants .TarType ;
3843import static org .assertj .core .api .Assertions .assertThat ;
@@ -728,9 +733,9 @@ public void testUnifiedOrderV3() throws WxPayException {
728733 //构建金额信息
729734 WxPayUnifiedOrderV3Request .Amount amount = new WxPayUnifiedOrderV3Request .Amount ();
730735 //设置币种信息
731- amount .setCurrency (" CNY" );
736+ amount .setCurrency (WxPayConstants . CurrencyType . CNY );
732737 //设置金额
733- amount .setTotal (1 );
738+ amount .setTotal (BaseWxPayRequest . yuan2Fen ( BigDecimal . ONE ) );
734739 request .setAmount (amount );
735740
736741 WxPayUnifiedOrderV3Result .JsapiResult result = this .payService .createOrderV3 (TradeTypeEnum .JSAPI , request );
@@ -767,6 +772,63 @@ public void testRefundV3() throws WxPayException {
767772 System .out .println (GSON .toJson (result ));
768773 }
769774
775+ /**
776+ * 测试V3支付成功回调
777+ * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_5.shtml
778+ *
779+ * @throws Exception the exception
780+ */
781+ @ Test
782+ public void testParseOrderNotifyV3Result (HttpServletRequest request , HttpServletResponse response ) throws Exception {
783+
784+ String timestamp = request .getHeader ("Wechatpay-Timestamp" );
785+ Optional .ofNullable (timestamp ).orElseThrow (() -> new RuntimeException ("时间戳不能为空" ));
786+
787+ String nonce = request .getHeader ("Wechatpay-Nonce" );
788+ Optional .ofNullable (nonce ).orElseThrow (() -> new RuntimeException ("nonce不能为空" ));
789+
790+ String serialNo = request .getHeader ("Wechatpay-Serial" );
791+ Optional .ofNullable (serialNo ).orElseThrow (() -> new RuntimeException ("serialNo不能为空" ));
792+
793+ String signature = request .getHeader ("Wechatpay-Signature" );
794+ Optional .ofNullable (signature ).orElseThrow (() -> new RuntimeException ("signature不能为空" ));
795+
796+ log .info ("请求头参数为:timestamp:{} nonce:{} serialNo:{} signature:{}" , timestamp , nonce , serialNo , signature );
797+
798+ // V2版本请参考com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResultTest里的单元测试
799+ final WxPayOrderNotifyV3Result wxPayOrderNotifyV3Result = this .payService .parseOrderNotifyV3Result (RequestUtils .readData (request ),
800+ new SignatureHeader (timestamp , nonce , signature , serialNo ));
801+ log .info (GSON .toJson (wxPayOrderNotifyV3Result ));
802+ }
803+
804+ /**
805+ * 测试V3退款成功回调
806+ * https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_5_11.shtml
807+ *
808+ * @throws Exception the exception
809+ */
810+ @ Test
811+ public void testParseRefundNotifyV3Result (HttpServletRequest request , HttpServletResponse response ) throws Exception {
812+
813+ String timestamp = request .getHeader ("Wechatpay-Timestamp" );
814+ Optional .ofNullable (timestamp ).orElseThrow (() -> new RuntimeException ("时间戳不能为空" ));
815+
816+ String nonce = request .getHeader ("Wechatpay-Nonce" );
817+ Optional .ofNullable (nonce ).orElseThrow (() -> new RuntimeException ("nonce不能为空" ));
818+
819+ String serialNo = request .getHeader ("Wechatpay-Serial" );
820+ Optional .ofNullable (serialNo ).orElseThrow (() -> new RuntimeException ("serialNo不能为空" ));
821+
822+ String signature = request .getHeader ("Wechatpay-Signature" );
823+ Optional .ofNullable (signature ).orElseThrow (() -> new RuntimeException ("signature不能为空" ));
824+
825+ log .info ("支付请求头参数为:timestamp:{} nonce:{} serialNo:{} signature:{}" , timestamp , nonce , serialNo , signature );
826+
827+ final WxPayRefundNotifyV3Result wxPayRefundNotifyV3Result = this .payService .parseRefundNotifyV3Result (RequestUtils .readData (request ),
828+ new SignatureHeader (timestamp , nonce , signature , serialNo ));
829+ log .info (GSON .toJson (wxPayRefundNotifyV3Result ));
830+ }
831+
770832 @ Test
771833 public void testRefundQueryV3 () throws WxPayException {
772834 WxPayRefundQueryV3Request request = new WxPayRefundQueryV3Request ();
@@ -778,10 +840,11 @@ public void testRefundQueryV3() throws WxPayException {
778840
779841 /**
780842 * 测试包含正向代理的测试
843+ *
781844 * @throws WxPayException
782845 */
783846 @ Test
784- public void testQueryOrderV3WithProxy () {
847+ public void testQueryOrderV3WithProxy () {
785848 try {
786849 WxPayOrderQueryV3Request request = new WxPayOrderQueryV3Request ();
787850 request .setOutTradeNo ("n1ZvYqjAg3D3LUBa" );
0 commit comments