how can i link an existing customer to a new checkout before submiting a transaction ?
//Replace your access token and location ID $accessToken = 'xxxxxxxxxxxx'; $locationId = 'yyyyyyyyyyyyy'; // Create and configure a new API client object $defaultApiConfig = new \SquareConnect\Configuration(); $defaultApiConfig->setAccessToken($accessToken); $defaultApiClient = new \SquareConnect\ApiClient($defaultApiConfig); $checkoutClient = new SquareConnect\Api\CheckoutApi($defaultApiClient); //Create a Money object to represent the price of the line item. $price = new \SquareConnect\Model\Money; $price->setAmount(20); $price->setCurrency('CAD'); //Create the line item and set details $book = new \SquareConnect\Model\CreateOrderRequestLineItem; $book->setName('The Shining'); $book->setQuantity('1'); $book->setBasePriceMoney($price); //Puts our line item object in an array called lineItems. $lineItems = array(); array_push($lineItems, $book); // Create an Order object using line items from above $order = new \SquareConnect\Model\CreateOrderRequest(); $order->setIdempotencyKey(uniqid()); //uniqid() generates a random string. //sets the lineItems array in the order object $order->setLineItems($lineItems); $checkout = new \SquareConnect\Model\CreateCheckoutRequest(); $checkout->setIdempotencyKey(uniqid()); //uniqid() generates a random string. $checkout->setRedirectUrl('http://expertshipping.ca/login'); //uniqid() generates a random string. $checkout->setOrder($order); //this is the order we created in the previous step try { $result = $checkoutClient->createCheckout( $locationId, $checkout ); //Save the checkout ID for verifying transactions $checkoutId = $result->getCheckout()->getId(); //Get the checkout URL that opens the checkout page. $checkoutUrl = $result->getCheckout()->getCheckoutPageUrl(); print_r( $checkoutUrl ); } catch (Exception $e) { echo 'Exception when calling CheckoutApi->createCheckout: ', $e->getMessage(), PHP_EOL; }
👋 @elouadifi. Unfortunately, at this time, there isn't a way to add an existing customer with a payment processed through the Checkout API. It’ll always create a new customer after each successful payment. But you can merge duplicate customers from your online Square Dashboard when this happens.
You can assign a customer to a an order through a transaction. But you have to do it at the time you process the order. Join the slack community it’s much more useful for API help. Tons of great developers over there too
heres an example https://github.com/square/connect-php-sdk/blob/master/docs/Model/ChargeRequest.md
are there recommended secure ways of authenticating a customer so that I can link them to the checkout order?
Talked with an API specialist. Here's a word from them:
The customer_id might appear on the Payment object, the Transaction object, or the Order object (or a combination of any) depending on how it was created. If the customer is not on the Order object, chances are it’s on the Transaction or Payment object.
If you can’t find it anywhere, provide the order_id and our API Team can dig to find out what it’s attached to.
@JustinC - thanks for your reply.
I took a closer look at the transaction fetch response -- the tenders array's single object has a customer_id after all.
@rrrecords Oh sweet! Let us know if you need anything else!
I see the new customer being created afterwards when searching via the square dashboard, but when the customer is redirected back to my site (redirect_url) after completing the checkout, I use the transactions API to get the transaction (I don't get an orderId as a query parameter), and from there I get the orderId, but after batch searching for the order, there's no customerId in it. why is that?
Square Community