Python - unable to use Payments API CreatePayment endpoint

Trying to use square python sdk & following docs found here. From the section heading Take a payment using a secure token (nonce) having an issue with first line of the code block (see screenshot below)

For some reason/s I’m getting the following error in the terminal

result = client.payments.create_payment(
AttributeError: 'WebApplicationClient' object has no attribute 'payments'`
127.0.0.1 - - [29/Oct/2020 21:27:50] "GET /squarer?nonce=cnon%3ACBASEHw5xMniY32nCvrpMkr8mhU&totalamount=10 HTTP/1.1" 500 -

I don’t know why I’m getting the error as I’ve simply copied & pasted the code from the docs. I did pip install squareup so I believe all the necessary packages are available. Any ideas as to why the error occurs?

Hi @JC23 welcome to the forums!

It looks like that code block is neglecting to show you the client instantiation. You should create it like so:

from square.client import Client
 
client = Client(
    access_token='{{REPLACE_ACCESS_TOKEN}}',
    environment='sandbox',
)

I’m guessing without this, Python is assuming client is something else (a “WebApplicationClient” apparently).

Thanks @sjosey for the reply, creating the client got rid of the original error but now I have an error with the source id/nonce that’s passed to the backend. The terminal keeps telling me it’s invalid but when I print the value it appears just fine?

in squarer nonce is  "cnon:CBASEIyev5tZRPjYe8YbL2yhJBU"
[{'detail': 'Invalid source_id "cnon:CBASEIyev5tZRPjYe8YbL2yhJBU"', 'category': 'INVALID_REQUEST_ERROR', 'code': 'BAD_REQUEST'}] 

I’ve tried the source_id with & without the doubles quotes (") on the front & back but it’s the same invalid request error. I place the source_id into the body like so

body = {
"source_id": nonce,
"idempotency_key": "{{UNIQUE-KEY}}",
"amount_money":
.
.
.
}

Any ideas if it’s just a syntax error or something else going on?

The above is failing due to extra quotes. If it was an invalid nonce it would say something like “Card nonce not found” instead of “Invalid source_id” (this implies it’s badly formatted). Just pass the nonce exactly as you receive it from the payment form. If you try again without quotes and still get the error, let me know what the nonce is and I can take another look.

Thanks @sjosey without the quotes I was able to get a 200 response from the endpoint. But I don’t see it anywhere in Transactions when logged into the developer dashboard? The result I got back from the endpoint tells me for whatever reason/s the post is ‘delayed’ but I’m certain that autocomplete is set to True. The result is below

{'payment': {'delayed_until': '2020-11-06T21:51:55.687Z', 'delay_action': 'CANCEL', 'total_money': {'currency': 'USD', 'amount': 100}, 'location_id': '3YAJHY6MDMKBE', 'receipt_url': 'https://squareupsandbox.com/receipt/preview/3Vzx1bArVq16Vd0xqtXPmDoxq6XZY', 'updated_at': '2020-10-30T21:51:55.881Z', 'source_type': 'CARD', 'delay_duration': 'PT168H', 'id': '3Vzx1bArVq16Vd0xqtXPmDoxq6XZY', 'status': 'COMPLETED', 'receipt_number': '3Vzx', 'created_at': '2020-10-30T21:51:55.687Z', 'amount_money': {'currency': 'USD', 'amount': 100}, 'order_id': 'rStr0FiMvCSgeAMmqJPlZQstS8IZY', 'card_details': {'card': {'exp_month': 12, 'last_4': '1111', 'bin': '411111', 'fingerprint': 'sq-1-LSF6odEfLpmW5BiDVxZCxZUGWb_nHGljgL_Bq-Kz_koYWB9e-twpUiHz1H_Urvn3NQ', 'exp_year': 2021, 'card_brand': 'VISA', 'card_type': 'CREDIT'}, 'cvv_status': 'CVV_ACCEPTED', 'statement_description': 'SQ *DEFAULT TEST ACCOUNT', 'status': 'CAPTURED', 'avs_status': 'AVS_ACCEPTED', 'entry_method': 'KEYED'}}}

It’s confusing to me as ‘status’ has ‘COMPLETED’ & ‘CAPTURED’ but the ‘delayed_until’ is a week from now & as I previously stated can’t find it anywhere in the dashboard?

It appears those fields are present even if you set autocomplete=true, although I will get confirmation that it’s meant to be like that. The CAPTURED means the payment was complete. As this is a sandbox payment, are you looking at your sandbox dashboard? It will not show up on your production dashboard. You can find your sandbox dashboard on your apps page here: https://developer.squareup.com/apps, and open the sandbox account. If it’s still not there, let me know, and I can do some digging!

what is the “idempotency_key”: “{{UNIQUE-KEY}}”,?? from where i get this UNIQUE-KEY??? also I am creating two request to make a card and get payment transaction on one request but square doesnt allow me to use nonce more than once… any help how can i do this?

Idempotency is a key you would generate yourself. It’s generally defined as a “GUID” (globally unique identifier). Most modern programming languages have built-in functionality on how to generate a GUID.

On the CreatePayment request you would use the customer_card_id (that you created with CreateCustomerCard) for the source_id instead of the nonce.