Skip to content

Commit

Permalink
Merge pull request #1 from ashrafamin91/1.0.3
Browse files Browse the repository at this point in the history
1.0.3
  • Loading branch information
apis17 authored Aug 25, 2023
2 parents 128134c + 9b96e7c commit d3a293b
Show file tree
Hide file tree
Showing 13 changed files with 420 additions and 1,063 deletions.
328 changes: 149 additions & 179 deletions README.md

Large diffs are not rendered by default.

17 changes: 11 additions & 6 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ plugins {
}

android {
compileSdk 32
compileSdk 33

defaultConfig {
minSdk 24
targetSdk 32
versionCode 1
versionName "1.0"
minSdk 21
targetSdk 33
versionCode 2
versionName "1.0.3"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand All @@ -31,6 +31,11 @@ dependencies {
implementation 'com.google.zxing:core:3.5.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'com.google.android.material:material:1.6.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation "androidx.lifecycle:lifecycle-viewmodel:2.5.1"
implementation "com.google.android.gms:play-services-pay:16.1.0"
implementation "com.google.android.gms:play-services-wallet:19.2.0-beta01"
implementation 'org.apache.commons:commons-lang3:3.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
Expand All @@ -43,7 +48,7 @@ afterEvaluate {
from components.release
groupId ='rms.mobile'
artifactId = 'mobile_google_pay'
version = '1.0.0'
version = '1.0.3'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* Copyright 2023 Razer Merchant Services.
*/

package rms.library.googlepay.Helper;

import android.util.Base64;
import android.util.Log;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AlgorithmHelper {
private static final String TAG = "AlgoHelper";
private static final String TAG = "logGooglePay";

public static byte[] sha256(String data) {
Log.d(TAG, String.format("data: %s", data));
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
digest.reset();
byte[] result = digest.digest(data.getBytes());
Log.d(TAG, String.format("result: %s", UtilityHelper.ByteArrayToHexString(result)));
return result;
} catch (Exception exception) {
exception.printStackTrace();
Expand All @@ -30,14 +30,11 @@ public static byte[] sha256(String data) {
}

public static byte[] sha1(String data) {
Log.d(TAG, String.format("data: %s", data));
MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-1");
digest.reset();
byte[] result = digest.digest(data.getBytes());
;
Log.d(TAG, String.format("result: %s", UtilityHelper.ByteArrayToHexString(result)));
return result;
} catch (Exception exception) {
exception.printStackTrace();
Expand All @@ -46,30 +43,11 @@ public static byte[] sha1(String data) {
}

public static byte[] md5(String data) {
Log.d(TAG, String.format("data: %s", data));
MessageDigest digest;
try {
digest = MessageDigest.getInstance("MD5");
digest.reset();
byte[] result = digest.digest(data.getBytes());
Log.d(TAG, String.format("result: %s", UtilityHelper.ByteArrayToHexString(result)));
return result;
} catch (Exception exception) {
exception.printStackTrace();
return new byte[0];
}
}

public static byte[] encryptAES(byte[] data, byte[] key) {
try {
Log.d(TAG, String.format("data: %s", UtilityHelper.ByteArrayToHexString(data)));
Log.d(TAG, String.format("key: %s", UtilityHelper.ByteArrayToHexString(key)));

SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
byte[] result = cipher.doFinal(data);
Log.d(TAG, String.format("result: %s", UtilityHelper.ByteArrayToHexString(result)));
return result;
} catch (Exception exception) {
exception.printStackTrace();
Expand All @@ -80,44 +58,19 @@ public static byte[] encryptAES(byte[] data, byte[] key) {
public static byte[] decryptAES(byte[] data, byte[] key) {
try {
byte[] iv = new byte[16];
Log.d(TAG, String.format("data: %s", UtilityHelper.ByteArrayToHexString(data)));
Log.d(TAG, String.format("key: %s", UtilityHelper.ByteArrayToHexString(key)));
Log.d(TAG, String.format("iv: %s", UtilityHelper.ByteArrayToHexString(iv)));

SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));
byte[] result = cipher.doFinal(data);
Log.d(TAG, String.format("result: %s", UtilityHelper.ByteArrayToHexString(result)));
return result;
} catch (Exception exception) {
Log.d(TAG, String.format("exception: %s", exception.getMessage()));
return new byte[0];
}
}

public static String encodeBase64(String data) {
Log.d(TAG, String.format("data: %s", data));
byte[] base64Data = data.getBytes(StandardCharsets.UTF_8);
String result = Base64.encodeToString(base64Data, Base64.DEFAULT);
Log.d(TAG, String.format("result: %s", result));
return result;
}

public static byte[] hmac_sha256(byte[] data, byte[] key) {
try {
Log.d(TAG, String.format("data: %s", UtilityHelper.ByteArrayToHexString(data)));
Log.d(TAG, String.format("key: %s", UtilityHelper.ByteArrayToHexString(key)));

Mac mac = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKeySpec = new SecretKeySpec(key, mac.getAlgorithm());
mac.init(secretKeySpec);
byte[] result = mac.doFinal(data);
Log.d(TAG, String.format("result: %s", UtilityHelper.ByteArrayToHexString(result)));
return result;
} catch (Exception exception) {
exception.printStackTrace();
return new byte[0];
}
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package rms.library.googlepay.Helper;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;
/*
* Copyright 2023 Razer Merchant Services.
*/

import java.util.UUID;
package rms.library.googlepay.Helper;

public class ApplicationHelper {
private static final String TAG = "ApplicationHelper";
private static ApplicationHelper single_instance = null;

private String _seed = "";

protected ApplicationHelper() {
}

Expand All @@ -22,66 +17,15 @@ public static ApplicationHelper getInstance() {
return single_instance;
}

public void GenerateSeed() {
if (this._seed.isEmpty()) {
this._seed = UUID.randomUUID().toString();
}
}

public String GetSeed() {
return this._seed;
}

public String EncryptedPassword(String merchantID, String appName, String verificationKey, String seed) {
byte[] hashPassword = AlgorithmHelper.sha256(merchantID + appName);
Log.d(TAG, merchantID + appName + seed);
Log.d(TAG, UtilityHelper.ByteArrayToHexString(hashPassword));

byte[] cipheredKey = AlgorithmHelper.decryptAES(UtilityHelper.HexStringToByteArray(verificationKey), hashPassword);
Log.d(TAG, UtilityHelper.ByteArrayToHexString(cipheredKey));

byte[] md5Key = AlgorithmHelper.md5(UtilityHelper.ByteArrayToHexString(cipheredKey));
Log.d(TAG, UtilityHelper.ByteArrayToHexString(md5Key));

byte[] finalHash = AlgorithmHelper.sha1(merchantID + appName + UtilityHelper.ByteArrayToHexString(md5Key));
Log.d(TAG, merchantID + appName + UtilityHelper.ByteArrayToHexString(md5Key));
Log.d(TAG, UtilityHelper.ByteArrayToHexString(finalHash));

return UtilityHelper.ByteArrayToHexString(hashPassword);
}

public String GetAuthorizationData(String username, String password, String merchantID, String appName, String seed) {
byte[] hashPassword = AlgorithmHelper.sha256(merchantID + appName + seed);
Log.d(TAG, String.format("data: %s%s%s", merchantID, appName, seed));
Log.d(TAG, String.format("hashPassword: %s", UtilityHelper.ByteArrayToHexString(hashPassword)));

String plainText = username + ":" + password;
Log.d(TAG, String.format("cipheredKey: %s", plainText));

String base64Data = AlgorithmHelper.encodeBase64(plainText);
Log.d(TAG, String.format("base64Data: %s", base64Data));

return String.format("Basic %s", base64Data);
}

public String GetVCode(String amount, String merchantID, String orderId, String verifyKey) {
byte[] hashData = AlgorithmHelper.md5(amount + merchantID + orderId + verifyKey);
Log.d(TAG, String.format("data: %s%s%s%s", amount, merchantID, orderId, verifyKey));
Log.d(TAG, String.format("hashData: %s", UtilityHelper.ByteArrayToHexString(hashData)));

return String.format("%s", UtilityHelper.ByteArrayToHexString(hashData));
}

public String GetSKey(String txnID, String merchantID, String verifyKey, String amount) {
byte[] hashData = AlgorithmHelper.md5(txnID + merchantID + verifyKey + amount);
Log.d(TAG, String.format("data: %s%s%s%s", txnID, merchantID, verifyKey, amount));
Log.d(TAG, String.format("hashData: %s", UtilityHelper.ByteArrayToHexString(hashData)));

return String.format("%s", UtilityHelper.ByteArrayToHexString(hashData));
}

public String getStringByIdName(Context context, String idName) {
Resources res = context.getResources();
return res.getString(res.getIdentifier(idName, "string", context.getPackageName()));
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
* Copyright 2023 Razer Merchant Services.
*/

package rms.library.googlepay.Helper;

import android.os.Build;
Expand Down Expand Up @@ -43,6 +47,7 @@ public Object requestPayment(String paymentInput, String paymentInfo) {
String isSandbox = paymentInputObj.getString("isSandbox");

JSONObject error = new JSONObject();

if (!ORDERID.matcher(orderId).matches()) {
//throw new IllegalArgumentException("Invalid String");
error.put("error", "400");
Expand Down Expand Up @@ -113,13 +118,17 @@ else if (!ENV.matcher(isSandbox).matches()) {
}

public Object queryPaymentResult(String transactionInput) {

JSONObject transactionInputObj = null;

try {
transactionInputObj = new JSONObject(transactionInput);
} catch (JSONException e) {
e.printStackTrace();
}

ApiRequestService pay = new ApiRequestService();

return pay.GetPaymentResult(transactionInputObj);
}
}
71 changes: 4 additions & 67 deletions app/src/main/java/rms/library/googlepay/Helper/UtilityHelper.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,15 @@
package rms.library.googlepay.Helper;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Environment;
import android.view.View;

import com.google.zxing.common.BitMatrix;
/*
* Copyright 2023 Razer Merchant Services.
*/

import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
package rms.library.googlepay.Helper;

public class UtilityHelper {
public static byte[] StringToByteArray(String str) {
return str.getBytes(StandardCharsets.UTF_8);
}

public static String ByteArrayToString(byte[] byteArray) {
return new String(byteArray, StandardCharsets.UTF_8);
}

public static String ByteArrayToHexString(byte[] byteArray) {
StringBuilder result = new StringBuilder();
for (byte b : byteArray) {
result.append(String.format("%02x", b));
}
return result.toString();
}

public static byte[] HexStringToByteArray(String str) {
int len = str.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(str.charAt(i), 16) << 4)
+ Character.digit(str.charAt(i + 1), 16));
}
return data;
}

public static Bitmap createBitmap(BitMatrix matrix) {
int width = matrix.getWidth();
int height = matrix.getHeight();
int[] pixels = new int[width * height];
for (int y = 0; y < height; y++) {
int offset = y * width;
for (int x = 0; x < width; x++) {
pixels[offset + x] = matrix.get(x, y) ? Color.BLACK : Color.WHITE;
}
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return bitmap;
}

public static Bitmap getScreenShot(View view) {
view.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
return bitmap;
}

public static boolean storeImage(Context context, Bitmap bitmap, String fileName){
final String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + File.separator + "Barcodes";
File file = new File(dirPath, fileName);
try {
FileOutputStream fOut = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
return file.exists();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
}
Loading

0 comments on commit d3a293b

Please sign in to comment.