I am selling a single item from an email driven workflow; once the user commits to buying, I generate a payment link, and send it to them. Initially I was using QuickPay, but I couldn’t find a way to create coupons with it, so I (after some trial and error) plugged in the item’s (variation) id to the link builder. What I have done though seems pretty brittle, and there must be a better way. Right?
I initially thought I’d be able to use the item id from the item’s url in the seller dashboard, but that didn’t work. So, after some experimentation, I managed to find the item via the api and dump it out:
val items = client.catalogApi.searchCatalogObjects(
SearchCatalogObjectsRequest.Builder()
.objectTypes(listOf("ITEM"))
.build())
val item = items.objects.first()
logger.info("$item")
Examining the item in the log, I saw that there was a single variation listed, which was also a CatalogObject, and which had its own id; I plugged that id into the link builder, and it worked.
val lineItem = OrderLineItem.Builder("1")
.catalogObjectId("S_hardcoded_6")
.build()
val link = checkoutApi.createPaymentLink(
CreatePaymentLinkRequest.Builder()
.order(Order.Builder(configuration.square.location)
.lineItems(listOf(lineItem))
.build())
.prePopulatedData(PrePopulatedData(email, null, null))
.build()
)
So here’s the question: is hardcoding the variation id safe? Will it change if I edit it? (I do not plan to add different types).
I can also get at the id with item.itemData.variations.first().id
. Will there always be one variation?
Or is there generally a better approach where I can search for the item more directly and plug it directly into the CreatePaymentLinkRequest?