Skip to main content

PayMongo Python Library

Project description

PayMongo Python Library

PayMongo Python library provides python applications an easy access to the PayMongo API. Explore various classes that can represent API resources on object instantiation. The goal of this library is simplify PayMongo integration with any python application.

Pending TODOs

  • TBD

Documentation

See the PayMongo API docs.

Requirements

  • Python 3.9.+

Installation

You don't need this source code unless you want to modify the library. If you just want to use the package, just run:

pip3 install paymongo-python

If you want to run the library from source:

Create a virtual environment

python3 -m venv env

Activate the virtual environment

source env/bin/activate

Installing package to virtual environment (editable)

pip3 install -e paymongo-python

python

Usage

The library needs to be configured with your account's secret key which is available in your [PayMongo Dashboard][api-keys]. Initialize the library to its value:

import paymongo

# Set api key config
paymongo.api_key='sk_test...'

# Payment Method
payment_method = paymongo.PaymentMethod.retrieve('pm_...')

# Retrieve attributes
payment_method.id
 => "pm_..."

payment_method.type
 => "card"

paymongo.PaymentMethod.create({
  'type': 'card',
  'details': {
    'card_number': '5111111111111118',
    'cvc': '123',
    'exp_month': 3,
    'exp_year': 2025,
  },
  'billing': {
    'address': {
      'line1': 'test line 1',
      'line2': 'test line 2',
      'city': 'Antipolo',
      'state': 'Rizal',
      'postal_code': '1870',
      'country': 'PH'
    },
    'email': 'test@paymongo.com',
    'name': 'Pay Mongo',
    'phone': '09123456789'
  }
})

# Payment Intent
paymongo.PaymentIntent.retrieve('pi_...')

payment_intent = paymongo.PaymentIntent.create({
  'amount': 10000,
  'currency': 'PHP',
  'description': 'Dog Treat',
  'payment_method_allowed': [
    'card'
  ],
  'statement_descriptor': 'BarkerShop'
})

paymongo.PaymentIntent.attach('pi_...', {
  'payment_method': 'pm_...',
  'return_url': 'https://test/success'
})

paymongo.PaymentIntent.cancel('pi_...')

paymongo.PaymentIntent.capture('pi_...', {
  'amount':10000
})

# Payment
paymongo.Payment.retrieve('pay_...')

# Refund
paymongo.Refund.retrieve('ref_...')

paymongo.Refund.create({
  'amount': 10000,
  'payment_id': 'pay_...',
  'reason': 'requested_by_customer',
  'metadata': {
    'merchant': 'test value'
  }
})

Customers

paymongo.Customer.retrieve('cus_...')

paymongo.Customer.create({
  'default_device': 'phone',
  'email': 'test@paymongo.com',
  'first_name': 'Pay',
  'last_name': 'Mongo',
  'phone': '+624123456789'
})

paymongo.Customer.update('cus_...', {
  'default_device': 'phone',
  'email': 'test@paymongo.com',
  'first_name': 'Pay',
  'last_name': 'Mongo',
  'phone': '+649223456789'
})

paymongo.Customer.delete('cus_...')

Links

paymongo.Link.retrieve('link_...')

paymongo.Link.archive('link_...')

paymongo.Link.unarchive('link_...')

paymongo.Link.create({
  'amount': 10000,
  'description': 'link description',
  'remarks': 'link remarks'
})

links = paymongo.Link.all({'reference_number': '1234abc'})

Webhooks

paymongo.Webhook.retrieve('hook_...')

paymongo.Webhook.create({
  'events': ['payment.refunded', 'payment.refund.updated'],
  'url': 'http://localhost:3100/webhook'
})

paymongo.Webhook.disable('hook_...')

paymongo.Webhook.enable('hook_...')

paymongo.Webhook.update('hook_...', {
   'events': ['payment.refunded', 'payment.refund.updated'],
   'url': 'http://localhost:3001/webhook'
})

webhooks = paymongo.Webhook.all()

Handle errors

try:
  payment_intent = paymongo.PaymentIntent.retrieve('pi_...')
except paymongo.StandardException as e:
  # Handle error
  print(e.errors[0].detail)
  print(e.errors[0].code)

Verifying webhook signature

payload = '{"data":{"id":"evt_...","type":"event","attributes":{"type":"source.chargeable"},"created_at":1675323264}}}'
signature_header = 't=1675323267,te=,li=99f...'
webhook_secret_key = 'whsk_...'

try:
  event = paymongo.Webhook.construct_event(
    payload=payload,
    signature_header=signature_header,
    webhook_secret_key=webhook_secret_key
  )

  event.id
  event.type
  event.resource
except paymongo.SignatureVerificationException:
  # Handle invalid signature

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

paymongo-python-0.1.0.tar.gz (3.8 MB view details)

Uploaded Source

Built Distribution

paymongo_python-0.1.0-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file paymongo-python-0.1.0.tar.gz.

File metadata

  • Download URL: paymongo-python-0.1.0.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.9

File hashes

Hashes for paymongo-python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d785d8e2a39e199f8723e9ce91e053367e055859f9cf317d9fe915b206bf47df
MD5 1743cf7271edd67156b1f87f460e5040
BLAKE2b-256 7d434d2d6f17fd9f24702e16133b84cfc73b2e530da5ae34b44ed442856d5b28

See more details on using hashes here.

File details

Details for the file paymongo_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for paymongo_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7e8c29c9916ebf99ae41dc9c6ed8db5088fc43f785e92f2b5fd6329df5ed31b8
MD5 2eb7daf06302b37df41c33f10c54102d
BLAKE2b-256 b42779a47e3897cdae780e38298a93b54ad3aa1e868a40e7dae1b1eb3bae40a8

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page