> For the complete documentation index, see [llms.txt](https://docs.appstitch.dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.appstitch.dev/stripe.md).

# Stripe

## Install

{% tabs %}
{% tab title="JavaScript" %}

```bash
npm install @appstitch/stripe
```

{% endtab %}

{% tab title="Flutter" %}

```yaml
appstitch_stripe: ^1.0.0
```

{% endtab %}
{% endtabs %}

Install Peer Dependencies

{% tabs %}
{% tab title="JavaScript" %}

```bash
npm install @appstitch/core
npm install @appstitch/types
```

{% endtab %}

{% tab title="Flutter" %}

```yaml
appstitch_core: ^2.0.0
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
Remember to initialize [Appstitch](https://docs.appstitch.dev/)
{% endhint %}

## Usage

Apart from a few exceptions, this library mirror Stripe's API. You can use [Stripe's Docs](https://stripe.com/docs/api?lang=node) for reference.

{% tabs %}
{% tab title=" JavaScript" %}

```typescript
import { createSubscription } from "@appstitch/stripe";
// OR
import * as stripeClient from "@appstitch/stripe";
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:appstitch_stripe/stripe.dart'
```

{% endtab %}
{% endtabs %}

### Create a customer

{% tabs %}
{% tab title="JavaScript" %}

```typescript
import * as stripe from "@appstitch/stripe";

const createCustomer = () => {

  stripe.createCustomer({
      email: "email@example.com"
  })
  .then((res) => {
    if (res.object === "customer") {
       // success
    }
  })
}
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:appstitch_stripe/stripe.dart';
import 'package:appstitch_stripe/types.dart';

createCustomer() async {

    final customerOpts = CreateCustomerOpts(email: "email@example.com");
    final customer = await stripe.createCustomer(customerOpts);
    
    if (customer.object == "customer") {
      // success
    }
}

```

{% endtab %}
{% endtabs %}

### Create a subscription

{% tabs %}
{% tab title="JavaScript" %}

```typescript
import * as stripe from "@appstitch/stripe";

const createSubscription = () => {

  stripe.createSubsciption({
      customer : "cus_ABC123456",
      items:[{price : "price_XYZ123456"}],
  })
  .then(result => {

    if(result.object === "subscription")
    {
        // Success
    }
  })
}
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:appstitch_stripe/stripe.dart'
import 'package:appstitch_stripe/types.dart';

createSubscription() async {

    final subscriptionOpts = CreateSubscriptionOpts(
      customer: "cus_ABC123456",
      items: [SubscriptionItemOpts(price: "price_XYZ123456")],
    );
    final subscription = await stripe.createCustomer(subscriptionOpts);
    
    if (subscription.object == "subscription") {
      // success
    }
}
```

{% endtab %}
{% endtabs %}

### Create a Payment Intent

{% tabs %}
{% tab title="JavaScript" %}

```typescript
import * as stripe from "@appstitch/stripe";

const createPaymentIntent = () => {
    
    stripeClient.createPaymentIntent({
        customer: "cus_abc123"
        amount : 2500, // $25.00 
        currency:"usd",
        confirm:true
        receipt_email: "customer@example.com"
    })
    .then(result => {
        if(result.object === "payment_intent")
        {
            // Success
        }
    })
}


```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:appstitch_stripe/stripe.dart'
import 'package:appstitch_stripe/types.dart';


createPaymentIntent() async {

  final paymentIntentOpts = CreatePaymentIntentOpts(
        customer: "cus_abc123",
        amount: 2500, // $25.00
        currency: "usd",
        confirm: true,
        receiptEmail: "customer@example.com");
    final paymentIntent = await stripe.createPaymentIntent(paymentIntentOpts);

    if (paymentIntent.object == "payment_intent") {
      // success
    }

}
```

{% endtab %}
{% endtabs %}

### Stripe Connect Options

All requests can be made on behalf of another Stripe Account. Here's how you do it

{% tabs %}
{% tab title="JavaScript" %}

```typescript
import * as stripe from "@appstitch/stripe";

const createPaymentIntent = () => {

   stripe.createPaymentIntent({
        customer: "cus_abc123"
        amount : 2500,
        currency:"usd",
        confirm:true
        receipt_email: "customer@example.com"
        
        // Stripe Account ID         
        stripeAccount : "act_xyz890"
    })
    .then(result => {
        if(result.object === "payment_intent")
        {
            // Success
        }
    })
}
```

{% endtab %}

{% tab title="Flutter" %}

```dart
import 'package:appstitch_stripe/types.dart';

createCustomer() async {

    final customerOpts = CreateCustomerOpts(email: "email@example.com");
    
    // Stripe Account ID  
    customerOpts.stripeAccount = "act_abc123";
    
    final customer = await stripe.createCustomer(customerOpts);
    
    if (customer.object == "customer") {
      // success
    }
}
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}
**Note:** Similar to Stripe's API, stripeAccount is in camel case
{% endhint %}

### Exceptions

For Retrieve, Delete and Update requests, Stripe Api look something similar to this

```javascript
// Update a customer
customers.update(id, {
    ...options
})
```

After consideration, we went for chose to go for this approach instead. Notice the invoice id is passed with the update options

```
// Update a customer
updateInvoice({
    id,
    ...options
})
```

Reference [Stripe's Docs](https://stripe.com/docs/api?lang=node)
