Skip to content

Commit

Permalink
feat: allow passing a config object instead of separate arguments (#5)
Browse files Browse the repository at this point in the history
* Repleace release-it with auto

* Update ci.yml

* Set access public

* Create empty CHANGELOG.md file

* Add scan job with trivy

* Fix CI actions

* Upgrade babel-plugin-module-resolver to fix json5 vulnerability

* Skip scan node_modules directory

* Fix buyerId prop type

* Pass debugMode prop

* Update example app README with cache reset info

* Update default GR4VY_ID in .env.example

* Add externalIdentifier prop support

* Add other props and attempt to properly type paymentSource

* Add cartItems prop

* Add cartItems typing on the js side

* Add paymentSource and cartItems type conversions

* Remove previous cartItems map

* Fix Android params

* Use Native Event Emitter in iOS

* Attempt to add the same `onEvent` logging to the Android part

* Fix events handling in android

* Remove any onEvent listener before adding a new one

* Clean up android code

* Set default buyerId to null in example app

* Allow passing a config object to showPaymentSheet

* Remove RCTCartItem
  • Loading branch information
luca-gr4vy authored Apr 18, 2023
1 parent 7e49ed1 commit fad9723
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,25 @@ public String getName() {
}

@ReactMethod
public void showPaymentSheet(
String gr4vyId,
String token,
Double amount,
String currency,
String country,
String buyerId,
String externalIdentifier,
String store,
String display,
String intent,
ReadableMap metadata,
String paymentSource,
ReadableArray cartItems,
String environment,
Boolean debugMode) {
public void showPaymentSheet(ReadableMap config) {
Log.d("Gr4vy", "showPaymentSheet()");

String gr4vyId = config.getString("gr4vyId");
String environment = config.getString("environment");
String token = config.getString("token");
Double amount = config.getDouble("amount");
String currency = config.getString("currency");
String country = config.getString("country");
String buyerId = config.getString("buyerId");
String externalIdentifier = config.getString("externalIdentifier");
String store = config.getString("store");
String display = config.getString("display");
String intent = config.getString("intent");
ReadableMap metadata = config.getMap("metadata");
String paymentSource = config.getString("paymentSource");
ReadableArray cartItems = config.getArray("cartItems");
Boolean debugMode = config.getBoolean("debugMode");

ReactApplicationContext context = getReactApplicationContext();
Intent androidIntent = new Intent(context, Gr4vyActivity.class);

Expand Down
28 changes: 4 additions & 24 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,21 @@ import { SafeAreaView, StatusBar } from 'react-native'
import { Checkout } from './components/Checkout'
import EmbedReactNative, {
EmbedReactNativeEventEmitter,
Gr4vyConfig,
Gr4vyEvent,
} from '@gr4vy/embed-react-native'
import { total } from './constants/data'

const config = {
const config: Gr4vyConfig = {
gr4vyId: `${GR4VY_ID}`,
env: 'sandbox',
environment: 'sandbox',
token: `${TOKEN}`,
amount: total,
currency: 'USD',
country: 'US',
buyerId: null,
externalIdentifier: null,
store: 'ask',
display: 'all',
intent: 'capture',
metadata: {},
paymentSource: null,
cartItems: null,
debugMode: true,
}

Expand All @@ -41,23 +37,7 @@ function App(): JSX.Element {
)

const handleCheckout = () => {
EmbedReactNative.showPaymentSheet(
config.gr4vyId,
config.token,
config.amount,
config.currency,
config.country,
config.buyerId,
config.externalIdentifier,
config.store,
config.display,
config.intent,
config.metadata,
config.paymentSource,
config.cartItems,
config.env,
config.debugMode
)
EmbedReactNative.showPaymentSheet(config)
}

return (
Expand Down
1 change: 0 additions & 1 deletion ios/EmbedReactNative-Bridging-Header.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,3 @@
#import <React/RCTUtils.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTConvert.h>
#import "RCTCartItem.h"
23 changes: 1 addition & 22 deletions ios/EmbedReactNative.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,11 @@
#import <React/RCTConvert.h>
#import <React/RCTEventEmitter.h>
#import <Foundation/Foundation.h>
#import "RCTCartItem.h"

@interface RCT_EXTERN_MODULE(EmbedReactNative, NSObject)

RCT_EXTERN_METHOD(
showPaymentSheet:(NSString *)gr4vyId
token:(NSString *)token
amount:(double)amount
currency:(NSString *)currency
country:(NSString *)country
buyerId:(NSString *)buyerId
externalIdentifier:(NSString *)externalIdentifier
store:(NSString *)store
display:(NSString *)display
intent:(NSString *)intent
metadata:(NSDictionary *)metadata
paymentSource:(NSString *)paymentSource
cartItems:(NSArray<RCTCartItem *> *)cartItems
environment:(NSString *)environment
debugMode:(BOOL)debugMode)
RCT_EXTERN_METHOD(showPaymentSheet:(NSDictionary *)config)
@end

@interface RCT_EXTERN_MODULE(EmbedReactNativeEvents, RCTEventEmitter)
RCT_EXTERN_METHOD(supportedEvents)
@end

@implementation RCTConvert (RCTCartItemArray)
RCT_ARRAY_CONVERTER(RCTCartItem)
@end
56 changes: 31 additions & 25 deletions ios/EmbedReactNative.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class EmbedReactNative: NSObject {
intent: String?,
metadata: [String: String]?,
paymentSource: String?,
cartItems: [RCTCartItem]?,
cartItems: [Gr4vyCartItem]?,
environment: String?,
debugMode: Bool = false,
completion: @escaping(_ gr4vy: Gr4vy?) -> Void) {
Expand All @@ -28,13 +28,6 @@ class EmbedReactNative: NSObject {
paymentSourceConverted = Gr4vyPaymentSource(rawValue: paymentSource!)
}

var cartItemsConverted: [Gr4vyCartItem]?
if let cartItems = cartItems {
cartItemsConverted = cartItems.compactMap { (item: RCTCartItem) -> Gr4vyCartItem? in
return try? Gr4vyCartItem(name: item.name, quantity: item.quantity, unitAmount: item.unitAmount)
}
}

DispatchQueue.main.async(execute: {
guard let gr4vy = Gr4vy(gr4vyId: gr4vyId,
token: token,
Expand All @@ -48,7 +41,7 @@ class EmbedReactNative: NSObject {
intent: intent,
metadata: metadata,
paymentSource: paymentSourceConverted,
cartItems: cartItemsConverted,
cartItems: cartItems,
environment: (environment != nil && environment?.lowercased() == "production") ? .production : .sandbox,
debugMode: debugMode) else {
completion(nil)
Expand All @@ -69,23 +62,36 @@ class EmbedReactNative: NSObject {
}

@objc
func showPaymentSheet(
_ gr4vyId: String,
token: String,
amount: Double,
currency: String,
country: String,
buyerId: String?,
externalIdentifier: String?,
store: String?,
display: String?,
intent: String?,
metadata: [String: String]?,
paymentSource: String?,
cartItems: [RCTCartItem]?,
environment: String?,
debugMode: Bool)
func showPaymentSheet(_ config: [String: Any])
{
guard let gr4vyId = config["gr4vyId"] as? String,
let environment = config["environment"] as? String?,
let token = config["token"] as? String,
let amount = config["amount"] as? Double,
let currency = config["currency"] as? String,
let country = config["country"] as? String,
let buyerId = config["buyerId"] as? String?,
let externalIdentifier = config["externalIdentifier"] as? String?,
let store = config["store"] as? String?,
let display = config["display"] as? String?,
let intent = config["intent"] as? String?,
let metadata = config["metadata"] as? [String: String]?,
let paymentSource = config["paymentSource"] as? String?,
let cartItems = config["cartItems"] as? [Gr4vyCartItem]?,
let debugMode = config["debugMode"] as? Bool
else {
EmbedReactNativeEvents.emitter.sendEvent(
withName: "onEvent",
body: [
"name": "generalError",
"data": [
"message" : "Invalid configuration"
]
]
)
return
}

gr4vyInit(gr4vyId: gr4vyId,
token: token,
amount: Int(amount),
Expand Down
9 changes: 0 additions & 9 deletions ios/RCTCartItem.h

This file was deleted.

14 changes: 0 additions & 14 deletions ios/RCTCartItem.m

This file was deleted.

6 changes: 3 additions & 3 deletions src/EmbedReactNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ export type Gr4vyEvent = {
}

export type Gr4vyConfig = {
gr4vId: string
gr4vyId: string
environment?: string | null
token: string
amount: number
currency: string
Expand All @@ -46,12 +47,11 @@ export type Gr4vyConfig = {
quantity: string
unitAmount: string
} | null
environment?: string | null
debugMode?: boolean
}

export interface Gr4vyInterface {
showPaymentSheet(...args: Gr4vyConfig[keyof Gr4vyConfig][]): void
showPaymentSheet(config: Gr4vyConfig): void
}

const LINKING_ERROR =
Expand Down

0 comments on commit fad9723

Please sign in to comment.