cordova-plugin-googlepay-button 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,20 @@
1
+ # Cordova Google PayButton API integration
2
+
3
+ Cordova plugin for implementing the Google [PayButton API](https://developers.google.com/pay/api/android/guides/resources/pay-button-api), which is now required when using a Google Pay branded payment button.
4
+
5
+ ## Installation
6
+
7
+ ```
8
+ cordova plugin add cordova-plugin-googlepay-button
9
+ ```
10
+
11
+ or for Cordova build service, e.g. [VoltBuilder](https://volt.build/), add the following to config.xml:
12
+
13
+ ```
14
+ <plugin name="cordova-plugin-googlepay-button" source="npm" spec="0.0.1" />
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ This plugin is in active development and has not yet been tested or used in a submission to Google. The version will be updated to 1.0.0 when it has been and is ready for public production use.
20
+
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "cordova-plugin-googlepay-button",
3
+ "version": "0.0.1",
4
+ "description": "Cordova plugin for implementing the Google PayButton API",
5
+ "cordova": {
6
+ "id": "cordova-plugin-googlepay-button",
7
+ "platforms": [
8
+ "android"
9
+ ]
10
+ },
11
+ "keywords": [
12
+ "ecosystem:cordova",
13
+ "cordova-android",
14
+ "googlepay",
15
+ "button",
16
+ "paybutton"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/rareplanet1/cordova-plugin-googlepay-button.git"
21
+ },
22
+ "author": "John Daniels",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/rareplanet1/cordova-plugin-googlepay-button/issues"
26
+ },
27
+ "homepage": "https://github.com/rareplanet1/cordova-plugin-googlepay-button#readme"
28
+ }
package/plugin.xml ADDED
@@ -0,0 +1,30 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
3
+ xmlns:android="http://schemas.android.com/apk/res/android"
4
+ id="cordova-plugin-googlepay-button"
5
+ version="0.0.1">
6
+
7
+ <name>Google Pay Button Plugin</name>
8
+ <description>Cordova plugin for Google Pay PayButton API integration</description>
9
+ <license>MIT</license>
10
+ <keywords>cordova,google,pay,button,paybutton</keywords>
11
+
12
+ <js-module src="www/GooglePayButton.js" name="GooglePayButton">
13
+ <clobbers target="window.GooglePayButton" />
14
+ </js-module>
15
+
16
+ <platform name="android">
17
+ <config-file target="res/xml/config.xml" parent="/*">
18
+ <feature name="GooglePayButtonPlugin">
19
+ <param name="android-package" value="com.plugin.googlepaybutton.GooglePayButtonPlugin" />
20
+ </feature>
21
+ </config-file>
22
+
23
+ <!-- Google Pay dependencies -->
24
+ <framework src="src/android/build.gradle" custom="true" type="gradleReference" />
25
+
26
+ <!-- Source files -->
27
+ <source-file src="src/android/GooglePayButtonPlugin.java"
28
+ target-dir="src/com/plugin/googlepaybutton" />
29
+ </platform>
30
+ </plugin>
@@ -0,0 +1,113 @@
1
+ package com.plugin.googlepaybutton;
2
+
3
+ import org.apache.cordova.CordovaPlugin;
4
+ import org.apache.cordova.CallbackContext;
5
+ import org.apache.cordova.CordovaInterface;
6
+ import org.apache.cordova.CordovaWebView;
7
+
8
+ import org.json.JSONArray;
9
+ import org.json.JSONException;
10
+ import org.json.JSONObject;
11
+
12
+ import android.app.Activity;
13
+ import android.content.Context;
14
+ import android.view.View;
15
+ import android.view.ViewGroup;
16
+ import android.widget.FrameLayout;
17
+
18
+ import com.google.android.gms.wallet.button.ButtonConstants;
19
+ import com.google.android.gms.wallet.button.ButtonOptions;
20
+ import com.google.android.gms.wallet.button.PayButton;
21
+
22
+ public class GooglePayButtonPlugin extends CordovaPlugin {
23
+
24
+ private PayButton payButton;
25
+ private CallbackContext callbackContext;
26
+
27
+ @Override
28
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
29
+ this.callbackContext = callbackContext;
30
+
31
+ if (action.equals("showPayButton")) {
32
+ this.showPayButton();
33
+ return true;
34
+ } else if (action.equals("hidePayButton")) {
35
+ this.hidePayButton();
36
+ return true;
37
+ }
38
+ return false;
39
+ }
40
+
41
+ private void showPayButton() {
42
+ cordova.getActivity().runOnUiThread(new Runnable() {
43
+ @Override
44
+ public void run() {
45
+ try {
46
+ // Get the WebView's parent container
47
+ View webView = webView.getView();
48
+ ViewGroup parent = (ViewGroup) webView.getParent();
49
+
50
+ // Create PayButton if not exists
51
+ if (payButton == null) {
52
+ Context context = cordova.getActivity().getApplicationContext();
53
+ payButton = new PayButton(context);
54
+
55
+ // Configure button using the official API
56
+ ButtonOptions buttonOptions = ButtonOptions.newBuilder()
57
+ .setButtonTheme(ButtonConstants.ButtonTheme.DARK)
58
+ .setButtonType(ButtonConstants.ButtonType.CHECKOUT)
59
+ .setCornerRadius(4)
60
+ .build();
61
+
62
+ payButton.initialize(buttonOptions);
63
+
64
+ // Set click listener
65
+ payButton.setOnClickListener(new View.OnClickListener() {
66
+ @Override
67
+ public void onClick(View v) {
68
+ if (callbackContext != null) {
69
+ callbackContext.success("clicked");
70
+ }
71
+ }
72
+ });
73
+ }
74
+
75
+ // Remove from any previous parent
76
+ if (payButton.getParent() != null) {
77
+ ((ViewGroup) payButton.getParent()).removeView(payButton);
78
+ }
79
+
80
+ // Add to bottom of screen
81
+ FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
82
+ FrameLayout.LayoutParams.MATCH_PARENT,
83
+ FrameLayout.LayoutParams.WRAP_CONTENT
84
+ );
85
+ params.gravity = android.view.Gravity.BOTTOM;
86
+ params.setMargins(40, 20, 40, 100); // left, top, right, bottom margins
87
+
88
+ parent.addView(payButton, params);
89
+ payButton.setVisibility(View.VISIBLE);
90
+
91
+ } catch (Exception e) {
92
+ if (callbackContext != null) {
93
+ callbackContext.error("Error showing PayButton: " + e.getMessage());
94
+ }
95
+ }
96
+ }
97
+ });
98
+ }
99
+
100
+ private void hidePayButton() {
101
+ cordova.getActivity().runOnUiThread(new Runnable() {
102
+ @Override
103
+ public void run() {
104
+ if (payButton != null) {
105
+ payButton.setVisibility(View.GONE);
106
+ if (payButton.getParent() != null) {
107
+ ((ViewGroup) payButton.getParent()).removeView(payButton);
108
+ }
109
+ }
110
+ }
111
+ });
112
+ }
113
+ }
@@ -0,0 +1,12 @@
1
+
2
+ dependencies {
3
+ /* "play-services-wallet" is the only module needed for the Google Pay API and this sample app
4
+ but the "play-services" module can also be used.
5
+ The version provided is the minimum version required for the most recent major version of the
6
+ Google Pay API library. Your app may choose a dependency greater than the value below.
7
+ See https://developers.google.com/pay/api/android/guides/setup#dependencies for more info.
8
+ */
9
+ implementation 'com.google.android.gms:play-services-wallet:18.1.2'
10
+ implementation 'com.android.support:appcompat-v7:28.0.0'
11
+ implementation "net.sourceforge.streamsupport:streamsupport:1.5.1"
12
+ }
@@ -0,0 +1,119 @@
1
+ // GooglePayButton.js - JavaScript interface for the Cordova plugin
2
+
3
+ class GooglePayButton {
4
+ constructor() {
5
+ this.isReady = false;
6
+ this.callbacks = {};
7
+
8
+ // Wait for Cordova to be ready
9
+ document.addEventListener('deviceready', () => {
10
+ this.isReady = true;
11
+ }, false);
12
+ }
13
+
14
+ /**
15
+ * Create the Google Pay button with specified options
16
+ * @param {Object} options - Button configuration options
17
+ * @param {string} options.theme - 'light' or 'dark'
18
+ * @param {string} options.type - 'buy', 'checkout', 'pay', etc.
19
+ * @param {number} options.cornerRadius - Corner radius in dp
20
+ * @param {string} options.allowedPaymentMethods - JSON string of allowed payment methods
21
+ * @param {Function} callback - Callback function for button clicks
22
+ */
23
+ createButton(options, callback) {
24
+ return new Promise((resolve, reject) => {
25
+ if (!this.isReady) {
26
+ reject(new Error('Cordova not ready'));
27
+ return;
28
+ }
29
+
30
+ // Store the click callback
31
+ if (callback) {
32
+ this.callbacks.onClick = callback;
33
+ }
34
+
35
+ cordova.exec(
36
+ (result) => {
37
+ if (result === 'payButtonClicked' && this.callbacks.onClick) {
38
+ this.callbacks.onClick();
39
+ } else {
40
+ resolve(result);
41
+ }
42
+ },
43
+ (error) => reject(error),
44
+ 'GooglePayButtonPlugin',
45
+ 'createPayButton',
46
+ [options]
47
+ );
48
+ });
49
+ }
50
+
51
+ /**
52
+ * Show the Google Pay button at specified position
53
+ * @param {Object} position - Button position and size
54
+ * @param {number} position.x - X coordinate in dp
55
+ * @param {number} position.y - Y coordinate in dp
56
+ * @param {number} position.width - Width in dp
57
+ * @param {number} position.height - Height in dp
58
+ */
59
+ showButton(position) {
60
+ return new Promise((resolve, reject) => {
61
+ if (!this.isReady) {
62
+ reject(new Error('Cordova not ready'));
63
+ return;
64
+ }
65
+
66
+ cordova.exec(
67
+ resolve,
68
+ reject,
69
+ 'GooglePayButtonPlugin',
70
+ 'showPayButton',
71
+ [position]
72
+ );
73
+ });
74
+ }
75
+
76
+ /**
77
+ * Hide the Google Pay button
78
+ */
79
+ hideButton() {
80
+ return new Promise((resolve, reject) => {
81
+ if (!this.isReady) {
82
+ reject(new Error('Cordova not ready'));
83
+ return;
84
+ }
85
+
86
+ cordova.exec(
87
+ resolve,
88
+ reject,
89
+ 'GooglePayButtonPlugin',
90
+ 'hidePayButton',
91
+ []
92
+ );
93
+ });
94
+ }
95
+
96
+ /**
97
+ * Update button options
98
+ * @param {Object} options - New button options
99
+ */
100
+ updateButton(options) {
101
+ return new Promise((resolve, reject) => {
102
+ if (!this.isReady) {
103
+ reject(new Error('Cordova not ready'));
104
+ return;
105
+ }
106
+
107
+ cordova.exec(
108
+ resolve,
109
+ reject,
110
+ 'GooglePayButtonPlugin',
111
+ 'updatePayButton',
112
+ [options]
113
+ );
114
+ });
115
+ }
116
+ }
117
+
118
+ // Export for use in React/ES6 modules
119
+ export default new GooglePayButton();