|
| 1 | +package com.moyasarsdk |
| 2 | + |
| 3 | +import android.os.Bundle |
| 4 | +import com.facebook.react.bridge.Promise |
| 5 | +import com.facebook.react.bridge.ReactApplicationContext |
| 6 | +import com.samsung.android.sdk.samsungpay.v2.PartnerInfo |
| 7 | +import com.samsung.android.sdk.samsungpay.v2.SamsungPay |
| 8 | +import com.samsung.android.sdk.samsungpay.v2.SpaySdk |
| 9 | +import com.samsung.android.sdk.samsungpay.v2.StatusListener |
| 10 | +import java.util.concurrent.atomic.AtomicBoolean |
| 11 | + |
| 12 | +/** |
| 13 | + * Shared implementation for the Samsung Pay native module. |
| 14 | + * |
| 15 | + * Exposes an availability check so apps building their own UI can decide |
| 16 | + * whether to show a Samsung Pay option before rendering the Samsung Pay button. |
| 17 | + */ |
| 18 | +class RTNSamsungPayModuleImpl { |
| 19 | + |
| 20 | + companion object { |
| 21 | + const val NAME = "RTNSamsungPay" |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Resolves `true` only when Samsung Pay is ready to process a payment |
| 26 | + * (`SPAY_READY`). Every other status (not supported, not set up, temporarily |
| 27 | + * not allowed, or any failure) resolves `false`. The promise never rejects, |
| 28 | + * so callers get a simple boolean. |
| 29 | + */ |
| 30 | + fun isSamsungPayAvailable( |
| 31 | + reactContext: ReactApplicationContext, |
| 32 | + serviceId: String?, |
| 33 | + promise: Promise |
| 34 | + ) { |
| 35 | + Logger.d("MoyasarSDK", "Checking Samsung Pay availability...") |
| 36 | + |
| 37 | + // Guarantees the promise is settled exactly once even if the Samsung Pay |
| 38 | + // SDK were to invoke the listener more than once, which would otherwise |
| 39 | + // throw when resolving an already-settled promise. |
| 40 | + val settled = AtomicBoolean(false) |
| 41 | + val resolveOnce = { isAvailable: Boolean -> |
| 42 | + if (settled.compareAndSet(false, true)) { |
| 43 | + promise.resolve(isAvailable) |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + if (serviceId.isNullOrBlank()) { |
| 48 | + Logger.e("MoyasarSDK", "serviceId is null or blank, cannot check Samsung Pay availability") |
| 49 | + resolveOnce(false) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + try { |
| 54 | + val bundle = Bundle() |
| 55 | + bundle.putString(SpaySdk.PARTNER_SERVICE_TYPE, SpaySdk.ServiceType.INAPP_PAYMENT.toString()) |
| 56 | + |
| 57 | + val partnerInfo = PartnerInfo(serviceId, bundle) |
| 58 | + |
| 59 | + // A status query only needs the application context; using it (rather |
| 60 | + // than an Activity) avoids leaks and works even when no Activity is |
| 61 | + // resumed. Fall back defensively if it is somehow unavailable. |
| 62 | + val context = reactContext.applicationContext ?: reactContext |
| 63 | + val samsungPay = SamsungPay(context, partnerInfo) |
| 64 | + |
| 65 | + samsungPay.getSamsungPayStatus(object : StatusListener { |
| 66 | + // Capturing `samsungPay` keeps it (and the service binding it |
| 67 | + // owns) alive until this asynchronous callback fires. Using a |
| 68 | + // per-call local rather than a shared field also keeps |
| 69 | + // concurrent availability checks independent. |
| 70 | + @Suppress("unused") |
| 71 | + private val retained = samsungPay |
| 72 | + |
| 73 | + override fun onSuccess(status: Int, extras: Bundle) { |
| 74 | + val isReady = status == SpaySdk.SPAY_READY |
| 75 | + Logger.d("MoyasarSDK", "Samsung Pay status: $status, ready: $isReady") |
| 76 | + resolveOnce(isReady) |
| 77 | + } |
| 78 | + |
| 79 | + override fun onFail(errorCode: Int, extras: Bundle) { |
| 80 | + Logger.w("MoyasarSDK", "Samsung Pay status check failed with error code: $errorCode") |
| 81 | + resolveOnce(false) |
| 82 | + } |
| 83 | + }) |
| 84 | + } catch (ex: Exception) { |
| 85 | + Logger.e("MoyasarSDK", "Checking Samsung Pay availability failed", ex) |
| 86 | + resolveOnce(false) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments