2016-11-10 78 views
1

登錄後,它正在生成一個哈希值,但仍然給出錯誤「發生了一些問題!再試一次」payUmoney集成提供了一個錯誤

PayUmoneySdkInitilizer.PaymentParam.Builder builder = 
    new PayUmoneySdkInitilizer.PaymentParam.Builder(); 

builder.setAmount(10.0) 
     .setTnxId("0nf7" + System.currentTimeMillis()) 
     .setPhone(<My phone>) 
     .setProductName("product_name") 
     .setFirstName(<My Name>) 
     .setEmail(<My email>) 
     .setsUrl("https://www.payumoney.com/mobileapp/payumoney/success.php") 
     .setfUrl("https://www.payumoney.com/mobileapp/payumoney/failure.php") 
     .setUdf1("").setUdf2("").setUdf3("").setUdf4("").setUdf5("") 
     .setIsDebug(false) 
     .setKey(<mykey>) 
     .setMerchantId(<my debug merchant id>); 

String tnxId="0nf7" + System.currentTimeMillis(); 
PayUmoneySdkInitilizer.PaymentParam paymentParam = builder.build(); 
String hashSequence = "<...>|"+tnxId+"|10.0|product_name|<My name>|<My email>|||||||||||salt"; 
String serverCalculatedHash= hashCal("SHA-512", hashSequence); 
Toast.makeText(getApplicationContext(), 
       serverCalculatedHash, Toast.LENGTH_SHORT).show(); 
paymentParam.setMerchantHash(serverCalculatedHash); 
// calculateServerSideHashAndInitiatePayment(paymentParam); 
PayUmoneySdkInitilizer.startPaymentActivityForResult(TrayActivity.this, paymentParam); 

public static String hashCal(String type, String str) { 
    byte[] hashseq = str.getBytes(); 
    StringBuffer hexString = new StringBuffer(); 
    try { 
     MessageDigest algorithm = MessageDigest.getInstance(type); 
     algorithm.reset(); 
     algorithm.update(hashseq); 
     byte messageDigest[] = algorithm.digest(); 
     for (int i = 0; i<messageDigest.length; i++) { 
      String hex = Integer.toHexString(0xFF &messageDigest[i]); 
      if (hex.length() == 1) { hexString.append("0"); } 
      hexString.append(hex); 
     } 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } return hexString.toString(); 
} 
+0

什麼是錯誤? –

+0

發生了一些錯誤..稍後再試 –

回答

0

你在代碼中使用:

.setTnxId("0nf7" + System.currentTimeMillis()) 

再後來:

String tnxId="0nf7" + System.currentTimeMillis(); 

也許不是唯一的問題,但你真的想用這兩個不同的值(兩次調用之間的時間可能會改變)?兩種情況下你不想要相同的tnxId嗎?


TransactionIdProvider.java:

import java.util.Locale; 

public class TransactionIdProvider { 

    private final static String DEFAULT_PREFIX = "ID"; 

    // Convenient prime number for incrementing the counter 
    private final static long ID_ADD = 0xF0AD; // "f*ck off and die" 

    // 64b counter with non-trivial start value 
    private static long idCounter = 0x0101F00DDEADBEEFL; 

    /** 
    * Returns ID consisting of prefix string and 64b counter interleaved 
    * with 32b per-4s-timestamp. 
    * 
    * May produce identical ID (collision) when: 
    * 1) class is reloaded within 4s 
    * (to fix: serialize "idCounter" upon shutdown/restart of VM, or 
    * modify prefix per start of VM) 
    * 2) more than 2^64 IDs are requested within 4s (no fix, unexpected) 
    * 3) more than 2^64 IDs are requested after cca. 550 years. 
    * (no fix, unexpected) 
    * 4) more than one static instance of TransactionIdProvider is used 
    * (two or more VMs running the app) (to fix put different prefix in 
    * every VM/server running this) 
    * 
    * Length of returned ID is prefix.length() + 24 alphanumeric symbols. 
    */ 
    public static synchronized String getNewId(final String prefix) { 
     idCounter += ID_ADD; // increment counter 
     // get 32b timestamp per ~4s (millis/4096) (good for ~550 years) 
     final int timeStamp = (int)(System.currentTimeMillis()>>12); 
     final int idPart1 = (int)(idCounter>>32); 
     final int idPart2 = (int)(idCounter); 
     return String.format(Locale.US, "%s%08X%08X%08X", 
          prefix, idPart1, timeStamp, idPart2); 
    } 

    public static String getNewId() { 
     return getNewId(DEFAULT_PREFIX); 
    } 

} 

不知道有多少可用的是這個,如果ID可能是這麼久。隨意使用/修改任何你想要的方式。

我還想知道,我是否沒有忘記重要的事情,但什麼都記不起來。

這一個的安全性方面還是相當薄弱的,因爲在4s的時間範圍內,ID就像簡單的加法一樣,但至少它不會產生1,2,3 ...系列。


難道發現了一些SDK文檔,看起來像txnId可能是25個字符長,所以你有1個字符只有前綴。或者減少時間戳,使用%07X的格式和掩碼值與0x0FFFFFFF,這將使它重複每34年 - > 2個字母的前綴。或者更改計數器32b int,應該還是綽綽有餘,除非您希望每秒處理數千次事務 - >這將刪除8個字符。或base32/base64整個身份證縮短它(取決於什麼字母是合法的內容)...

或任何...已經花了足夠的時間與此。聘請專業人士。

+0

是的,我想..其實這是我第一次嘗試付款網關 –

+0

@AdityaReja所以移動上面的字符串tnxId只定義一次,然後使用相同的價值(每筆交易)無處不在,需要交易ID。其實'timeMillis'作爲「後綴」也可能是個壞主意,如果你的解決方案几乎在同一時間被兩個人使用,他們將創建具有相同ID的交易。你應該在單獨的類中創建一些同步的靜態計數器,並且每個事務調用一次getNewTxId()。出於安全原因,您可能仍想在流程中添加一些時間或散列以避免簡單的1,2,3,4,5 ... ID。 – Ped7g

+0

@AdityaReja順便說一句,我不知道「PayUmoney SDK」,所以我不知道代碼在做什麼以及進一步的錯誤可能是什麼,但交易id的問題是一般的,所以我只回答那個問題。 – Ped7g