Skip to content

Commit 5fdaf86

Browse files
authored
Share QrCode from frontend
* Share QrCode from frontend * Update allowed methods
1 parent a9ec721 commit 5fdaf86

File tree

11 files changed

+240
-9
lines changed

11 files changed

+240
-9
lines changed

payment-environment-resolver/src/main/java/com/payu/android/front/sdk/payment_environment_resolver/rest/environment/ProductionRestEnvironment.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,9 @@ public Environment getGooglePayEnvironment() {
5858
public String silentAcceptEnvironment() {
5959
return "'https://secure.payu.com'";
6060
}
61+
62+
@Override
63+
public String getOrigin() {
64+
return "secure.payu.com";
65+
}
6166
}

payment-environment-resolver/src/main/java/com/payu/android/front/sdk/payment_environment_resolver/rest/environment/SandboxPartnersRestEnvironment.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,10 @@ public Environment getGooglePayEnvironment() {
4141
public String silentAcceptEnvironment() {
4242
return null;
4343
}
44+
45+
@Override
46+
public String getOrigin() {
47+
return null;
48+
}
49+
4450
}

payment-environment-resolver/src/main/java/com/payu/android/front/sdk/payment_environment_resolver/rest/environment/SandboxRestEnvironment.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,8 @@ public String silentAcceptEnvironment() {
6262
return "'https://merch-prod.snd.payu.com'";
6363
}
6464

65+
@Override
66+
public String getOrigin() {
67+
return "merch-test.snd.payu.com";
68+
}
6569
}

payment-environment-resolver/src/test/java/com/payu/android/front/sdk/payment_environment_resolver/rest/environment/TestRestEnvironment.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,8 @@ public String silentAcceptEnvironment() {
7070
return null;
7171
}
7272

73-
73+
@Override
74+
public String getOrigin() {
75+
return "test.payu.com";
76+
}
7477
}

payment-library-api-client/src/main/java/com/payu/android/front/sdk/payment_library_api_client/internal/rest/environment/RestEnvironment.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ public interface RestEnvironment {
3535

3636
String silentAcceptEnvironment();
3737

38+
String getOrigin();
39+
3840
}

payment-library-chooser-module/src/main/java/com/payu/android/front/sdk/payment_library_payment_chooser/payment_method/internal/use_cases/RetrievePblPaymentMethods.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,21 @@
2020
//For the list of values please see http://developers.payu.com/en/overview.html#Polish_PBLs
2121
public class RetrievePblPaymentMethods {
2222
private static final List<String> ALLOWED_METHODS = Arrays.asList(
23-
"blik", "m", "w", "wc", "nstb", "sgb", "plsb", "o", "i", "p", "g", "gbx", "l", "as", "u", "ab", "ps",
24-
"wm", "wd", "bo", "bnx", "orx", "bs", "t", "cs", "mp", "kb", "rf", "pg", "pf", "era", "cb", "uc", "bt", "pt", "ma", "vc",
25-
"uni", "sporo", "tatra", "vub", "posta", "viamo", "sb", "it", "gp", "sdd", "pid", "pbc", "pmb", "paypal",
26-
"pmbc", "ptrl", "ucro", "dpts", "dpcz", "dpt", "dpkl", "dpp", "dppron", "dpklczk", "dpkleur", "dpklhuf", "dpklron", "ppf",
27-
// PSD2PL
28-
"ms", "gs", "pks", "is", "os", "wms", "ws", "abs",
29-
// PSD2CZ
30-
"css", "pfs", "kbs", "airs", "cbs"
23+
"blik", "m", "w", "o", "i", "p", "g", "gbx", "l", "ab", "bn", "wm", "wc", "bo", "bnx",
24+
"bs", "nstb", "plsb", "b", "t", "sgb",
25+
// Pay Letter
26+
"dpts", "dpcz", "dpt", "dpkl", "dpp", "ppf", "dppron", "dpklczk", "dpkleur", "dpklhuf",
27+
"dpklron",
28+
// Czech
29+
"cs", "mp", "kb", "rf", "pg", "pf", "cb", "uc", "bt", "pt", "qrcz",
30+
// Slovak
31+
"bliksk", "posta", "sporo", "tatra", "vub", "viamo",
32+
// International
33+
"pbc", "pmb", "paypal", "pmbc", "ptrl", "pscd", "sdd", "sb", "uni",
34+
// PL
35+
"ms", "gs", "pks", "is", "os", "wms", "ws", "abs", "wys",
36+
// CZ
37+
"css", "pfs", "kbs", "airs", "cbs", "creds", "jtbs", "mons", "rbczs", "ucczs", "mps"
3138
);
3239

3340
@NonNull

payment-library-webview-module/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ dependencies {
5656

5757
testImplementation "org.mockito:mockito-core:$project.mockitoVersion"
5858
testImplementation "org.assertj:assertj-core:$project.assertJsVersion"
59+
testImplementation project(path: ':payment-environment-resolver')
5960

6061
androidTestImplementation "androidx.test.ext:junit:$project.junitAssertionsVersion"
6162
androidTestImplementation "androidx.test.espresso:espresso-core:$project.espressoVersion"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.payu.android.front.sdk.payment_library_webview_module.web.view;
2+
3+
import android.app.Activity;
4+
import android.content.Context;
5+
import android.content.Intent;
6+
import android.graphics.Bitmap;
7+
import android.graphics.BitmapFactory;
8+
import android.net.Uri;
9+
import android.provider.MediaStore;
10+
import android.util.Base64;
11+
import android.util.Log;
12+
import android.webkit.JavascriptInterface;
13+
import android.content.ContentValues;
14+
import android.webkit.WebView;
15+
16+
import com.payu.android.front.sdk.payment_library_api_client.internal.rest.environment.RestEnvironment;
17+
import com.payu.android.front.sdk.payment_library_core_android.ConfigurationEnvironmentProvider;
18+
19+
import java.io.OutputStream;
20+
import java.net.URI;
21+
import java.net.URISyntaxException;
22+
23+
public class QrCode {
24+
private static final String TAG = QrCode.class.getName();
25+
private final Context context;
26+
private final WebView webView;
27+
private final RestEnvironment environment;
28+
29+
public QrCode(Context context, WebView webView) {
30+
this.context = context;
31+
this.webView = webView;
32+
this.environment = ConfigurationEnvironmentProvider.provideEnvironment(context);
33+
}
34+
35+
@JavascriptInterface
36+
public void share(String imageData, String fileName) {
37+
getCurrentUrlOrigin(urlOrigin -> {
38+
if (urlOrigin != null && urlOrigin.equals(environment.getOrigin())) {
39+
shareQrCode(imageData, fileName);
40+
}
41+
42+
});
43+
}
44+
45+
private void shareQrCode(String imageData, String fileName) {
46+
try {
47+
byte[] decodedBytes = Base64.decode(imageData, Base64.DEFAULT);
48+
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedBytes, 0, decodedBytes.length);
49+
50+
ContentValues values = new ContentValues();
51+
values.put(MediaStore.Images.Media.TITLE, fileName);
52+
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
53+
54+
Uri imageUri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
55+
56+
if (imageUri != null) {
57+
OutputStream outputStream = context.getContentResolver().openOutputStream(imageUri);
58+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
59+
outputStream.close();
60+
61+
Intent shareIntent = new Intent(Intent.ACTION_SEND);
62+
shareIntent.setType("image/png");
63+
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
64+
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
65+
context.startActivity(Intent.createChooser(shareIntent, "QR"));
66+
}
67+
} catch (Exception e) {
68+
Log.w(TAG, "Share error: " + e.getMessage());
69+
}
70+
}
71+
72+
private void getCurrentUrlOrigin(UrlOriginCallback callback) {
73+
if (webView != null) {
74+
((Activity) context).runOnUiThread(() -> {
75+
String url = webView.getUrl();
76+
77+
try {
78+
URI uri = new URI(url);
79+
callback.onUrlReceived(uri.getHost());
80+
} catch (URISyntaxException e) {
81+
callback.onUrlReceived(null);
82+
}
83+
});
84+
} else {
85+
callback.onUrlReceived(null);
86+
}
87+
}
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.payu.android.front.sdk.payment_library_webview_module.web.view;
2+
3+
public interface UrlOriginCallback {
4+
void onUrlReceived(String url);
5+
}

payment-library-webview-module/src/main/java/com/payu/android/front/sdk/payment_library_webview_module/web/view/WebPaymentView.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ protected void onFinishInflate() {
4949
this.webView = findViewById(R.id.payment_webView);
5050
this.progressBar = findViewById(R.id.payment_progressBar);
5151
configureWebViewSettings(webView.getSettings());
52+
webView.addJavascriptInterface(new QrCode(getContext(), webView), "QrCode");
5253
applyStyles();
5354
}
5455

0 commit comments

Comments
 (0)