E
E
Emil Revencu2017-10-15 22:45:41
Python
Emil Revencu, 2017-10-15 22:45:41

How to make Paypal Payment correctly?

import requests
import base64
import json
ClientId = 'XXXXXXXXXXXXXXXXX';
Secret = 'XXXXXXXXXXXXXXXXX';
token = base64.b64encode(ClientId + ':' + Secret);
header = {
    'Authorization': ('Basic %s' % token),
    'Accept': 'application/json',
}
data = {'grant_type': 'client_credentials'}
page=requests.post('https://api.sandbox.paypal.com/v1/oauth2/token', headers=header, data=data)
token=json.loads(page.text)['access_token']

header={
'Content-Type': 'application/json',
'Authorization': 'Bearer '+token,
}

data={
  "intent": "sale",
  "payer": {"payment_method": "paypal"},
  "transactions": [{
    "amount": {"total": "7.47", "currency": "USD"}
  }]
}

page=requests.post('https://api.sandbox.paypal.com/v1/payments/payment', headers=header, data=data)
data=json.loads(page.text)
print data

I receive Access Token without problems. When creating a Payment, I get the following error:
{'message': 'The request JSON is not well formed.', 'debug_id': '30ae2cc8d3a63', 'name': 'MALFORMED_REQUEST', 'information_link': 'https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST'}

As I understand it, the JSON request body is not correct. But I checked it: VALID.
What is wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Emil Revencu, 2017-10-15
@Revencu

Found a solution!
It turns out that my JSON does not accept Paypal due to the fact that it has single quotes.
You need double quotes.
json.dumps(data)

page=requests.post('https://api.sandbox.paypal.com/v1/payments/payment', headers=header, data=json.dumps(data))

E
EyesFit, 2022-01-31
@EyesFit

I used PayPal Payments . Not as big though. Remember that they get a significant share of everything that goes through them, especially when there is a currency conversion. This is actually the case, even if they don't think you're worried about money laundering, which they probably do more often than you think.
If PayPal works, it's good... for your client. This is for you, dear convenience. If it doesn't work and you will lose your job and livelihood. They don't care about the confirmation you provided.
About a year ago, I started talking to my clients about bitcoin. Naturally, not all clients pay me in bitcoins, but every dollar that is put into my wallet instead of a PayPal dollar.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question