Skip to content

Commit

Permalink
chore: add readme
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertSasak committed Nov 16, 2023
1 parent cb5f851 commit ce277aa
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 41 deletions.
36 changes: 29 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
# react-native-stripe-apple-pay

React Native wrapper around Stripe Apple Pay
React Native wrapper around Stripe Apple Pay. Currently there are very limited configuration options. Feel free to open a PR to add more.

## Installation

```sh
npm install react-native-stripe-apple-pay
yarn add install react-native-stripe-apple-pay
```

## Usage
## Testing

```js
import { multiply } from 'react-native-stripe-apple-pay';
Before including this library in your project you can test it by running the example app and example server.

// ...
### Install dependencies

const result = await multiply(3, 7);
```sh
yarn
```

### Run server

```sh
export SECRET_KEY=sk_test_...
export PUBLISHABLE_KEY=pk_test_...
yarn server
# or
SECRET_KEY=sk_test_... PUBLISHABLE_KEY=pk_test_... yarn server
```

### Run example app

```sh
yarn example ios
# or select a simulator
yarn example ios --simulator "iPhone 15 Pro Max"
```

## Usage

See example and server folder.

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
15 changes: 5 additions & 10 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
import * as React from 'react';

import { StyleSheet, View, Text, Button, Alert } from 'react-native';
import { multiply, pay } from 'react-native-stripe-apple-pay';
import { StyleSheet, View, Button, Alert } from 'react-native';
import { pay } from 'react-native-stripe-apple-pay';

export default function App() {
const [result, setResult] = React.useState<number | undefined>();

React.useEffect(() => {
multiply(3, 7).then(setResult);
}, []);

return (
<View style={styles.container}>
<Text>Result: {result}</Text>
<Button
onPress={async () => {
// You need to get clientSecret from your server
Expand All @@ -36,14 +29,16 @@ export default function App() {
merchantIdentifier,
country,
currency,
amount,
} = await response.json();

pay(
publishableKey,
clientSecret,
merchantIdentifier,
country,
currency
currency,
amount
)
.then(() => Alert.alert('Success'))
.catch((error) => {
Expand Down
6 changes: 1 addition & 5 deletions ios/StripeApplePay.mm
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@

@interface RCT_EXTERN_MODULE(StripeApplePay, NSObject)

RCT_EXTERN_METHOD(multiply:(float)a
withB:(float)b
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(pay:(NSString *)publishableKey
withClientSecret:(NSString *)clientSecret
withMerchantIdentifier:(NSString *)merchantIdentifier
withCountry:(NSString *)country
withCurrency:(NSString *)currency
withAmount:(NSString *)amount
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)

Expand Down
16 changes: 4 additions & 12 deletions ios/StripeApplePay.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,17 @@ class StripeApplePay: NSObject, ApplePayContextDelegate {
var resolve: RCTPromiseResolveBlock? = nil
var reject: RCTPromiseRejectBlock? = nil

@objc(multiply:withB:withResolver:withRejecter:)
func multiply(
a: Float,
b: Float,
resolve: RCTPromiseResolveBlock,
reject: RCTPromiseRejectBlock
) {
resolve(a * b)
}

@objc(
pay:withClientSecret:withMerchantIdentifier:withCountry:withCurrency:withResolver:withRejecter:
pay:withClientSecret:withMerchantIdentifier:withCountry:withCurrency:withAmount:withResolver:
withRejecter:
)
func pay(
publishableKey: String,
clientSecret: String,
merchantIdentifier: String,
country: String,
currency: String,
amount: String,
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) {
Expand All @@ -43,7 +35,7 @@ class StripeApplePay: NSObject, ApplePayContextDelegate {

pr.requiredBillingContactFields = []
pr.paymentSummaryItems = [
PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(string: "29.99"))
PKPaymentSummaryItem(label: "Total", amount: NSDecimalNumber(string: amount))
]

let applePayContext = STPApplePayContext(paymentRequest: pr, delegate: self)
Expand Down
3 changes: 2 additions & 1 deletion server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const amount = 100;

if (!secretKey) {
const error =
'You must set the PUBLISHABLE_KEY environment variable.\nexport SECRET_KEY=<your publishable key>\nor\nSECRET_KEY=<your publishable key> yarn start';
'You must set the SECRET_KEY environment variable.\nexport SECRET_KEY=<your publishable key>\nor\nSECRET_KEY=<your publishable key> yarn start';
console.error(error);
throw new Error(error);
}
Expand All @@ -37,6 +37,7 @@ app.post('/create-payment-intent', async (req, res) => {
clientSecret: paymentIntent.client_secret,
country,
currency,
amount: `${amount / 100}`,
});
console.log('Payment intent created');
});
Expand Down
10 changes: 4 additions & 6 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,20 @@ const StripeApplePay = NativeModules.StripeApplePay
}
);

export function multiply(a: number, b: number): Promise<number> {
return StripeApplePay.multiply(a, b);
}

export function pay(
publishableKey: String,
clientSecret: String,
merchantIdentifier: string,
country: string,
currency: string
currency: string,
amount: string
): Promise<string> {
return StripeApplePay.pay(
publishableKey,
clientSecret,
merchantIdentifier,
country,
currency
currency,
amount
);
}

0 comments on commit ce277aa

Please sign in to comment.