@@ -3,7 +3,6 @@ package com.outsystems.payments
33import android.app.Activity
44import android.content.Intent
55import com.google.gson.Gson
6- import com.outsystems.plugins.oscordova.CordovaImplementation
76import com.outsystems.plugins.payments.controller.OSPMTGooglePayManager
87import com.outsystems.plugins.payments.controller.OSPMTGooglePlayHelper
98import com.outsystems.plugins.payments.controller.OSPMTController
@@ -13,12 +12,16 @@ import com.outsystems.plugins.payments.model.PaymentDetails
1312import com.outsystems.plugins.payments.model.Tokenization
1413import org.apache.cordova.CallbackContext
1514import org.apache.cordova.CordovaInterface
15+ import org.apache.cordova.CordovaPlugin
1616import org.apache.cordova.CordovaWebView
17+ import org.apache.cordova.PluginResult
18+ import org.apache.cordova.PluginResult.Status
1719import org.json.JSONArray
20+ import org.json.JSONObject
1821
19- class OSPayments : CordovaImplementation () {
22+ class OSPayments : CordovaPlugin () {
2023
21- override var callbackContext: CallbackContext ? = null
24+ var callbackContext: CallbackContext ? = null
2225 private lateinit var googlePayManager: OSPMTGooglePayManager
2326 private lateinit var paymentsController: OSPMTController
2427 private lateinit var googlePlayHelper: OSPMTGooglePlayHelper
@@ -47,9 +50,9 @@ class OSPayments : CordovaImplementation() {
4750
4851 override fun initialize (cordova : CordovaInterface , webView : CordovaWebView ) {
4952 super .initialize(cordova, webView)
50- googlePayManager = OSPMTGooglePayManager (getActivity() )
53+ googlePayManager = OSPMTGooglePayManager (cordova.activity )
5154 googlePlayHelper = OSPMTGooglePlayHelper ()
52- paymentsController = OSPMTController (googlePayManager, buildPaymentConfigurationInfo(getActivity() ), googlePlayHelper)
55+ paymentsController = OSPMTController (googlePayManager, buildPaymentConfigurationInfo(cordova.activity ), googlePlayHelper)
5356 }
5457
5558 override fun execute (action : String , args : JSONArray , callbackContext : CallbackContext ): Boolean {
@@ -70,7 +73,7 @@ class OSPayments : CordovaImplementation() {
7073 }
7174
7275 private fun setupConfiguration (args : JSONArray ) {
73- paymentsController.setupConfiguration(getActivity() , args.get(0 ).toString(),
76+ paymentsController.setupConfiguration(cordova.activity , args.get(0 ).toString(),
7477 {
7578 sendPluginResult(it, null )
7679 },
@@ -81,7 +84,7 @@ class OSPayments : CordovaImplementation() {
8184 }
8285
8386 private fun checkWalletSetup (){
84- paymentsController.verifyIfWalletIsSetup(getActivity() ,
87+ paymentsController.verifyIfWalletIsSetup(cordova.activity ,
8588 {
8689 sendPluginResult(it, null )
8790 }, {
@@ -91,12 +94,12 @@ class OSPayments : CordovaImplementation() {
9194 }
9295
9396 private fun setDetailsAndTriggerPayment (args : JSONArray ){
94- setAsActivityResultCallback( )
97+ cordova.setActivityResultCallback( this )
9598
9699 paymentDetails = buildPaymentDetails(args)
97100
98101 if (paymentDetails != null ){
99- paymentsController.setDetailsAndTriggerPayment(getActivity() , paymentDetails!! , args.getString(1 ))
102+ paymentsController.setDetailsAndTriggerPayment(cordova.activity , paymentDetails!! , args.getString(1 ))
100103 }
101104 else {
102105 sendPluginResult(null , Pair (formatErrorCode(OSPMTError .INVALID_PAYMENT_DETAILS .code), OSPMTError .INVALID_PAYMENT_DETAILS .description))
@@ -126,11 +129,6 @@ class OSPayments : CordovaImplementation() {
126129 // Not used in this project.
127130 }
128131
129- override fun areGooglePlayServicesAvailable (): Boolean {
130- // Not used in this project.
131- return false
132- }
133-
134132 private fun formatErrorCode (code : Int ): String {
135133 return ERROR_FORMAT_PREFIX + code.toString().padStart(4 , ' 0' )
136134 }
@@ -163,12 +161,26 @@ class OSPayments : CordovaImplementation() {
163161 private fun buildPaymentDetails (args : JSONArray ) : PaymentDetails ? {
164162 return try {
165163 gson.fromJson(args.getString(0 ), PaymentDetails ::class .java)
166- } catch (e : Exception ){
164+ } catch (_ : Exception ){
167165 null
168166 }
169167 }
170168
171169 private fun getStringResourceId (activity : Activity , typeAndName : String ): Int {
172170 return activity.resources.getIdentifier(typeAndName, " string" , activity.packageName)
173171 }
172+
173+ private fun <T > sendPluginResult (resultVariable : T , error : Pair <String , String >? ) {
174+ var pluginResult: PluginResult ?
175+ resultVariable?.let {
176+ pluginResult = PluginResult (Status .OK , resultVariable.toString())
177+ this .callbackContext?.sendPluginResult(pluginResult)
178+ return
179+ }
180+ val jsonResult = JSONObject ()
181+ jsonResult.put(" code" , error?.first)
182+ jsonResult.put(" message" , error?.second ? : " No Results" )
183+ pluginResult = PluginResult (Status .ERROR , jsonResult)
184+ this .callbackContext?.sendPluginResult(pluginResult)
185+ }
174186}
0 commit comments