I have a Wordpress site with a custom-built plugin. Originally it was set up to take payments via Paypal - you selected your products, aded them to the cart, filled in your shipping and billing information, your total was calculated and then you and hit pay. It worked well. What I'd like to do is to keep everything as is and replace PayPal with Square as a payment method/gateway. I don't need inventory management or invoicing, just the ability for customers to pay through Square. Products are dynamic and prices vary depending on product configuration so a simple payment link is not going to work. Any ideas?
Square's quick-pay payment links fit this well, and you keep your whole cart as it is.
When the customer clicks Pay, have your plugin call Square's Checkout API from the server (CreatePaymentLink) with a quick_pay object: a name like "Order 1234", the total your plugin already calculated in cents (so $42.50 is 4250), and your location ID. Square returns a checkout URL; send the customer there and they pay on Square's hosted page. Set a redirect URL in the checkout options so they land back on your thank-you page, and confirm the payment through a webhook or the order before you mark it paid, rather than trusting the redirect alone.
If you'd rather keep the card form inside your own checkout page, Square's Web Payments SDK does that: it turns the card into a one-time token in the browser, and your server charges it through the Payments API. More work, but no redirect.
Either way, you create an application on developer.squareup.com to get the access token and location ID, and test in the sandbox first.
Does your plugin calculate the final total, including shipping and tax, before the customer clicks Pay?
Thank you very much, that's very helpful. Yes, the plugin calculates the final total including shipping and tax before you click Pay.
Good, that makes it simpler: your plugin already has the items and the total, so you can hand Square the whole cart instead of one lump sum.
When the customer clicks Pay, have the plugin call CreatePaymentLink from your server with an order object instead of quick_pay. Add each configured product as an ad hoc line item (name, quantity, and base_price_money with the price your plugin worked out, in cents), then shipping and tax as their own lines, so Square's total matches your cart to the cent and the receipt shows what they bought. (If you'd rather Square's reports show tax as tax, add it to the order as a percentage tax instead of a line.) Put the email and address you already collected in pre_populated_data so they don't type them twice.
Square sends back payment_link.url and payment_link.order_id. Save the order_id against your plugin's order, then send the customer to the url.
Mark the order paid only from the webhook: subscribe to payment.updated, and when a payment for that order_id shows COMPLETED, flip your order to paid. Check the x-square-hmacsha256-signature header first (an HMAC-SHA256 of your notification URL plus the raw request body, made with your webhook signature key, compared with hash_equals in PHP), because anyone can post to that URL.
Is the plugin your own code, or did a developer build it for you?
Square Community
Square Products