CreatePaymentLink, CreateOrder, and Dynamic Number of Line Items

Hi, Square Community:

I am new to building web apps and working with APIs, so this project has been a steep learning curve.

I am testing the APIs in API Explorer before I implement them in my own app. I have been successful at adding CreatePaymentLink to my app, but I am only able to include one line item when the number of line items is supposed to be dynamic.

My understanding is CreatePaymentLink includes an itemized list of the ordered products, so I do not need CreateOrder, correct? And I just found out I can manually add line items in CreatePaymentLink in API Explorer. Yet, I’m looking at CreateOrder that defines a line item ad hoc. Does that mean I need CreateOrder to add line items? And how to I make that dynamic?

Thank you.

Welcome! With the Payment links you’ll build the itemized order in the request to create the link. When the link is created we will call the Orders API to create the order and return it in the response. Currently you can’t add an order created with the Orders API to a payment link. :slightly_smiling_face:

Then I am having trouble making the number of line items dynamic.

How so? You’ll just need to add an additional line item to the CreatePaymentLink request. For example:

{
    "checkout_options": {
      "redirect_url": "https://www.myRedirectURL.com",
      "accepted_payment_methods": {
        "afterpay_clearpay": true,
        "cash_app_pay": true,
        "google_pay": true,
        "apple_pay": true
      },
      "allow_tipping": true
    },
    "idempotency_key": "3e7aa4f4-d325-4245-9838-489c136967c9",
    "pre_populated_data": {
      "buyer_address": {
        "first_name": "Bryan",
        "last_name": "Wiseman"
      },
      "buyer_email": "[email protected]",
      "buyer_phone_number": "4152128888"
    },
    "quick_pay": {
      "location_id": "{{location_id}}",
      "name": "name test",
      "price_money": {
        "amount": 100,
        "currency": "USD"
      }
    },
    "order": {
      "location_id": "{{location_id}}",
      "line_items": [
        {
          "quantity": "1",
          "base_price_money": {
            "amount": 100,
            "currency": "USD"
          },
          "item_type": "ITEM",
          "name": "item_1"
        },
        {
          "quantity": "1",
          "base_price_money": {
            "amount": 100,
            "currency": "USD"
          },
          "name": "item_2",
          "item_type": "ITEM"
        }
      ]
    }
  }'

add an additional line item

I am able to do so manually on API Explorer, but I can’t figure out how to do that when the number of items can vary depending on the customer.

Since each link is going to be unique for each customer you’ll pass in the number of item in the order for each API call. :slightly_smiling_face:

@Bryan-Square
Finally got it working.

I have another quick question. I noticed that since I am testing in the sandbox, Square’s checkout preview page does not allow me to simulate putting in credit card info and pretend to pay. Should I not worry about this working since it is Square-hosted?

Right, the sandbox won’t let you input data. There’s currently no way of testing this in sandbox at this time. We’re constantly working to improve our features based on feedback like this, so I’ll be sure to share your request to the API product team. :slightly_smiling_face: