# Immediate capture (/platforms/checkout/immediate-capture)

Capture funds from buyers instantly and transfer to sellers. Follow steps to generate headers, create orders, capture payments with PayPal immediate capture.



Use this integration to capture money from your buyers and move it to your sellers instantly.

## Know before you code [#know-before-you-code]

> **Info:** To use this integration you must:
>
> * Be an [approved partner](/platforms/get-started).
> * [Onboard sellers](/platforms/seller-onboarding/) before you begin this integration.
> * Have an [access token](/platforms/create-account/#exchange-your-api-credentials-for-an-access-token).
> * Inform your sellers of [PayPal's Seller Protection policy](https://www.paypal.com/webapps/mpp/security/seller-protection), so they are aware of use cases that invalidate that protection, such as shipping to an address other than the one in the transaction confirmation.

> **Info:** **Note:** This server-side integration uses the [Orders REST API](/api/orders/v2/) for this server-side integration. You'll need to set intent to capture in the **create order** call for this feature to work.

## 1. Generate PayPal-Auth-Assertion header [#1-generate-paypal-auth-assertion-header]

You'll need to Pass the [PayPal-Auth-Assertion](/api/rest/requests/#paypal-auth-assertion)header with the standard `Content-Type`, `Authorization`, and `PayPal-Request-ID` headers. In client-side JavaScript, the value of the `PayPal-Auth-Assertion` header can be generated as follows:

```javascript lineNumbers
// client-side JavaScript

function encodeObjectToBase64(object) {
  const objectString = JSON.stringify(object);
  return window.btoa(objectString);
}

const clientId = "CLIENT-ID";
const sellerPayerId = "SELLER-PAYER-ID"; // preferred
// const sellerEmail = "SELLER-ACCOUNT-EMAIL"; // use instead of payer-id if required

const header = {
  alg: "none"
};
const encodedHeader = encodeObjectToBase64(header);

const payload = {
  iss: clientId,
  payer_id: sellerPayerId
  // email: sellerEmail
};
const encodedPayload = encodeObjectToBase64(payload);

const jwt = `${encodedHeader}.${encodedPayload}.`; // json web token
console.log(`Paypal-Auth-Assertion=${jwt}`);
```

> **Info:** **Note:** The token contains two period (.) characters, which are required according to the JSON web token structure.

## Modify the code [#modify-the-code]

#### Modify the code [#modify-the-code-1]

* Use the client ID of the platform or marketplace from the PayPal Developer dashboard for `clientId`.
* In the given example, the `sellerPayerId` is the payer ID of the receiving seller's PayPal account. You can also use `email` instead of `payer_id` and supply the email address of the seller's PayPal account.

Example functions to generate the `PayPal-Auth-Assertion` header in other programming environments:

#### Node.js [#nodejs]

```javascript lineNumbers
// Node.js

          function encodeObjectToBase64(object) {
            const objectString = JSON.stringify(object);
            return Buffer
              .from(objectString)
              .toString("base64");
          }

          const clientId = "CLIENT-ID";
          const sellerPayerId = "SELLER-PAYER-ID"; // preferred
          // const sellerEmail = "SELLER-ACCOUNT-EMAIL"; // use instead if payer-id unknown

          const header = {
            alg: "none"
          };
          const encodedHeader = encodeObjectToBase64(header);

          const payload = {
            iss: clientId,
            payer_id: sellerPayerId
            // email: sellerEmail
          };
          const encodedPayload = encodeObjectToBase64(payload);

          const jwt = `${encodedHeader}.${encodedPayload}.`; // json web token
          console.log(`Paypal-Auth-Assertion=${jwt}`);
```

#### Java [#java]

```javascript lineNumbers
// Java

import org.apache.commons.codec.binary.Base64;

public class Base64Encode {

  public static void main(String[] args) {
    String clientId = "CLIENT-ID";
    String sellerPayerId = "SELLER-PAYER-ID"; // preferred
    // String sellerEmail = "SELLER-ACCOUNT-EMAIL"; // use instead if payer-id unknown

    String header = "{\"alg\":\"none\"}";
    String payload =
      "{\"iss\":\"" + clientId + "\",\"payer_id\":\"" + sellerPayerId + "\"}";
    // "{"iss":"" + clientId + "","email":"" + sellerEmail + ""}";

    byte[] encodedHeader = Base64.encodeBase64(header.getBytes());
    byte[] encodedPayload = Base64.encodeBase64(payload.getBytes());

    String jwt = new String(encodedHeader) +
      "." +
      new String(encodedPayload) +
      "."; // json web token
    System.out.println("Paypal-Auth-Assertion=" + jwt);
  }
}
```

## 2. Create order [#2-create-order]

You must first [create an order](/api/orders/v2/orders-create) and capture funds. To create an order, copy the following code and modify as follows:

* Replace `BN-CODE` with your [PayPal attribution ID](/api/rest/requests/#paypal-partner-attribution-id)
* Replace `PAYPAL-AUTH-ASSERTION` with your PayPal auth assertion generated from Step 1.
* Use the `purchase_units/payee` object to specify the end receiver of the funds.
* Set the `purchase_units/payment_instruction/disbursement_mode` field to `DELAYED`.
* Use the `purchase_units/payment_instruction/platform_fees` array to specify fees for the order. You must onboard your seller with the [PARTNER\_FEE](/platforms/seller-onboarding/onboarding-checklist/#features) feature to use this array.

#### Sample request - cURL [#sample-request---curl]

```bash lineNumbers
curl -v -X POST https://api-m.sandbox.paypal.com/v2/checkout/orders
-H 'Content-Type: application/json' -H 'Authorization: Bearer ACCESS-TOKEN'
-H 'PayPal-Partner-Attribution-Id: BN-CODE'
-H 'PayPal-Auth-Assertion: PAYPAL-AUTH-ASSERTION'
-d '{
"intent": "CAPTURE",
"purchase_units": [
  {
    "amount": {
      "currency_code": "USD",
      "value": "100.00"
      },
        "payment_instruction": {
          "disbursement_mode": "INSTANT",
        "platform_fees": [
          {
            "amount": {
              "currency_code": "USD",
              "value": "25.00"
              }
          }
        ]
      }
    }
  ]
}
```

#### Sample request - Node [#sample-request---node]

```javascript lineNumbers
var express = require('express');
var request = require('request');
express().post('/my-server/create-order', function(req, res) {
	request.post('https://api-m.sandbox.paypal.com/v2/checkout/orders', {
		headers: {
			"Content-Type": "application/json",
			"Authorization": "Bearer ACCESS-TOKEN",
			"PayPal-Partner-Attribution-Id": "BN-CODE",
			"PayPal-Auth-Assertion": "PAYPAL-AUTH-ASSERTION"
		},
		body: {
			"intent": "CAPTURE",
			"purchase_units": [{
				"amount": {
					"currency_code": "USD",
					"value": "100.00"
				},
				"payment_instruction": {
					"disbursement_mode": "INSTANT",
					"platform_fees": [{
						"amount": {
							"currency_code": "USD",
							"value": "25.00"
						}
					}]
				}
			}],
		},
		json: true
	}, function(err, response, body) {
		if (err) {
			console.error(err);
			return res.sendStatus(500);
		}
		res.json({
			id: body.id
		});
	});
});
```

## 3. Capture order [#3-capture-order]

After your buyer approves the order, call [capture order](/api/orders/v2/orders-capture) to capture the buyer's funds.

Copy the following code and modify as follows:

* Replace `ACCESS-TOKEN` with your access token.
* Replace `BN-CODE` with your [PayPal partner attribution ID](/api/rest/requests/#paypal-partner-attribution-id).
* Replace `PAYPAL-AUTH-ASSERTION` with your PayPal auth assertion generated from Step 1.

#### Sample request - cURL [#sample-request---curl-1]

```javascript lineNumbers
curl -v -k -X POST https://api-m.paypal.com/v2/checkout/orders/5O190127TN364715T/capture
-H 'Content-Type: application/json'
-H 'Authorization: Bearer ACCESS-TOKEN'
-H 'PayPal-Partner-Attribution-Id: BN-CODE'
-H 'PayPal-Auth-Assertion: PAYPAL-AUTH-ASSERTION'
-d '{}'
```

#### Sample request - Node [#sample-request---node-1]

```javascript lineNumbers
var express = require('express');
var request = require('request');
express().post('/my-server/handle-approve/:id', function(req, res) {
	var OrderID = req.params.id;
	request.post('https://api-m.sandbox.paypal.com/v2/checkout/orders/' + OrderID + '/capture', {
		headers: {
			"Content-Type": "application/json",
			"Authorization": "Bearer ACCESS-TOKEN",
			"PayPal-Partner-Attribution-Id": "BN-CODE",
			"PayPal-Auth-Assertion": "PAYPAL-AUTH-ASSERTION"
		}
	}, function(err, response, body) {
		if (err) {
			console.error(err);
			return res.sendStatus(500);
		}
		res.json({
			status: 'success'
		});
	});
});
```

> **Info:** **Note:** Orders cannot be captured until the status of the order is set to APPROVED. The order status is set to APPROVED when the buyer successfully completes the [checkout](/platforms/checkout/) flow.

## 4. Show order details [#4-show-order-details]

To see your order details, pass the order ID as a path parameter in a [show order details](/api/orders/v2/orders-get) call.

Copy the following code and modify as follows:

* Replace `ACCESS-TOKEN` with your access token.
* Replace `BN-CODE` with your [PayPal partner attribution ID](/api/rest/requests/#paypal-partner-attribution-id).
* Replace `PAYPAL-AUTH-ASSERTION` with your PayPal auth assertion generated from Step 1.

#### Sample request - cURL [#sample-request---curl-2]

```javascript lineNumbers
curl -v -X GET https://api-m.sandbox.paypal.com/v2/checkout/orders/5O190127TN364715T
-H "Content-Type: application/json"
-H 'Authorization: Bearer ACCESS-TOKEN'
-H 'PayPal-Partner-Attribution-Id: BN-CODE'
-H 'PayPal-Auth-Assertion: PAYPAL-AUTH-ASSERTION'
```

#### Sample request - Node [#sample-request---node-2]

```javascript lineNumbers
var http = require("https");
var options = {
	"method": "GET",
	"hostname": "api-m.sandbox.paypal.com",
	"port": null,
	"path": "/v2/checkout/orders/5O190127TN364715T",
	"headers": {
		"Content-Type": "application/json",
		"Authorization": "Bearer ACCESS-TOKEN",
		"PayPal-Partner-Attribution-Id": "BN-CODE",
		"PayPal-Auth-Assertion": "PAYPAL-AUTH-ASSERTION"
	}
};
var req = http.request(options, function(res) {
	var chunks = [];
	res.on("data", function(chunk) {
		chunks.push(chunk);
	});
	res.on("end", function() {
		var body = Buffer.concat(chunks);
		console.log(body.toString());
	});
});
req.end();
```

## Next steps [#next-steps]

> **Info:** [Integration Checklist](/platforms/integration-checklist)
>
> Go through the integration checklist before you go live.
