I have the code below that I am using to build out an ecommerce website. Fulfillment Recipient and Address are hard coded for testing purposes.
The payment link is created successfully and does show up in the Transactions section of the Square Dashboard. Problem is that the order does not show up in the Orders section of the Square Dashboard. Please advise what may be wrong and why is the order information not showing up on the Dashboard?
Also, if the user enters their shipping information on the payment link page, is it not added to the order?
$client = new SquareClient([
'accessToken' => 'REDACTED',
// 'accessToken' => getenv('SQUARE_ACCESS_TOKEN'),
'environment' => Environment::SANDBOX,
]);
$locationId = "REDACTED";
$postCheckoutUrl = 'http://127.0.0.1:8080/c2.php';
$merchantEmail = "[email protected]";
$json = $_POST['cartContents'];
$cartContents = json_decode($json);
$lineItems = array();
foreach ($cartContents as $item) {
$basePriceMoney = new \Square\Models\Money();
$amount = $item->price * 100;
$basePriceMoney->setAmount($amount);
$basePriceMoney->setCurrency('USD');
$orderLineItem = new \Square\Models\OrderLineItem($item->quantity);
$orderLineItem->setUid($item->id);
$orderLineItem->setName($item->name);
$orderLineItem->setNote($item->description);
$orderLineItem->setItemType(OrderLineItemItemType::ITEM);
$orderLineItem->setBasePriceMoney($basePriceMoney);
array_push($lineItems, $orderLineItem);
}
$fulfillmentAddress = new \Square\Models\Address();
$fulfillmentAddress->setAddressLine1("123 Main Street");
$fulfillmentAddress->setLocality("Anywhere");
$fulfillmentAddress->setAdministrativeDistrictLevel1("Alabama");
$fulfillmentAddress->setPostalCode("44234");
$fulfillmentAddress->setCountry("US");
$fulfillmentRecipient = new FulfillmentRecipient();
$fulfillmentRecipient->setDisplayName("John Doe");
$fulfillmentRecipient->setAddress($fulfillmentAddress);
$fulfillmentRecipient->setPhoneNumber("555-555-5555");
$shipment_details = new FulfillmentShipmentDetails();
$shipment_details->setRecipient($fulfillmentRecipient);
$fulfillment = new Fulfillment();
$fulfillment->setType(OrderFulfillmentType::SHIPMENT);
$fulfillment->setShipmentDetails($shipment_details);
$fulfillment->setState(OrderFulfillmentState::PROPOSED);
$fulfillments = [$fulfillment];
$orderSource = new OrderSource();
$orderSource->setName("website");
$order = new \Square\Models\Order($locationId);
$order->setLineItems($lineItems);
$order->setFulfillments($fulfillments);
$order->setSource($orderSource);
$checkout_options = new \Square\Models\CheckoutOptions();
$checkout_options->setAllowTipping(false);
$checkout_options->setRedirectUrl($postCheckoutUrl);
$checkout_options->setAskForShippingAddress(true);
$checkout_options->setEnableCoupon(true);
$checkout_options->setMerchantSupportEmail($merchantEmail);
$checkout_options->setEnableCoupon(true);
$paymentLink = new \Square\Models\CreatePaymentLinkRequest();
$idempotencyKey = uniqid();
$paymentLink->setIdempotencyKey($idempotencyKey);
$paymentLink->setOrder($order);
$paymentLink->setCheckoutOptions($checkout_options);
$createPaymentLinkResponse = $client->getCheckoutApi()->createPaymentLink($paymentLink);
if ($createPaymentLinkResponse->isSuccess()) {
$result = $createPaymentLinkResponse->getResult();
header("Location: " . $result->getPaymentLink()->getUrl());
} else {
$errors = $createPaymentLinkResponse->getErrors();
echo "Unexpected error occurred";
error_log(print_r($errors));
}
exit();