# Integration with Stripe

## What You'll Need <a href="#what-youll-need" id="what-youll-need"></a>

Backbench account - [Sign Up](https://bench.backbench.io/signup), the personal account will always remain free to use.

Stripe Login - Create an account([www.stripe.com](http://www.stripe.com)) and activate your account.\
&#x9;

## **Prerequisites**

1. **Generating api keys:** once you signup and activate your account for api keys select developers section from stripe dashboard under that select api keys and there is a toggle switch to view test data to generate your test apis.

### Operation 1: Creating checkout for for Payment.

## Frontend <a href="#backend" id="backend"></a>

1. Select +, in the upper right corner of file manager to create a html file. For example, say "stripe.html" and select CREATE or hit Enter.
2. Copy and paste the code module from below.
3. Replace your data key with publishable key from your stripe dashboard under developers.
4. Select save.

```markup
<html>
  <form action="/stripe" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key=<Stripe_Key>
    data-amount="999"
    data-name="Demo Site"
    data-description="Example charge"
    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
    data-locale="auto">
  </script>
</form>
</html>
```

## Dependencies <a href="#backend" id="backend"></a>

Only for nodejs. Paste following code as `package.json`.

{% code title="package.json" %}

```javascript
{
   "dependencies":{
      "stripe":"4.19.1"
   }
}
```

{% endcode %}

## Backend <a href="#backend" id="backend"></a>

1. Select +, in the upper right corner to create a Module. For example, say "stripe.js" and select CREATE or hit Enter.
2. Copy and paste the code module from below.
3. Replace SECRET KEY from stripe dashboard under developers.
4. Select save.

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

```javascript
var stripe = require("stripe")("SECRET KEY");
module.exports.endpoint = function(req, cb) {
    stripe.charges.create({
        amount: 2000,
        currency: "usd",
        source: "tok_visa", // obtained with Stripe.js
        description: "Charge for mia.johnson@example.com"
    }, function(err, charge) {
        // asynchronously called
        if (err) {
            console.log(err);
            cb(err.message);
        }
        cb(undefined, {
            res: charge
        });
    });
};
```

{% endtab %}

{% tab title="BBLANG" %}

```javascript
template:shome()=>{ 

    '<form action="/stripe" method="POST">
  <script
    src="https://checkout.stripe.com/checkout.js" class="stripe-button"
    data-key="key"
    data-amount="999"
    data-name="Ranjith Biswas"
    data-description="Widget"
    data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
    data-locale="auto"
    ></script>
</form>'
} 

    stripe:shandler(req)->{
       bb:http:post({
            
                
        url: "https://api.stripe.com/v1/charges",
       
        Headers :{
        Content_Type: "application/x-www-form-urlencoded"
        },
        auth:{
        
        bearer:"key"
    },
           
            form:  {
                amount: "2000",
                currency: "usd",
                source: "tok_amex",
                description: "Charge for ethan.thompson@example.com"
            } 
            
            })
    }
    
    //just a redirect handler
redirect:handler(req, url) -> {
    return {
        status: 302,
        headers: {
            Location: url
        }
    }
}
```

{% endtab %}
{% endtabs %}

### Operation 2: Retrieve balance

**Balance**

This is an object representing your Stripe balance. You can retrieve it to see the balance currently on your Stripe account. You can also retrieve the balance history, which contains a list of transactions that contributed to the balance (charges, pay-outs, and so forth).

**Retrieve balance**

Retrieves the current account balance, based on the authentication that was used to make the request.

```javascript
stripe.balance.retrieve(function(err, balance) {
  // asynchronously called
});
Retrieve a balance transaction
Retrieves the balance transaction with the given ID.
stripe.balance.retrieveTransaction(
  "REPLACE TRANSACTION ID",
  function(err, balanceTransaction) {
    // asynchronously called
  }
);
```

TRANSACTION ID: The ID of the desired balance transaction, as found on any API object that affects the balance.

### Operation 3: List all balance history

Returns a list of transactions that have contributed to the Stripe account balance (e.g., charges, transfers, and so forth). The transactions are returned in sorted order, with the most recent transactions appearing first.

```javascript
stripe.balance.listTransactions({ limit: 3 }, function(err, transactions) {
  // asynchronously called
});
```

### Operation 3: Create a charge

**Charges**

To charge a credit or a debit card, you create a charge object.

To charge a credit card or other payment source, you create a charge object.

```javascript
stripe.charges.create({
  amount: amount,
  currency: "currency",
  source: "tok_visa", // obtained with Stripe.js
  description: "Charge for madison.harris@example.com"
}, function(err, charge) {
  // asynchronously called
});
```

**Amount**: A positive integer representing how much to charge.\
**Currency:** 3-letter ISO code for currency.

Other optional parameters\
**description, receipt\_email,** **source.**

### Operation 4: Retrieve a charge

Retrieves the details of a charge that has previously been created. Supply the unique charge ID that was returned from your previous request, and Stripe will return the corresponding charge information. The same information is returned when creating or refunding the charge.

```javascript
stripe.charges.retrieve("ch_1CEvhdDdG7hvTLa0PSdmu4ye", function(err, charge) {
    // asynchronously called
  }
);
```

Charge: The identifier of the charge to be retrieved.

### Operation 5: Customers

Customer objects allow you to perform recurring charges, and to track multiple charges, that are associated with the same customer. The API allows you to create, delete, and update your customers. You can retrieve individual customers as well as a list of all your customers.

**Create a customer**

Creates a new customer object.

```javascript
stripe.customers.create({
  description: 'Customer for addison.johnson@example.com',
  source: "tok_visa" // obtained with Stripe.js
}, function(err, customer) {
  // asynchronously called
});
```

**Retrieve a customer**

Retrieves the details of an existing customer. You need only supply the unique customer identifier that was returned upon customer creation.

```javascript
stripe.customers.retrieve("CUSTOMER ID",function(err, customer) {
    // asynchronously called
  }
);
```

**List all customers**

Returns a list of your customers. The customers are returned sorted by creation date, with the most recent customers appearing first.

```javascript
stripe.customers.list({ limit: 3 },
  function(err, customers) {
    // asynchronously called
  }
);
```

### Operation 6: Payouts

A Payout object is created when you receive funds from Stripe, or when you initiate a payout to either a bank account or debit card of a connected Stripe account. You can retrieve individual payouts, as well as list all payouts.

**Create a payout**

To send funds to your own bank account, you create a new payout object. Your Stripe balance must be able to cover the payout amount, or you’ll receive an “Insufficient Funds” error.

If your API key is in test mode, money won’t actually be sent, though everything else will occur as if in live mode.

```javascript
stripe.payouts.create({
  amount: AMOUNT,
  currency: "CURRENCY"
}, function(err, payout) {
  // asynchronously called
});
```

**Retrieve a payout**

Retrieves the details of an existing payout. Supply the unique payout ID from either a payout creation request or the payout list, and Stripe will return the corresponding payout information.

```javascript
stripe.payouts.retrieve("PAYOUT",
  function(err, payout) { 
  // asynchronously called
  }
}
```

Payout: The identifier of the payout to be retrieved.

**List all payouts**

Returns a list of existing payouts sent to third-party bank accounts or that Stripe has sent you. The payouts are returned in sorted order, with the most recently created payouts appearing first.

```javascript
stripe.payouts.list({ limit: 3 },
  function(err, payouts) {
    // asynchronously called
  }
);
```

### Operation 7: Refunds

Refunds objects allow you to refund a charge that has previously been created but not yet refunded. Funds will be refunded to the credit or debit card that was originally charged.

**Create a refund**

When you create a new refund, you must specify a charge on which to create it.

Creating a new refund will refund a charge that has previously been created but not yet refunded. Funds will be refunded to the credit or debit card that was originally charged.

```javascript
stripe.refunds.create({ charge: “CHARGE"
}, function(err, refund) {
  // asynchronously called
});
```

Charge: The identifier of the charge to refund.

**Retrieve a refund**

Retrieves the details of an existing payout. Supply the unique payout ID from either a payout creation request or the payout list, and Stripe will return the corresponding payout information.

```javascript
Retrieves the details of an existing refund.
stripe.refunds.retrieve("re_1CEuDFDdG7hvTLa0khr26CZa", function(err, refund) {
    // asynchronously called
  }
);
```

**List all refunds**

Returns a list of all refunds you’ve previously created. The refunds are returned in sorted order, with the most recent refunds appearing first. For convenience, the 10 most recent refunds are always available by default on the charge object.

```javascript
stripe.refunds.list({ limit: 3 }, function(err, refunds) {
    // asynchronously called
  }
);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://backbench.gitbook.io/backbench-docs/3rd-party-integrations/integration-with-stripe.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
