Sam Moldenha
Sam Moldenha

Reputation: 284

Square Up python API create payment

I have been trying to make a payment using the Square Up API using python. I have discovered from the less than helpful docs after a lot of wasted time that you need a source_id to make a payment with a credit card. Apparently, the only way to get the source_id which is also a nonce is to have the credit card information filled out on a website form. So, I came to the conclusion as a worst case scenario that I have the form uploaded on a local flask server, connect to it through the selenium or requests library, and then have the form filled out and submitted to create a nonce. I have tried finding examples that will work on a flask or web server that can be created using python. I have tried the main html example on the doc and then I found this example:https://github.com/square/connect-api-examples/blob/master/connect-examples/v2/python_payment/main.py

I added these 2 lines to the end to at least have the server running:

import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)

And for those that try using this example, the config file will have to be remade using this python script, otherwise errors with the config file will be thrown:

import configparser

config = configparser.ConfigParser()
config['DEFAULT'] = {'environment':'sandbox'}
config['PRODUCTION'] = {'square_application_id':you-application-id, 
'square_access_token':your-access-token, 
'square_location_id':your-location-id}

 config['SANDBOX'] = {'square_application_id':you-application-id, 
'square_access_token':your-access-token, 
'square_location_id':your-location-id}

Is there a version that does work with a flask server? Or is there a known easier way to work with this API, so that I don't have to do all these extra steps just to take a credit card payment?

I was also wondering if there was a way with sockets to connect to the server to get the credit card nonce. I know that is a stretch, but maybe possible, and it would be a lot easier.

Upvotes: 0

Views: 1050

Answers (1)

Dmytro Bugayev
Dmytro Bugayev

Reputation: 686

I am no expert on the Square API or SDK (only been playing around with it today), but from what I can gather, there are two parts to it - firstly, the front-end forms/javascript which you can quite easily serve up as part of your pages, which will then generate the source and location IDs (presumably this also registers the ID on their servers). You then post these IDs to YOUR own server. Then the second part is to make the payment API calls from your server. Sounds like you've focused on the latter, while I've gotten the former to work without any of the follow-up API calls yet.

The complete front-end walk-through is here:

https://developer.squareup.com/docs/web-payments/take-card-payment

Thought I found this example to be more succinct, if lacking the POST to your server's end-point: https://developer.squareup.com/reference/sdks/web/payments/card-payments

Upvotes: 0

Related Questions