Package: python-paypal-api
PYTHON-PAYPAL-APIPaypal's Rest APIA python 3 wrapper to access Paypal Rest API with an easy-to-use interface.Installpip install python-paypal-apiDonateIf you find this project is useful consider donating orsponsorit to keep on going on it, thank you.OverviewYou need obtain your own credentials with Paypal that may include a paypal personal or business account and access as developer. Please view the officialPaypal DeveloperYou can also check how use the credentials on thedocumentationof this Python Paypal API.Environment Credentialsfrompython_paypal_api.apiimportIdentityos.environ["client_id"]="your-client-id"os.environ["client_secret"]="your-client-secret"# os.environ["client_mode"] = "PRODUCTION"# Can omit client_mode if using SANDBOXresult=Identity().get_userinfo()Code CredentialsYou can use your credentials as follows passing it to the client as a dict. Please review the fulldocumentationto see all posibilities to include your credentials.Python codefrompython_paypal_api.apiimportIdentitymy_credentials=dict(client_id="your-client-id",client_secret="your-client-secret",client_mode="PRODUCTION")# Can omit client_mode to use SANDBOXresult=Identity(credentials=my_credentials).get_userinfo()YAML CredentialsUse a config.yaml file with your credentials for more convenience and manage diferent accounts or profiles. You can store a Sandbox and Production (Live) credentials to comvenient switch from sandbox to live environment.
Note: default credentials without client_mode will use SANDBOX paypal endpoint for testingCreate a file config.yaml (From version 0.1.1 the file use the default name provided by confuse package and use template validation)Please review the fulldocumentationto see all posibilities to include in your configuration.version:'1.0'configuration:production:client_id:'your-client-id'client_secret:'your-client-secret'client_mode:'PRODUCTION'default:client_id:'your-client-id-sandbox'client_secret:'your-client-secret-sandbox'Python codefrompython_paypal_api.apiimportIdentity# Leave empty will use the 'default' accountresult=Identity().get_userinfo()# will use germany account dataresult=Identity(credentials="production").get_userinfo()Search path for config.yamlmacOS:~/.config/python-paypal-apiand~/Library/Application Support/python-paypal-apiOther Unix:~/.config/python-paypal-apiand/etc/python-paypal-apiWindows:%APPDATA%\python-paypal-apiwhere the APPDATA environment variable falls back to%HOME%\AppData\Roamingif undefinedConfuse HelpManaging obtained credentialsBy default the package will store it in cache to use the LRU Cache from cachetools but the cache will be available only during the script living environment, so once you get the token, any call will use the cached token but since the script terminates the cached key will be gone.There is a way to create a 600 permissions file in the configuration search path. This is because the token obtained it will ve valid for 32400 seconds and storing it will reduce the calls to the oauth paypal endpoint.
The token also can be stored encrypted, for complex configurations read thePython Paypal API Help.frompython_paypal_api.apiimportIdentityfrompython_paypal_api.baseimportPaypalApiExceptionimportloggingtry:result=Identity(store_credentials=True).get_userinfo()logging.info(result)exceptPaypalApiExceptionaserror:logging.error(error)ExceptionsYou can use atryexcept statement when you call the API and catch exceptions if some problem ocurred:frompython_paypal_api.apiimportIdentityfrompython_paypal_api.baseimportPaypalApiExceptionimportloggingtry:result=Identity().get_userinfo()logging.info(result)exceptPaypalApiExceptionaserror:logging.error(error)DebugUse debug=True if you want see some logs like the header you submit to the api endpoint, the method and path used among the params and the data submitted if any, to trace possible errors.frompython_paypal_api.apiimportIdentity,frompython_paypal_api.baseimportPaypalApiExceptionimportloggingtry:result=Identity(debug=True).get_userinfo()logging.info(result)exceptPaypalApiExceptionaserror:logging.error(error)Paypal Current ResourcesCatalogDisputesIdentityInvoicesOrdersPartner ReferralTrackingTransactionsAPI NOTICEThis API is based on theAPI Clientcreated by@saleweaverbut adapted to paypal auth requeriments and improved system for token callDISCLAIMERWe are not affiliated with PayPalLICENSE