PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/README.md

https://gitlab.com/CORP-RESELLER/PayPal-Python-SDK
Markdown | 348 lines | 264 code | 84 blank | 0 comment | 0 complexity | 4ff3dd0e740d39f853f987170dc2a0f9 MD5 | raw file
  1. # PayPal REST SDK
  2. Continuous integration status:
  3. [![Build Status](https://travis-ci.org/paypal/PayPal-Python-SDK.svg?branch=master)](https://travis-ci.org/paypal/PayPal-Python-SDK) [![Coverage Status](https://coveralls.io/repos/paypal/PayPal-Python-SDK/badge.svg?branch=master)](https://coveralls.io/r/paypal/PayPal-Python-SDK?branch=master)
  4. The PayPal REST SDK provides Python APIs to create, process and manage payment. The [Paypal REST APIs](https://developer.paypal.com/webapps/developer/docs/api/) are fully supported by the sdk.
  5. > The REST APIs are getting closer to parity with older merchant APIs. Check out https://github.com/paypal/PayPal-Python-SDK#explore-further-payment-capabilities
  6. > If deploying on Google appengine and running into issues since requests is a dependency, see [#66](https://github.com/paypal/PayPal-Python-SDK/issues/66) for workaround.
  7. > **The Payment Card Industry (PCI) Council has [mandated](http://blog.pcisecuritystandards.org/migrating-from-ssl-and-early-tls) that early versions of TLS be retired from service. All organizations that handle credit card information are required to comply with this standard. As part of this obligation, PayPal is updating its services to require TLS 1.2 for all HTTPS connections. At this time, PayPal will also require HTTP/1.1 for all connections. See the [PayPal TLS Update repository](https://github.com/paypal/tls-update) for more information.**
  8. > **TLSv1_2 warning: Due to PCI compliance, merchant servers using a version of TLS that does not support TLSv1_2 will receive a warning.
  9. > **To verify that your server supports PCI compliant version of TLS, run [this sample](/samples/payment/create_with_paypal_security_test.py) with your sandbox client_id and client_secret.
  10. ## System Requirements
  11. PayPal SDK depends on the following system libraries:
  12. * libssl-dev
  13. * libffi-dev
  14. On Debian-based systems, run:
  15. ```sh
  16. apt-get install libssl-dev libffi-dev
  17. ```
  18. ## Installation
  19. Install using `easy_install`:
  20. ```sh
  21. easy_install paypalrestsdk
  22. ```
  23. Install using `pip`:
  24. ```sh
  25. pip install paypalrestsdk
  26. ```
  27. ## Configuration
  28. Register for a developer account and get your client_id and secret at [PayPal Developer Portal](https://developer.paypal.com).
  29. ```python
  30. import paypalrestsdk
  31. paypalrestsdk.configure({
  32. "mode": "sandbox", # sandbox or live
  33. "client_id": "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM",
  34. "client_secret": "EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM" })
  35. ```
  36. Configure through environment variables:
  37. ```sh
  38. export PAYPAL_MODE=sandbox # sandbox or live
  39. export PAYPAL_CLIENT_ID=EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM
  40. export PAYPAL_CLIENT_SECRET=EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM
  41. ```
  42. Configure through a non-global API object
  43. ```python
  44. import paypalrestsdk
  45. my_api = paypalrestsdk.Api({
  46. 'mode': 'sandbox',
  47. 'client_id': '...',
  48. 'client_secret': '...'})
  49. payment = paypalrestsdk.Payment({...}, api=my_api)
  50. ```
  51. ### Create Payment
  52. ```python
  53. import paypalrestsdk
  54. import logging
  55. paypalrestsdk.configure({
  56. "mode": "sandbox", # sandbox or live
  57. "client_id": "EBWKjlELKMYqRNQ6sYvFo64FtaRLRR5BdHEESmha49TM",
  58. "client_secret": "EO422dn3gQLgDbuwqTjzrFgFtaRLRR5BdHEESmha49TM" })
  59. payment = paypalrestsdk.Payment({
  60. "intent": "sale",
  61. "payer": {
  62. "payment_method": "credit_card",
  63. "funding_instruments": [{
  64. "credit_card": {
  65. "type": "visa",
  66. "number": "4417119669820331",
  67. "expire_month": "11",
  68. "expire_year": "2018",
  69. "cvv2": "874",
  70. "first_name": "Joe",
  71. "last_name": "Shopper" }}]},
  72. "transactions": [{
  73. "item_list": {
  74. "items": [{
  75. "name": "item",
  76. "sku": "item",
  77. "price": "1.00",
  78. "currency": "USD",
  79. "quantity": 1 }]},
  80. "amount": {
  81. "total": "1.00",
  82. "currency": "USD" },
  83. "description": "This is the payment transaction description." }]})
  84. if payment.create():
  85. print("Payment created successfully")
  86. else:
  87. print(payment.error)
  88. ```
  89. ### Get Payment details
  90. ```python
  91. # Fetch Payment
  92. payment = paypalrestsdk.Payment.find("PAY-57363176S1057143SKE2HO3A")
  93. # Get List of Payments
  94. payment_history = paypalrestsdk.Payment.all({"count": 10})
  95. payment_history.payments
  96. ```
  97. ### Execute Payment
  98. Only for [Payment](https://github.com/paypal/PayPal-Python-SDK/blob/master/samples/payment/create_with_paypal.py) with `payment_method` as `"paypal"`
  99. ```python
  100. payment = paypalrestsdk.Payment.find("PAY-57363176S1057143SKE2HO3A")
  101. if payment.execute({"payer_id": "DUFRQ8GWYMJXC"}):
  102. print("Payment execute successfully")
  103. else:
  104. print(payment.error) # Error Hash
  105. ```
  106. ### Subscription
  107. Create [subscription payments](https://developer.paypal.com/docs/integration/direct/create-billing-plan/) i.e. planned sets of future recurring payments at periodic intervals. Billing plans serve as the template for a subscription while billing agreements can be used to have customers subscribe to the plan.
  108. #### Create a billing plan
  109. ```python
  110. from paypalrestsdk import BillingPlan
  111. billing_plan = BillingPlan({
  112. "name": "Fast Speed Plan",
  113. "description": "Create Plan for Regular",
  114. "merchant_preferences": {
  115. "auto_bill_amount": "yes",
  116. "cancel_url": "http://www.paypal.com/cancel",
  117. "initial_fail_amount_action": "continue",
  118. "max_fail_attempts": "1",
  119. "return_url": "http://www.paypal.com/execute",
  120. "setup_fee": {
  121. "currency": "USD",
  122. "value": "25"
  123. }
  124. },
  125. "payment_definitions": [
  126. {
  127. "amount": {
  128. "currency": "USD",
  129. "value": "100"
  130. },
  131. "charge_models": [
  132. {
  133. "amount": {
  134. "currency": "USD",
  135. "value": "10.60"
  136. },
  137. "type": "SHIPPING"
  138. },
  139. {
  140. "amount": {
  141. "currency": "USD",
  142. "value": "20"
  143. },
  144. "type": "TAX"
  145. }
  146. ],
  147. "cycles": "0",
  148. "frequency": "MONTH",
  149. "frequency_interval": "1",
  150. "name": "Regular 1",
  151. "type": "REGULAR"
  152. }
  153. ],
  154. "type": "INFINITE"
  155. })
  156. response = billing_plan.create()
  157. print(response)
  158. ```
  159. Check out [more samples](/samples/subscription/). The [Subscription REST APIs](https://developer.paypal.com/webapps/developer/docs/api/#subscriptions) are fully supported by the sdk.
  160. Also, check out a [flask application](/samples/subscription/ppsubscribe) demonstrating the use of subscription APIs from both merchant and customer points of view.
  161. ### Future Payments
  162. Check out this [sample](/samples/payment/create_future_payment.py) for executing [future payments](https://developer.paypal.com/docs/integration/mobile/make-future-payment/) for a customer who has granted consent on a mobile device.
  163. ### Third Party Invoicing
  164. Check out this [sample](/samples/invoice/third_party_invoicing.py) for executing third party invoicing for a merchant who has granted consent to send invoice on their behalf.
  165. ### Orders
  166. Create and manage [Orders](https://developer.paypal.com/webapps/developer/docs/integration/direct/create-process-order/#create-the-order), i.e. getting consent from buyer for a purchase but only placing the funds on hold when the merchant is ready to fulfill the [order](https://developer.paypal.com/webapps/developer/docs/api/#orders), have a look at [samples](/samples/order)
  167. ### Payouts
  168. For creating [batch and single payouts](https://developer.paypal.com/webapps/developer/docs/integration/direct/payouts-overview/), check out the samples for [payouts](/samples/payout) and [payout items](/samples/payout_item). The [Payouts feature](https://developer.paypal.com/webapps/developer/docs/api/#payouts) enables you to make PayPal payments to multiple PayPal accounts in a single API call.
  169. #### Create a synchronous payout
  170. ```python
  171. from paypalrestsdk import Payout, ResourceNotFound
  172. payout = Payout({
  173. "sender_batch_header": {
  174. "sender_batch_id": "batch_1",
  175. "email_subject": "You have a payment"
  176. },
  177. "items": [
  178. {
  179. "recipient_type": "EMAIL",
  180. "amount": {
  181. "value": 0.99,
  182. "currency": "USD"
  183. },
  184. "receiver": "shirt-supplier-one@mail.com",
  185. "note": "Thank you.",
  186. "sender_item_id": "item_1"
  187. }
  188. ]
  189. })
  190. if payout.create(sync_mode=True):
  191. print("payout[%s] created successfully" %
  192. (payout.batch_header.payout_batch_id))
  193. else:
  194. print(payout.error)
  195. ```
  196. ### Explore further payment capabilities
  197. For [exploring additional payment capabilites](https://developer.paypal.com/docs/integration/direct/explore-payment-capabilities/), such as handling discounts, insurance, soft_descriptor and invoice_number, have a look at this [example](/samples/payment/create_with_paypal_further_capabilities.py). These bring REST payment functionality closer to parity with older Merchant APIs.
  198. ### Customizing a PayPal payment experience
  199. Customizing a [PayPal payment experience](https://developer.paypal.com/webapps/developer/docs/integration/direct/rest-experience-overview/) is available as of version 1.5.0 enabling merchants to provide a customized experience to consumers from the merchant’s website to the PayPal payment. Get started with the [supported rest methods](https://developer.paypal.com/webapps/developer/docs/api/#payment-experience) and [samples](/samples/payment_experience/web_profile).
  200. ### Webhooks - Receive notifications about PayPal Payments
  201. To receive [notifications from PayPal about Payment events](https://developer.paypal.com/webapps/developer/docs/api/#notifications) on your server, webhook support is now available as of version 1.6.0.
  202. - For creating and managing [Webhook and Webhook Events](https://developer.paypal.com/webapps/developer/docs/integration/direct/rest-webhooks-overview/), check out the [samples](/samples/notification/) to see how you can use the Python sdk to create and manage webhooks and webhook events.
  203. - See this [sample](/samples/notification/webhook-events/verify_webhook_events.py) for verifying that the webhook response is unaltered, from PayPal and targeted towards the intended recipient.
  204. - See this [sample](/samples/notification/webhook-events/get_webhook_event_resource.py) for parsing webhook payload and getting the resource delivered via the webhook event.
  205. ### Invoicing
  206. Create, send and manage [invoices](https://developer.paypal.com/docs/integration/direct/invoicing/).
  207. #### Create an invoice
  208. ```python
  209. from paypalrestsdk import Invoice
  210. invoice = Invoice({
  211. 'merchant_info': {
  212. "email": "default@merchant.com",
  213. },
  214. "billing_info": [{
  215. "email": "example@example.com"
  216. }],
  217. "items": [{
  218. "name": "Widgets",
  219. "quantity": 20,
  220. "unit_price": {
  221. "currency": "USD",
  222. "value": 2
  223. }
  224. }],
  225. })
  226. response = invoice.create()
  227. print(response)
  228. ```
  229. ### OpenID Connect
  230. ```python
  231. import paypalrestsdk
  232. from paypalrestsdk.openid_connect import Tokeninfo, Userinfo
  233. paypalrestsdk.configure({
  234. "mode": "sandbox",
  235. "client_id": "CLIENT_ID",
  236. "client_secret": "CLIENT_SECRET",
  237. "openid_redirect_uri": "http://example.com" })
  238. # Generate login url
  239. login_url = Tokeninfo.authorize_url({ "scope": "openid profile"})
  240. # Create tokeninfo with Authorize code
  241. tokeninfo = Tokeninfo.create("Replace with Authorize code")
  242. # Refresh tokeninfo
  243. tokeninfo = tokeninfo.refresh()
  244. # Create tokeninfo with refresh_token
  245. tokeninfo = Tokeninfo.create_with_refresh_token("Replace with refresh_token")
  246. # Get userinfo
  247. userinfo = tokeninfo.userinfo()
  248. # Get userinfo with access_token
  249. userinfo = Userinfo.get("Replace with access_token")
  250. # Generate logout url
  251. logout_url = tokeninfo.logout_url()
  252. ```
  253. ### Debugging
  254. * Include Headers and Content by setting logging level to DEBUG, particularly for
  255. Paypal-Debug-Id if requesting PayPal Merchant Technical Services for support
  256. logging.basicConfig(level=logging.INFO).
  257. * Full request and response headers and body is visible at DEBUG level logging only
  258. for sandbox or non-production mode. This is done to prevent sensitive information
  259. from getting logged in live mode.
  260. Check out [more samples](/samples/invoice/). The [Invoicing REST APIs](https://developer.paypal.com/webapps/developer/docs/api/#invoicing) are fully supported by the sdk.
  261. ## License
  262. Code released under [SDK LICENSE](LICENSE)
  263. ## Contributions
  264. Pull requests and new issues are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md) for details.