# React Native SDK

Take crypto payments in a React Native app (Android and iOS, bare or Expo dev build). The checkout opens as a
full-screen modal in your app and returns a typed result.

- **Package:** [`@razcrypto/react-native`](https://www.npmjs.com/package/@razcrypto/react-native) on npm
- **Version:** 1.0.0
- **Requires:** React 18+, React Native 0.72+, `react-native-webview` 13+
- **Key in the app:** none (opens the `checkout_url` from your server), or the publishable key `pk_…`
- **Download:** [razcrypto-react-native-sdk.zip](https://razcryptogateway.com/downloads/razcrypto-react-native-sdk.zip) · [Source on GitHub](https://github.com/WorldxGrowth/razcrypto-react-native-sdk) · MIT

## How it works

1. The app asks **your server** to start a payment.
2. Your server creates it with the **secret key** and returns the `checkout_url`
   ([PHP / Laravel](/docs/php-sdk), [Node.js](/docs/node-sdk), [Python](/docs/python-sdk)).
3. The app opens the checkout. The customer pays.
4. The app gets `success`, `pending`, `cancelled` or `failed` and tells your server.
5. Your server verifies the signature and the real status. **Only the server marks the order paid.**

## 1. Install

### From npm (recommended)

```bash
npm install @razcrypto/react-native react-native-webview
cd ios && pod install      # iOS only
```

Expo: `npx expo install react-native-webview`, then `npm install @razcrypto/react-native`. It works in a development
build and in EAS builds.

### From the download (local / offline)

```bash
curl -L -o razcrypto-react-native-sdk.zip https://razcryptogateway.com/downloads/razcrypto-react-native-sdk.zip
mkdir razcrypto-react-native-sdk && unzip razcrypto-react-native-sdk.zip -d razcrypto-react-native-sdk
cd razcrypto-react-native-sdk
npm install && npm run build && npm pack        # creates razcrypto-react-native-1.0.0.tgz
cd ../your-app
npm install ../razcrypto-react-native-sdk/razcrypto-react-native-1.0.0.tgz react-native-webview
```

A packed `.tgz` works better with Metro than a symlinked folder.

### Android

The internet permission is already in every React Native app. To let Android 11+ open wallet apps directly, add
this to `android/app/src/main/AndroidManifest.xml` inside `<manifest>`:

```xml
<queries>
  <intent><action android:name="android.intent.action.VIEW" /><data android:scheme="metamask" /></intent>
  <intent><action android:name="android.intent.action.VIEW" /><data android:scheme="trust" /></intent>
</queries>
```

## 2. Open the checkout

### Hook (promise style)

```tsx
import { Alert, Button } from 'react-native';
import { useRazCryptoCheckout } from '@razcrypto/react-native';

export function PayWithCrypto({ amount }: { amount: number }) {
  const { open, element } = useRazCryptoCheckout();

  const pay = async () => {
    const order = await api.post('/payments/razcrypto/create', { amount });   // { payment_id, checkout_url }
    const r = await open({ checkoutUrl: order.checkout_url, locale: 'en' });  // or 'hi'

    switch (r.status) {
      case 'success': {
        const res = await api.post('/payments/razcrypto/verify', {
          payment_id: r.paymentId,
          reference_id: r.referenceId,
          status: r.paymentStatus,
          signature: r.signature,
        });
        res.status === 'completed' ? showPaid() : waitForPaid(r.paymentId);
        break;
      }
      case 'pending':                 // on-chain, confirming
        waitForPaid(r.paymentId);
        break;
      case 'cancelled':               // closed; order stays pending
        break;
      case 'failed':
        Alert.alert('Payment failed', r.message);
    }
  };

  return (
    <>
      <Button title="Pay with crypto" onPress={pay} />
      {element}
    </>
  );
}
```

Render `{element}` once in the screen; it is the checkout modal.

### Component

```tsx
import { RazCryptoCheckout } from '@razcrypto/react-native';

<RazCryptoCheckout
  visible={open}
  options={{ checkoutUrl }}           // or { key: 'pk_live_…', paymentId: 'pay_…' }
  onResult={(r) => {
    setOpen(false);
    handle(r);
  }}
/>
```

`waitForPaid` polls your server (`GET /payments/razcrypto/status/{id}`) for about 30 seconds, then tells the
customer the order will update shortly.

## 3. Server side

A complete Laravel backend (migration, admin settings, create / verify / status / webhook) is on the
[PHP SDK page](/docs/php-sdk#laravel-full-integration-for-a-mobile-app). Node.js: [Node.js SDK](/docs/node-sdk#full-integration-for-a-mobile-app).

- `signature` = hex HMAC-SHA256(webhook signing secret, `payment_id|reference_id|status`). Verify it on the server.
- Fulfil only when `payments.retrieve(id)` returns `paid` or on the verified `payment.paid` [webhook](/docs/webhooks).

## Reference

| Options | Use |
|---|---|
| `{ checkoutUrl, locale? }` | Recommended. The `checkout_url` from your server. |
| `{ key, paymentId, payHost?, locale? }` | Publishable key + payment id. `payHost` only for staging / self-hosted. |

| Result `status` | Other fields |
|---|---|
| `'success'` | `paymentId`, `referenceId`, `paymentStatus`, `signature` |
| `'pending'` | `paymentId` |
| `'cancelled'` | — |
| `'failed'` | `code`, `message` |

**What the customer sees:** network → coin → QR or "Open in MetaMask / Trust" → green success screen that closes by
itself. Android Back asks "Leave this payment?".

## Test and go live

1. Server on `sk_test_…`, open a payment, then `simulate(paymentId, 'paid')` on the server.
2. Check **Dashboard → API apps → Webhooks** for the delivery.
3. Live: `sk_live_…` + live signing secret + live webhook URL, then one small real payment.

## Develop the SDK

```bash
npm install
npm run typecheck && npm test && npm run build
```
