From 1dda04a9e5a9429935cc86ef19e89df47c1d98ec Mon Sep 17 00:00:00 2001 From: luoyang Date: Fri, 20 Mar 2020 11:05:57 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E5=95=86=E6=88=B7=E7=AB=AFtransaction?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=AE=B0=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../core/impls/TradeLogServiceImpl.java | 2 +- .../GatewayApplicationMerchantDemoTest.java | 282 ++++++++++++++++++ 3 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/GatewayApplicationMerchantDemoTest.java diff --git a/pom.xml b/pom.xml index 0bb623d5b..642e4d63c 100644 --- a/pom.xml +++ b/pom.xml @@ -9,7 +9,7 @@ 4.0.0 manage - 1.4.3 + 1.4.4 UTF-8 1.8.0 diff --git a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java index f9f65ff54..36c94ceac 100644 --- a/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java +++ b/src/main/java/au/com/royalpay/payment/manage/tradelog/core/impls/TradeLogServiceImpl.java @@ -536,7 +536,7 @@ public class TradeLogServiceImpl implements TradeLogService { JSONObject mchConfig = merchantInfoProvider.getMchExtParams(partner.getIntValue("client_id")); params.put("hide_sub_mch", mchConfig.getBooleanValue("hide_sub_mch")); - List logs = transactionMapper.listTransFlowPage(params, + PageList logs = transactionMapper.listTransFlowPage(params, new PageBounds(query.getPage(), query.getLimit(), Order.formString("transaction_time.desc"))); TimeZoneUtils.switchTimeZone(logs, timezone, "create_time", "confirm_time", "transaction_time"); Paginator paginator = new Paginator(query.getPage(), query.getLimit(), logs.size()); diff --git a/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/GatewayApplicationMerchantDemoTest.java b/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/GatewayApplicationMerchantDemoTest.java new file mode 100644 index 000000000..8a3565b6e --- /dev/null +++ b/src/test/java/au/com/royalpay/payment/manage/citypartner/core/impls/GatewayApplicationMerchantDemoTest.java @@ -0,0 +1,282 @@ +package au.com.royalpay.payment.manage.citypartner.core.impls; + +import au.com.royalpay.payment.tools.exceptions.BadRequestException; +import au.com.royalpay.payment.tools.exceptions.ServerErrorException; +import cn.yixblog.platform.http.HttpRequestGenerator; +import cn.yixblog.platform.http.HttpRequestResult; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.RandomStringUtils; +import org.apache.commons.lang3.time.DateFormatUtils; +import org.apache.commons.lang3.time.DateUtils; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.web.bind.annotation.RequestMethod; + +import java.io.*; +import java.nio.charset.StandardCharsets; +import java.security.*; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Date; + + +@SpringBootTest +public class GatewayApplicationMerchantDemoTest { + + static String SHORTID = "NH6NR8J53M"; + static String RPDEMOPUBKEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtBE+bgY1FHUOP/7PIDdRZH8BUkDJBpZkI5kh+ytJs7uLJY7LJMOG2HTkJv5SdzdsBBaJgGAHcIjZrsd6oRrcBohWmAxTN0p+vlQEwNd689mlOAObds23lXWtn+huDv8cjorC4Doy79grPni8SZYJocqs9qFpdaQLIZaGUIRTIE0eTSV2tZ3ry4l6kNdFLpkBvXW76iWC6CZcrfuGcgiJq438CmFeXrY5HQPjRZ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wDwIDAQAB"; + /** + * The demo can run tests directly + */ + private static Provider securityProvider = new BouncyCastleProvider(); + private Logger logger = LoggerFactory.getLogger(getClass()); + + private static PublicKey loadPublicKey(InputStream ins) { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(ins)); + String line; + StringBuilder certBuffer = new StringBuilder(); + while ((line = reader.readLine()) != null) { + if (line.startsWith("-")) { + continue; + } + certBuffer.append(line).append("\r"); + } + return resolvePublic(Base64.decodeBase64(certBuffer.toString())); + } catch (IOException | InvalidKeySpecException e) { + throw new ServerErrorException(e); + } finally { + IOUtils.closeQuietly(ins); + } + } + + private static PublicKey resolvePublic(byte[] bytes) throws InvalidKeySpecException { + X509EncodedKeySpec x509 = new X509EncodedKeySpec(bytes); + try { + KeyFactory factory = KeyFactory.getInstance("RSA", securityProvider); + return factory.generatePublic(x509); + } catch (NoSuchAlgorithmException e) { + //Shall Never Happense + e.printStackTrace(); + return null; + } + } + + private static PrivateKey loadPrivateKey(InputStream ins) { + try { + BufferedReader reader = new BufferedReader(new InputStreamReader(ins)); + String line; + StringBuilder certBuffer = new StringBuilder(); + while ((line = reader.readLine()) != null) { + if (line.startsWith("-")) { + continue; + } + certBuffer.append(line); + } + return resolvePrivate(Base64.decodeBase64(certBuffer.toString())); + } catch (IOException | InvalidKeySpecException e) { + throw new ServerErrorException(e); + } finally { + IOUtils.closeQuietly(ins); + } + } + + private static PrivateKey resolvePrivate(byte[] bytes) throws InvalidKeySpecException { + PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(bytes); + try { + KeyFactory factory = KeyFactory.getInstance("RSA", securityProvider); + return factory.generatePrivate(pkcs8); + } catch (NoSuchAlgorithmException e) { + //Shall Never happens + e.printStackTrace(); + return null; + } + } + + @Test + public void applicationMerchant() throws Exception { + JSONObject companyInfo = new JSONObject() {{ + put("company_name", "CompanyLongName"); + put("short_name", "CompanyShortName"); + put("store_name", "StoreName"); + put("business_name", null); + put("business_structure", "ASSOCIATION"); + put("abn", "2332423"); + put("acn", null); + put("company_phone", "0411120000"); + put("logo_url", "https://file.royalpay.com.au/open/2018/11/01/1541059398731_7fJpTXCHagUGOHjc7NfvZIxEP28yMD.png"); + }}; + JSONObject contactInfo = new JSONObject() {{ + put("contact_person", "Applicant name"); + put("contact_phone", "0411120000"); + put("contact_email", "anyemail@test.com"); + put("contact_job", "General Manager"); + put("address", "111 Bourke Street"); + put("suburb", "Melbourne"); + put("postcode", "3000"); + put("state", "VIC"); + put("country", "AUS"); + put("registered_address", "111 Bourke Street"); + put("registered_suburb", "Melbourne"); + put("registered_postcode", "3000"); + put("registered_state", "VIC"); + put("timezone", "Australia/Melbourne"); //"AUSTRALIA_MELBOURNE" is illegal, just need to fill in the enumerated values + }}; + JSONObject ClientLegalConfig = new JSONObject() {{ + put("legal_representative_person", "Legal contact person name"); + put("legal_representative_phone", "0411120001"); + put("legal_representative_email", "legalcontactemail@test.com"); + put("legal_representative_job", "Legal contact job title"); + }}; + JSONObject clientPayConfig = new JSONObject() {{ + put("client_pay_type", "1"); //"1:The online payment" is illegal,just need to fill in the enumerated values + put("client_pay_desc", "101");//"101:PC Web" is illegal,just need to fill in the enumerated values + put("royalpay_industry", "70001");//"Supermarket" is illegal,just need to fill in the enumerated values + put("company_website", "www.anywebsite.com.au"); + }}; + JSONObject settleConfig = new JSONObject() {{ + put("swift_code", "BKWAAU6P"); + put("bsb_no", "063551"); + put("account_no", "00000000"); + put("account_name", "account name"); + put("clean_days", 1); + put("wechat_rate", 2.5); //0 is not allowed + put("alipay_rate", 2.5); //0 is not allowed + put("alipay_online_rate", 2.5); //0 is not allowed + put("transaction_fee", 0); + put("active_time", DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss")); //Please format the date :'yyyy-MM-dd HH:mm:ss"' + put("expire_time", DateFormatUtils.format(DateUtils.addYears(new Date(), 1), "yyyy-MM-dd HH:mm:ss"));//Please format the date :'yyyy-MM-dd HH:mm:ss"' + }}; + + JSONObject complianceInfo = new JSONObject() {{ + put("id_type", "passport");//"passport:Passport" is illegal,just need to fill in the enumerated values + put("id_title", "Ultimate beneficiary owner");//"jobTitle" is illegal,Legal values:"Ultimate beneficiary owner"| "CEO"| "Director"| "General Manager"| "Other" + put("bank_statement", "https://file.royalpay.com.au/open/2018/11/01/1541059398731_7fJpTXCHagUGOHjc7NfvZIxEP28yMD.png"); + put("certificate_of_registration", "https://file.royalpay.com.au/open/2018/11/01/1541059398731_7fJpTXCHagUGOHjc7NfvZIxEP28yMD.pn"); + put("id_file", "https://file.royalpay.com.au/open/2018/11/01/1541059398731_7fJpTXCHagUGOHjc7NfvZIxEP28yMD.png"); + }}; + + + JSONObject params = new JSONObject() {{ + put("apply_id", "orgtestadmin3"); //username = spadmin or spgwadmin + put("company_info", companyInfo); + put("contact_info", contactInfo); + put("legal_info", ClientLegalConfig); + put("pay_info", clientPayConfig); + put("settle_info", settleConfig); + put("compliance_file_info", complianceInfo); + }}; + String originUrl = "https://sandbox.royalpay.com.au/api/v1.0/org_gateway/partners/" + SHORTID + "/merchant/application"; + String url = addSignUrl(originUrl, params, SPDEMOPRIKEY); + HttpRequestGenerator gen = new HttpRequestGenerator(url, RequestMethod.POST); + gen.setJSONEntity(params); + gen.setTimeout(10000); + HttpRequestResult result = gen.execute(); + if (result.isSuccess()) { + JSONObject resultJson = result.getResponseContentJSONObj(); + logger.debug("result info :{}", resultJson.toJSONString()); + if (!signIsValid(resultJson)) { + throw new BadRequestException("Sign Invalid"); + } + //todo save partner_code && credential_code + }else { + //todo error + } + } + + private boolean signIsValid(JSONObject jsonObject) { + JSONObject data = jsonObject.getJSONObject("data"); + String sign = jsonObject.getString("sign"); + return checkSign(JSONObject.toJSONBytes(data, SerializerFeature.MapSortField), sign, RPDEMOPUBKEY); + } + + @Test + public void queryMerchantStatus() throws Exception{ + String partnerCode = "PHQ3"; + String originUrl = "https://sandbox.royalpay.com.au/api/v1.0/org_gateway/partners/" + SHORTID + "/merchant/" + partnerCode + "/status"; + String url = addSignUrl(originUrl, null, SPDEMOPRIKEY); + HttpRequestGenerator gen = new HttpRequestGenerator(url, RequestMethod.GET); + HttpRequestResult result = gen.execute(); + if (result.isSuccess()) { + JSONObject resultJson = result.getResponseContentJSONObj(); + logger.debug("result info :{}", resultJson.toJSONString()); + if (!signIsValid(resultJson)) { + throw new BadRequestException("Sign Invalid"); + } + }else { + //todo error + } + } + + @Test + public void updateFile() throws Exception{ + String originUrl = "https://sandbox.royalpay.com.au/api/v1.0/org_gateway/partners/" + SHORTID + "/attachment/files"; + String url = addSignUrl(originUrl, null, SPDEMOPRIKEY); + File file = new File("/Users/1/Downloads/banner_eshop.jpg"); + InputStream stream = new FileInputStream(file); + HttpRequestGenerator gen = new HttpRequestGenerator(url, RequestMethod.POST); + gen.setTimeout(10000); + gen.initFileEntity().attachFile("file", "banner_eshop.jpg", stream); + HttpRequestResult result = gen.execute(); + if (result.isSuccess()) { + JSONObject resultJson = result.getResponseContentJSONObj(); + logger.debug("result info :{}", resultJson.toJSONString()); + if (!signIsValid(resultJson)) { + throw new BadRequestException("Sign Invalid"); + } + }else { + //todo error + } + } + + private String addSignUrl(String originUrl, JSONObject params, String priKey) { + if (params == null) { + params = new JSONObject(); + } + String nonceStr = RandomStringUtils.random(15, true, true); + params.put("url", originUrl); + params.put("sign_type", "RSA2"); + params.put("nonce_str", nonceStr); + String signStr = generateSign(JSONObject.toJSONBytes(params, SerializerFeature.MapSortField), priKey); + originUrl += ("?nonce_str=" + nonceStr); + originUrl += "&sign_type=RSA2"; + originUrl += "&sign=" + signStr; + return originUrl; + } + + private String generateSign(byte[] source, String privateKey) { + try { + PrivateKey priKey = loadPrivateKey(new ByteArrayInputStream(privateKey.getBytes(StandardCharsets.UTF_8))); + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initSign(priKey); + signature.update(source); + byte[] signed = signature.sign(); + return Base64.encodeBase64URLSafeString(signed); + } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) { + //shall never happen + throw new ServerErrorException(e); + } + } + + private boolean checkSign(byte[] source, String sign, String publicKey) { + try { + PublicKey pubKey = loadPublicKey(new ByteArrayInputStream(publicKey.getBytes(StandardCharsets.UTF_8))); + Signature signature = Signature.getInstance("SHA256withRSA"); + signature.initVerify(pubKey); + signature.update(source); + return signature.verify(Base64.decodeBase64(sign)); + } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) { + //shall never happen + throw new ServerErrorException(e); + } + } +}