Before we get to the token the form is asking for a ZIP code; I have a postcode. How do I control this? Is there such a thing like javadoc for the script?
Yes the payment token is received, the post body is like:
{"locationId":"TC4Z39DBKWXKH","sourceId":"cnon:CA4HHFG4SnoMXDeDO0IPEOqGnEsYASgC","idempotencyKey":"bc35f7ac-356e-4f49-8521-f6dbd2171a10"}
Straight away I have 2 questions
How do I check the token for validity?
CreatePaymentRequest
.builder()
.sourceId(sourceId)
is not listed as throwing an exception which might be the answer.
How do I send back failure? This is answered because I write the script that received the response albeit I copied it from the example so I can rewrite this as I want but I a struggling to see why I should have to rewrite the example, typically this indicates I have misunderstood. Taking a step backwards I realise I am writing a script to run client side to process data I send to the client, then I ask the client to send back the answer. RMI. Why do I not calculate the token myself? The only explanation I have is someone thinks the client script only needs writing once whereas several SDKs are needed depending on the server. However it does mean the script needs checking on all current web browsers. Personally I’d rather do it server side and retain control. Alternative explanations include it is doing something opaque.
My attitude is tainted by how easy it was to take a payment 20 years ago. We had to construct our own posts but it did mean it was completely clear when the server communicated with the payment system. Such communication is now opaque being buried in an SDK call.
Back to the present day, I attempted to construct a payment request:
CreatePaymentResponse createPaymentResponse = squareClient.payments().create(
CreatePaymentRequest
.builder()
.sourceId(sourceId)
.idempotencyKey(idempotencyKey)
.amountMoney(
Money
.builder()
.amount((long) order.price())
.currency(Currency.GBP)
.build()
)
.customerId(order.person().squareid())
.locationId(company.square_location_id())
.build()
);
but I get an exception
StandardWrapperValve[xx.xxxx.xxxx.xxxx.ServletSquarePayment]: Servlet.service() for servlet xx.xxxx.xxxx.xxxx.ServletSquarePayment threw exception
SquareClientApiException{message: Error with status code 400, statusCode: 400, body: {errors=[{code=BAD_REQUEST, detail=Not authorized to take payments with location_id=TC4ZZ3KKKRXXXRH, field=location_id, category=INVALID_REQUEST_ERROR}]}}
at com.squareup.square.RawPaymentsClient.create(RawPaymentsClient.java:257)
at com.squareup.square.RawPaymentsClient.create(RawPaymentsClient.java:208)
at com.squareup.square.PaymentsClient.create(PaymentsClient.java:91)
the locationid is correct and for a sandbox so what authorisation does it need?