# SDKs

The SDKs wrap the REST API and handle authentication and the sandbox/live switch for you.

## Install

**Python**

```bash
pip install intasend-python
```

**PHP**

```bash
composer require intasend/intasend-php
```

**Node.js**

```bash
npm install intasend-node
```

**Flutter**

```bash
flutter pub add intasend_flutter
```

## Authenticate

Pass your publishable key, your secret key (server side only) and whether you're testing.
Set `test` to `false` with live keys.

**Python**

```python
from intasend import APIService

service = APIService(token="<SECRET_KEY>", publishable_key="<PUBLISHABLE_KEY>", test=True)
```

**PHP**

```php
use IntaSend\IntaSendPHP\Checkout;

$credentials = [
    'token' => '<SECRET_KEY>',
    'publishable_key' => '<PUBLISHABLE_KEY>',
    'test' => true,
];
$checkout = new Checkout();
$checkout->init($credentials);
```

**Node.js**

```javascript
const IntaSend = require('intasend-node');

const intasend = new IntaSend('<PUBLISHABLE_KEY>', '<SECRET_KEY>', true);
```

For checkout only, the publishable key is enough. Leave the secret key out of anything that runs in a browser or app.

## Examples

### Checkout

Creates a [checkout](https://developers.intasend.com/guides/collections/checkout/) and returns the payment `url`.

**Python**

```python
response = service.collect.checkout(
    phone_number="254712345678", email="jane@example.com", amount=1500, currency="KES",
    comment="Order 123", redirect_url="https://example.com/thank-you",
)
print(response.get("url"))
```

**PHP**

```php
use IntaSend\IntaSendPHP\Customer;

$customer = new Customer();
$customer->first_name = "Jane";
$customer->last_name = "Doe";
$customer->email = "jane@example.com";
$customer->country = "KE";

$resp = $checkout->create(
    $amount = 1500, $currency = "KES", $customer = $customer,
    $host = "https://example.com", $redirect_url = "https://example.com/thank-you",
    $api_ref = "order-123"
);
print_r($resp->url);
```

**Node.js**

```javascript
intasend.collection()
  .charge({
    first_name: 'Jane', last_name: 'Doe', email: 'jane@example.com',
    host: 'https://example.com', amount: 1500, currency: 'KES',
    api_ref: 'order-123', redirect_url: 'https://example.com/thank-you',
  })
  .then((resp) => console.log(resp.url))
  .catch((err) => console.error(err));
```

**Flutter**

```dart

Checkout checkout = Checkout(
    publicKey: "<PUBLISHABLE_KEY>",
    amount: 1500,
    email: "jane@example.com",
    currency: "KES",
    firstName: "Jane",
    lastName: "Doe");

// Opens the checkout in a webview. Set test to false to go live.
IntasendFlutter.initCheckout(test: true, checkout: checkout, context: context);
```

The Flutter SDK only does checkout. Confirm the payment on your backend with a [webhook](https://developers.intasend.com/guides/webhooks/).
See the [full example](https://pub.dev/packages/intasend_flutter/example).

### M-Pesa STK Push

Sends an [STK Push](https://developers.intasend.com/guides/collections/mpesa-stk-push/) prompt to the customer's phone.

**Python**

```python
response = service.collect.mpesa_stk_push(
    phone_number="254712345678", email="jane@example.com", amount=10, narrative="Purchase",
)
```

**PHP**

```php
use IntaSend\IntaSendPHP\Collection;

$collection = new Collection();
$collection->init($credentials);
$response = $collection->create(
    $amount = 10, $phone_number = "254712345678", $currency = "KES",
    $method = "MPESA_STK_PUSH", $api_ref = "order-123", $name = "", $email = "jane@example.com"
);
```

**Node.js**

```javascript
intasend.collection()
  .mpesaStkPush({
    first_name: 'Jane', last_name: 'Doe', email: 'jane@example.com',
    host: 'https://example.com', amount: 10, phone_number: '254712345678', api_ref: 'order-123',
  })
  .then((resp) => console.log(resp))
  .catch((err) => console.error(err));
```

### Payment status

Pass the `invoice_id` from the checkout or STK Push response. See [checking payment status](https://developers.intasend.com/guides/collections/overview/#check-payment-status).

**Python**

```python
response = service.collect.status(invoice_id="<INVOICE_ID>")
```

**PHP**

```php
$response = $collection->status("<INVOICE_ID>");
```

**Node.js**

```javascript
intasend.collection().status('<INVOICE_ID>').then((resp) => console.log(resp));
```

## Packages

- Python: [`intasend-python`](https://pypi.org/project/intasend-python/)
- PHP: [`intasend/intasend-php`](https://packagist.org/packages/intasend/intasend-php)
- Node.js: [`intasend-node`](https://www.npmjs.com/package/intasend-node)
- Flutter: [`intasend_flutter`](https://pub.dev/packages/intasend_flutter)
- Browser: [`intasend-checkout-sdk`](https://developers.intasend.com/guides/collections/payment-button/)
