我的贝宝发票怎么了?

时间:2018-07-02 14:47:37

标签: python django rest paypal paypal-sandbox

我在django项目中使用paypal sdk制作Paypal发票时遇到了一些问题。当我尝试执行此代码

invoice_id = ''
    invoice = Invoice({
        "merchant_info": {
            "email": '',  # You must change this to your sandbox email account
            "first_name": str(merchant.first_name),
            "last_name": str(merchant.last_name),
            "business_name": str(merchant_full_name),
            "phone": {
                "country_code": "001",
                "national_number": str(merchant.phone)
            },
            "address": {
                "line1": str(merchant_address.address),
                "city": str(merchant_address.city),
                "state": str(merchant_address.state),
                "postal_code": str(merchant_address.zip_code),
                "country_code": "US"
            }
        },
        "billing_info": [{"email": buyer.paypal_address}],
        "items": [
            {
                "name": "Slab",
                "quantity": 1,
                "unit_price": {
                    "currency": "USD",
                    "value": float(slab.price)
                }
            }
        ],
        "note": "Invoice for slab",
        "payment_term": {
            "term_type": "NET_45"
        },
        "shipping_info": {
            "first_name": buyer.first_name,
            "last_name": buyer.last_name,
            "business_name": str(buyer_full_name),
            "phone": {
                "country_code": "001",
                "national_number": str(buyer.phone)
            },
            "address": {
                "line1": str(buyer_address.address),
                "city": str(buyer_address.city),
                "state": str(buyer_address.state),
                "postal_code": str(buyer_address.zip_code),
                "country_code": "US"
            }
        },
        "shipping_cost": {
            "amount": {
                "currency": "USD",
                "value": 0
            }
        }
    })

    if invoice.create():
        print(json.dumps(invoice.to_dict(), sort_keys=False, indent=4))
        invoice_id = invoice['id']
        return invoice_id
    else:
        print(invoice.error)

我的服务器向我返回这些错误

  

INFO:paypalrestsdk.api:Request [POST]:   https://api.sandbox.paypal.com/v1/oauth2/token   INFO:paypalrestsdk.api:Response [200]:确定,持续时间:1.156739s。   INFO:paypalrestsdk.api:PayPal-Request-Id:   660f7807-961f-4460-bafd-18412b489a91   INFO:paypalrestsdk.api:Request [POST]:   https://api.sandbox.paypal.com/v1/invoicing/invoices   INFO:paypalrestsdk.api:响应[401]:未经授权,持续时间:   1.213655秒。 INFO:paypalrestsdk.api:Request [POST]:https://api.sandbox.paypal.com/v1/oauth2/token   INFO:paypalrestsdk.api:Response [200]:确定,持续时间:1.53491s。

据我了解,这是贝宝令牌的问题,对吧?

1 个答案:

答案 0 :(得分:0)

您需要验证您的请求:

import requests, base64

# you can only encode byte objects
credentials = b"userid:password"
b64Val = base64.b64encode(credentials)
r=requests.post(api_URL, 
    headers={"Authorization": "Basic %s" % b64Val},
    data=payload)