PHP basic implementation

Hi guys,

If I retrieve an order and get the following response. (detailed underneath) Where ‘state’ is “COMPLETED”. Does this mean that the payment has gone through?
{
“order”: {
“id”: “VWliC8YXuB1hDNMKROr8YxB10aKZY”,
“location_id”: “LXHPD4390JST8”,
“line_items”: [
{
“uid”: “PPPoonjLPWOd5eQ9U0YBaD”,
“quantity”: “1”,
“name”: “Token Purchase”,
“base_price_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“gross_sales_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“total_tax_money”: {
“amount”: 0,
“currency”: “GBP”
},
“total_discount_money”: {
“amount”: 0,
“currency”: “GBP”
},
“total_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“variation_total_price_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“item_type”: “ITEM”,
“total_service_charge_money”: {
“amount”: 0,
“currency”: “GBP”
}
}
],
“created_at”: “2023-08-08T14:01:09.561Z”,
“updated_at”: “2023-08-08T14:01:43.000Z”,
“state”: “COMPLETED”,
“version”: 4,
“total_tax_money”: {
“amount”: 0,
“currency”: “GBP”
},
“total_discount_money”: {
“amount”: 0,
“currency”: “GBP”
},
“total_tip_money”: {
“amount”: 0,
“currency”: “GBP”
},
“total_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“closed_at”: “2023-08-08T14:01:43.090Z”,
“tenders”: [
{
“id”: “1ATYE253fm9jQ9IYmBaVwITrvaB”,
“location_id”: “LXHPD4390JST8”,
“transaction_id”: “VWliC8YXuB1hDNMKROr8YxB10aKZY”,
“created_at”: “2023-08-08T14:01:42Z”,
“note”: “Online Transaction”,
“amount_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“type”: “CARD”,
“card_details”: {
“status”: “CAPTURED”,
“card”: {
“card_brand”: “VISA”,
“last_4”: “1111”,
“fingerprint”: “sq-1-GxI7Y6M_WQZrkAVS_WaxCCnlq0lnSu3WB7qyA3lB9csE-uZ6BDOiWaeoET7qni5hxw”
},
“entry_method”: “KEYED”
}
}
],
“total_service_charge_money”: {
“amount”: 0,
“currency”: “GBP”
},
“net_amounts”: {
“total_money”: {
“amount”: 1000,
“currency”: “GBP”
},
“tax_money”: {
“amount”: 0,
“currency”: “GBP”
},
“discount_money”: {
“amount”: 0,
“currency”: “GBP”
},
“tip_money”: {
“amount”: 0,
“currency”: “GBP”
},
“service_charge_money”: {
“amount”: 0,
“currency”: “GBP”
}
},
“source”: {
“name”: “Sandbox for sq0idp-_wfRQ9iHCjtvmdLEZqFlEw”
},
“net_amount_due_money”: {
“amount”: 0,
“currency”: “GBP”
}
}
}

Cheers,
David

Yes, if a payment has a COMPLETED state that means that the payment was successful. :slightly_smiling_face:

Yep But I was only initially able to gain access to the order object. However, there are two parts to a transactions action, an Order and a Payment.

When the order is fully paid, then the Order state will change from OPEN to COMPLETED. That does indirectly tell us the payment(s) has gone through.

You can take the tenders id, which is a payment id to directly check on the payment with Get Payment. If it has a status of COMPLETED, then the payment has gone through.

Yes, that’s correct however orders wont always have a status of completed. Some paid orders may have a status of OPEN and there paid for with a fulfillment. To be sure you’ll want to check for the payment_id or tender_id of an order. :slightly_smiling_face:

Just quickly, can you tell me the difference between a payment status of ‘APPROVED’ as opposed to a payment status of ‘COMPLETED’ please.

An APPROVED payment is in an authorized state which can be canceled or voided. I completed payment means that the funds have been captured and will be sent to the customers bank. :slightly_smiling_face:

Hi Brian

Prior to the fallover, I would create an Order Request and use that to create a checkout Request, move through the motions until I had the checkout page URL and send the client through to that. However I’m now being told that that is deprecated and it’s not working. Is it deprecated or been removed entirely. If it is, should I be using CreatePaymentLinkRequest?

Yes, CreateCheckout is deprecated and we definitely recommend moving to the supported CreatePaymentLink. :slight_smile:

OK here we go…
I’ve followed the example given on the developer site which is as follows:
$amount = floatval($_POST[‘amount’]);
$price_money = new \Square\Models\Money();
$price_money->setAmount(round($amount * 100));
$price_money->setCurrency(‘GBP’);

                    $quick_pay = new \Square\Models\QuickPay(
                        'BFlush Tokens',
                        $price_money,
                        $transaction_id
                    );

                    $body = new \Square\Models\CreatePaymentLinkRequest();
                    $body->setIdempotencyKey($transaction_id);
                    $body->setQuickPay($quick_pay);
                    
                    $redirect_url = 'https://www.bflush.com/members_area/buy_tokens.html';
                    $body->setRedirectUrl($redirect_url);

                    $response = $client->getCheckoutApi()->createPaymentLink($body);
                    header('Location: '.$response);

but this is incorrect as I need to set the redirect URL in the checkout options. How do I do this though as there’s no direct setRedirectURL method. I don’t know how to talk to the checkout options.
Can you help please.

This is what you’ll need to set:

$price_money = new \Square\Models\Money();
$price_money->setAmount(100);
$price_money->setCurrency('USD');

$quick_pay = new \Square\Models\QuickPay(
    null,
    $price_money,
    '{{location_id}}'
);

$checkout_options = new \Square\Models\CheckoutOptions();
$checkout_options->setRedirectUrl('YOUR_REDIRECT_URL');

$body = new \Square\Models\CreatePaymentLinkRequest();
$body->setIdempotencyKey('9466543d-067b-4c72-abbb-9e5d4b6bd9fa');
$body->setQuickPay($quick_pay);
$body->setCheckoutOptions($checkout_options);

$api_response = $client->getCheckoutApi()->createPaymentLink($body);

if ($api_response->isSuccess()) {
    $result = $api_response->getResult();
} else {
    $errors = $api_response->getErrors();
}

You can check it out in our API Explorer :slight_smile:

Cheers Brian.

That’s sorted however how do I test the response. It’s taking me through to sand box confirmation pages but what I’d really like is to be able to see the response that comes in and test it appropriately as it was before.

Also I managed to figure out that you need to getPaymentLink() and then getUrl() but where is the documentation for this?

Your API explorer doesn’t detail any methods. Where as a rookie programmer would I find trhe method description?

Glad to hear that worked.

We don’t currently have documentation on what to do with the response since that’s entirely up to you and your business need. Are you looking to redirect the customer to that link or are you using some mechanism to send the link to a customer. :slight_smile:

Hi Brian,
I really would like to be able to test. Let me give you the client story and you can comment from there. It seems a fairly standard real world scenario that others would be implementing.
Bflush i a token gaming site.

  1. The customer logs on and goes to the ‘Obtain Tokens’ page and is presented with several to buy a set amount of tokens.
  2. He clicks on one of the options. Let’s say 1 million tokens for £10 (We are UK based) and gets sent to the payment options page where chooses 'Square Payments.
  3. The amounts and transaction details are prepared and he is redirected to the Square payments page.
  4. He completes the transaction and is sent back to the return URL with the transaction ID.
  5. the Bflush system then queries Square via the 'getOrders() API->getBody.
  6. if the status is ‘COMPLETED’ or ‘APPROVED’ then he is credited with the tokens other wise he is not.

My question is: Has the getOrders method changed and if it has, how would I test it?

Cheers,
David

Hi David,

If the customer is redirected back to your site and you get the order_id in the URL then the payment was successful. We won’t redirect the customer till there is a successful payment. Once you get the order_id in the URL you’ll call RetrieveOrder to further confirm that the payment is COMPLETED. :slight_smile:

Hi Brian,

We have a loyalty program for our customers. They pay in to use our services but some of them earn a commission by attracting other clients for us, giving them commission. I want to be able to pay them directly into the card they used.
So, when taking a payment, do I need to capture their card details (or a reference to their card details which should really only be held by Square) and then use that reference when making a payment back to them. What is the process and which apis should I be using?
Are there any examples using PHP?

Sorry to have to ask but I’m a bit lost to tell the truth.

Yes I am a total newbie…

Cheers,
David

Currently, the ability to pay out to a specific card isn’t currently available with our APIs. 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:

Hi Brian,

So how would I be able to pay someone their commission?

There must be things like charge backs. It wouldn’t matter whether it was their card or the account linked to that card.

Are there any alternatives?

Cheers,
David

Since we don’t have the ability to payout to cards in this scenario you’ll need to find other means of sending the funds as a commission outside of Square’s APIs. :slightly_smiling_face: