Skip to main content

PayPal integration example

This tutorial shows how to integrate PayPal and introduces new nodes.

1. Tracking the order created event​

1.1. The "order created" event in the "Events" module​

To track events in the "Integrations" module, create an event in the "Events" module.

Learn more about the Events block.

(opens in a new tab)

1.2. Tracking the order created event​

  • Drag the "events" node onto the workspace.
  • Change the value in the "Set" field to "orderEvent".
  • The event is saved to the "msg.orderEvent" object, so you can use it in other nodes.
  • Select the "order created" event you created from the list and click "submit".

This subscribes to the order creation event and saves the event to "msg.orderEvent" for later use.

Learn more about the Events node.

(opens in a new tab)

2. Creating a payment session​

2.1. Create a payment account

Before you start the integration, create a custom account for PayPal.

Learn how to create one.

(opens in a new tab)

2.2. Create a payment session

  • Drag the "admin-api" node onto the workspace and connect it to the node from the previous step.
  • Select the "Creating a payment session" method and paste the code below into the "Request body" field.
{
"orderId": {{orderEvent.order.id}},
"type": "session"
}

This takes the order ID from the "msg.orderEvent" object and builds a request to create a payment session. The request takes two parameters: "orderId", the order ID, and "type", the payment session type (session or intent).

Learn more about the admin-api node.

(opens in a new tab)

2.3. Set the returned ID in msg.paymentSessionId

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This sets "msg.paymentSessionId" to the payment session ID returned by the previous node.

msg.paymentSessionId = msg.payload.id

return msg

Learn more about the Function node.

(opens in a new tab)

3. Authenticating with PayPal​

3.1. Set the PayPal authentication parameters

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This sets the "payload" and "headers" fields of the "msg" object. The next node needs these values.

// set the request body
msg.payload = {
grant_type: 'client_credentials',
}
// set the headers
msg.headers = {
'Content-Type': 'application/x-www-form-urlencoded',
Accept: 'application/json',
}

return msg

For details on the payment parameters, see the PayPal documentation.

Learn more about the Function node.

(opens in a new tab)

3.2. Request an access token

  • Drag the "http request" node onto the workspace and connect it to the node from the previous step.
  • In the Method field, select "POST".
  • In the Data field, select "Send as request body".
  • In the Link field, enter https://api-m.sandbox.paypal.com/v1/oauth2/token.
  • In the Return field, select "Object JSON".
  • Select "Use Authentication".
  • Enter your PayPal client ID in the "Username" field and your PayPal client secret in the "Password" field.

This request returns the "access_token".

For details on authentication, see the PayPal documentation.

Learn more about the http request node.

(opens in a new tab)

3.3. Set the parameters for creating a payment link

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This builds a request to create a payment link from the data returned by the previous nodes.

// PayPal access_token, obtained in the "paypal auth" node
const accessToken = msg.payload.access_token
// order data, obtained in the "order created" node
// the order object matches the order from the orders API https://oneentry.cloud/instructions/api
const order = msg.orderEvent.order
// payment session ID, obtained in the "create payment session" node
const paymentSessionId = msg.paymentSessionId

// build the PayPal data from the order data
const items = order.products.map((product) => {
return {
name: product.title,
description: 'your description',
quantity: String(product.quantity),
unit_amount: {
currency_code: order.currency,
value: product.price,
},
}
})

const totalSum = parseFloat(order.totalSum)

const data = {
intent: 'CAPTURE',
purchase_units: [
{
items,
custom_id: String(paymentSessionId),
amount: {
currency_code: order.currency,
value: totalSum,
breakdown: {
item_total: {
currency_code: order.currency,
value: totalSum,
},
},
},
payment_instruction: {
billingId: String(order.id),
},
},
],
payment_source: {
paypal: {
experience_context: {
payment_method_preference: 'UNRESTRICTED',
payment_method_selected: 'PAYPAL',
brand_name: 'EXAMPLE INC',
locale: 'en-US',
landing_page: 'LOGIN',
shipping_preference: 'NO_SHIPPING',
user_action: 'PAY_NOW',
return_url: 'https://example.com/return',
cancel_url: 'https://example.com/cancel',
},
},
},
}

// Request body
msg.payload = data
// Request headers
msg.headers = {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
Prefer: 'return=representation',
}

return msg

For details, see the PayPal documentation.

Learn more about the Function node.

(opens in a new tab)

This step uses the "http request" node, which sends HTTP requests.

  • Drag the "http request" node onto the workspace and connect it to the node from the previous step.
  • In the Method field, select "POST".
  • In the Data field, select "Send as request body".
  • In the Link field, enter https://api-m.sandbox.paypal.com/v2/checkout/orders.
  • In the Return field, select "Object JSON".

This sends a request to PayPal to create a payment link with the data from the previous node.

For details, see the PayPal documentation.

Learn more about the http request node.

(opens in a new tab)

5. Updating the payment session​

  • Drag the "admin-api" node onto the workspace and connect it to the node from the previous step.
  • Select the "Payment session update" method.
  • Paste the code below into the "Query parameters" field.
{
"id": {{paymentSessionId}}
}
  • Paste the code below into the "Request body" field.
{
"paymentUrl": "{{{payload.links.1.href}}}"
}

This takes the payment link ID "msg.paymentSessionId" and the payment link "msg.payload.links[0].href" from the previous requests.

For details on the returned values, see the PayPal documentation.

Learn more about the admin-api node.

(opens in a new tab)

6.1. Set the "createPaymentLink" topic

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

The topic identifies the current flow.

msg.topic = 'createPaymentLink'

return msg

Learn more about the Function node.

(opens in a new tab)

6.2. Create an endpoint to get the payment link

  • Drag the "http in" node onto the workspace.
  • Select the "POST" method.
  • Enter the link /get-payment-link.

You will use this endpoint to get the current payment link.

Learn more about the http in node.

(opens in a new tab)

6.3. Set the "getPaymentLink" topic

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

The topic identifies the current flow.

msg.topic = 'getPaymentLink'

return msg

Learn more about the Function node.

(opens in a new tab)

6.4. Save the data to the context

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This node builds the response.

// get current order id
const orderId = msg.payload.orderId
// current context key
const contextKey = `payment_link_${orderId}`
// get OneEntry api
const api = global.get('api')
// current context data
const contextData = flow.get(contextKey) || {}
// set data to context by topic
if (msg.topic === 'createPaymentLink') {
contextData.paymentSessionId = msg.paymentSessionId
contextData.createPaymentLinkCompleted = true
}
if (msg.topic === 'getPaymentLink') {
contextData.getPaymentLinkCompleted = true
contextData.res = msg.res
}
// set context data to context
flow.set(contextKey, contextData)
// if createPaymentLink and getPaymentLink completed, send response with current session
if (
contextData.createPaymentLinkCompleted &&
contextData.getPaymentLinkCompleted
) {
// get payment session by id from api
const paymentSession = await api.getPaymentSessionById({
id: contextData.paymentSessionId,
})
msg.payload = paymentSession
msg.res = contextData.res

// clear api
flow.set(contextKey, null)

return msg
}

return null

Learn more about the Function node.

(opens in a new tab)

6.5. Output point for the payment link

  • Drag the "http response" node onto the workspace and connect it to the node from the previous step.

This node returns the response to the request.

Learn more about the http response node.

(opens in a new tab)

7. Setting up PayPal webhooks​

7.1. Input point for PayPal

  • Drag the "http in" node onto the workspace.
  • Select the "POST" method.
  • Enter the link /paypal.

You will use this endpoint to update the payment status through PayPal.

Learn more about the http in node.

(opens in a new tab)

7.2. Response to the request

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This node builds the response.

msg.payload = {
ok: true,
}

return msg

Learn more about the function node.

(opens in a new tab)

7.3. Output point for PayPal

  • Drag the "http response" node onto the workspace and connect it to the node from the previous step.

This node returns the response to the request.

Learn more about the http response node.

(opens in a new tab)

7.4. Get the payment status

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This node takes the value received from the "paypal webhook" node and writes it to "msg.status" for use in the next node.

msg.status = msg.payload.resource.status

return msg

Learn more about the Function node.

(opens in a new tab)

7.5. Check the payment status

  • Drag the "switch" node onto the workspace and connect it to the node from the previous step.
  • In "Property", enter "status".
  • Add the first rule. Select the "==" condition and enter "APPROVED" as the value.
  • Add the second rule. Select the "else" condition.

This node checks the value of "msg.status" against the rules. If the value is "APPROVED", the nodes in the first branch run; otherwise, the nodes in the second branch run.

Learn more about the switch node.

(opens in a new tab)

7.6. Get the payment session ID

  • Drag the "function" node onto the workspace and connect it to the node from the previous step.
  • Paste the code from the example into the "Function" field.

This node takes the payment session ID saved in the "set payment data" node.

msg.paymentSessionId = parseInt(
msg.payload.resource.purchase_units[0].custom_id,
10
)

return msg

For details on the returned values, see the PayPal documentation.

Learn more about the Function node.

(opens in a new tab)

7.7. Update the payment status

  • Drag the "admin-api" node onto the workspace and connect it to the node from the previous step.
  • Select the "Payment session update" method.
  • Paste the code below into the "Query parameters" field.
{
"id": {{paymentSessionId}}
}
  • Paste the code below into the "Request body" field.
{
"status": "completed"
}

This updates the payment status using the ID from "msg.paymentSessionId".

Learn more about the admin-api node.

(opens in a new tab)

7.8. Add a webhook in your PayPal account

Use your project URL + /api/admin/workflows/endpoints/paypal.

Example:

https://<your-project>.oneentry.cloud/api/admin/workflows/endpoints/paypal

For details, see the PayPal documentation.

8. Testing the integration​

8.1. Create an order

Postman: POST request to orders-storage/marker/order_storage/orders with Authorization and x-app-token headers, response &quot;201 Created&quot; with order id 15

&quot;Orders&quot; section, &quot;order storage&quot;: order No. 15 for 2.00 usd paid via Paypal with status &quot;order created&quot;

To learn how to create an order, see the API documentation.

8.2. Get the payment link

Postman: POST request to workflows/endpoints/get-payment-link with body orderId 15, response &quot;200 OK&quot; with status waiting and a PayPal paymentUrl

  • Send a POST request to https://<your-project>.oneentry.cloud/api/admin/workflows/endpoints/get-payment-link, replacing https://<your-project>.oneentry.cloud with your project URL.

  • In the request body, specify the ID of the order you created:

{
"orderId": 15
}

8.3. Pay for the order

PayPal checkout page for $2.00: &quot;PayPal balance&quot; selected as payment method, &quot;Complete Purchase&quot; button at the bottom

8.4. Check the order status

&quot;Orders&quot; section, &quot;order storage&quot;: order No. 15 now has status &quot;order completed&quot;, with a lock icon in the &quot;Actions&quot; column