> ## Documentation Index
> Fetch the complete documentation index at: https://wpay-feat-edp-guide.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# PayPal via the API

To handle PayPal via the API directly, follow our [quick start guide](/guides/payments/direct-api/quick-start) on
direct API integrations, or use the steps below.

## Create a transactions

To create a PayPal transaction, set the `method` to `paypal` and the `redirect_url` to the endpoint of your application that can handle the customer returning once they've completed approving the transaction on `paypal.com`.

See the [`POST /transactions`](/reference/transactions/new-transaction) API endpoint for more details.

<CodeGroup>
  ```js cURL
  curl -i -X POST "https://api.example.gr4vy.app/transactions" \
      -H "Authorization: Bearer [JWT_TOKEN]" \
      -d '{
    "amount": 1400,
    "currency": "AUD",
    "intent": "capture",
    "payment_method": {
      "method": "paypal",
      "redirect_url": "https://example.com/return-url",
      "country": "AU",
      "currency": "AUD"
    },
    ...
  }'
  ```

  ```js Node
  const transactionRequest = new TransactionRequest()
  transactionRequest.amount = 1400
  transactionRequest.currency = 'AUD'
  transactionRequest.paymentMethod = new TransactionPaymentMethodRequest()
  transactionRequest.paymentMethod.method = 'paypal'
  transactionRequest.paymentMethod.redirectUrl = 'https://example.com/return-url'
  transactionRequest.paymentMethod.country = 'AU'
  transactionRequest.paymentMethod.currency = 'AUD'

  const transaction = await client.newTransaction("", transactionRequest)
  ```

  ```python Python
  transaction_request = {
      "amount":1299,
      "currency":"AUD",
      "intent":"authorize",
      "payment_method": {
          "method":"paypal",
          "redirect_url":"https://example.com/return-url",
          "currency":"AUD",
          "country":"AU"
      },
  }
  transaction = client.create_new_transaction(**transaction_request)
  ```

  ```php PHP
  $transaction_request = array("amount"=>1000,"currency"=>"AUD", "payment_method"=>array("method"=>"paypal", "redirect_url"=>"https://example.com/return-url", "country"=> "AU", "currency"=>"AUD"));

  $result = $config->authorizeNewTransaction($transaction_request);
  ```
</CodeGroup>

If successful, the response of this transaction will include a `status` set to `buyer_approval_pending` as well as a `payment_method.approval_url`.

```json
{
    "type": "transaction",
    "id": "0c41c8df-27f4-480e-97f0-9401558ae25e",
    "status": "buyer_approval_pending",
    "intent": "authorize",
    "payment_method": {
        "type": "payment-method",
        "method": "paypal",
        "mode": "redirect",
        "approval_url": "https://www.sandbox.paypal.com/checkoutnow?token=7NP38594266148058",
       ...
    },
    "method": "paypal",
    ...
}
```

## About tokenization

If your PayPal account supports **Reference Transactions**, then you can pass the `store=true` parameter to each request to vault a buyer's PayPal account for future use.

This does require you to tick the **Payment tokenization** toggle for your PayPal connection in your dashboard.

<Frame>
  ![Tokenization toggle](https://mintlify.s3-us-west-1.amazonaws.com/wpay-feat-edp-guide/assets/images/paypal/tokenization-toggle.png)
</Frame>

Once stored, you can use the stored PayPal account for future transactions without redirecting the buyer to PayPal.

## Handle redirect to PayPal

Your application will need to redirect your customer to PayPal where they will be required to authenticate the payment. To do so, redirect the customer to the `payment_method.approval_url`. After they've authenticated themselves, the customer will be redirected back to the `redirect_url` that you set when creating the transaction.

<Warning>
  If the customer abandons the checkout or somehow experiences network connection issues, the transaction state can get out of sync between your application and ours.  We recommend [the following best practices](/guides/best-practice) in handling these situations.
</Warning>

## Handle the return to your app

When the customer is redirected back to your app, the transaction status is not known. Your application will therefore need to call our API to get the latest transaction status. To do this, your `redirect_url` will be appended with the `gr4vy_transaction_id`.

```
[redirect_url]?gr4vy_transaction_id=2f37e0d0-5549-42c4-9c5c-e03d5fa97148&gr4vy_transaction_status=capture_succeeded
```

<Warning>
  Although we also provide the `gr4vy_transaction_status` in this callback, it's recommended to also fetch the latest status via the API, as the status may have changed since.
</Warning>

After you've handled the redirect, you can display a message to your customer letting them know the result of the transaction.

## Fetch the latest status

Finally, after the transaction has been created your application can get the
latest details and status using the transaction ID received via the callback to
the redirect URL, or on direct callback from the API.

<CodeGroup>
  ```js cURL
  curl -i -X GET "https://api.example.gr4vy.app/transactions/fe26475d-ec3e-4884-9553-f7356683f7f9" \
      -H "Authorization: Bearer [JWT_TOKEN]"
  ```

  ```js Node
  const response = await client.getTransaction(transaction_id);
  console.log(response.data);
  ```

  ```python Python
  response = client.GetTransaction(transaction_id)
  print(response)
  ```

  ```php PHP
  $result = $apiInstance->getTransaction($transaction_id);
  echo $result;
  ```
</CodeGroup>

The transaction includes details about the payment method used and the status
of the transaction.

```json
{
  "type": "transaction",
  "id": "fe26475d-ec3e-4884-9553-f7356683f7f9",
  "status": "authorized",
  "amount": 1299,
  "currency": "AUD",
  "payment_method": {
    "type": "payment-method",
    "id": "77a76f7e-d2de-4bbc-ada9-d6a0015e6bd5",
    "method": "paypal",
    ...
  },
  ...
}
```

<Info>
  Visit the [API reference documentation](/reference) for full details about the
  transaction resource, and any other API.
</Info>

## Webhooks

In order to receive timely updates regarding the status of your PayPal transactions please set up a PayPal webhook URL. In
the [PayPal Merchant Dashboard](https://www.paypal.com/signin) create a webhook with that URL. Once set up you will be given a
webhook ID by PayPal, provide this back to the team.

## FraudNet

FraudNet is a PayPal-developed, JavaScript library that collects browser-based data to help reduce fraud. Upon checkout, the FraudNet library sends data elements to PayPal Risk Services for fraud and risk assessment.

When creating transactions using PayPal Billing Agreements, the PayPal FraudNet library must be included on the checkout page for all transactions. When using Embed, the PayPal FraudNet library is included automatically. If you are using Direct API directly, you will need to use the [device fingerprinting library](/guides/features/anti-fraud/fingerprint) which includes the PayPal FraudNet library.
