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.2.1.tar.gz (3.8 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

paymongo_python-0.2.1-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file paymongo_python-0.2.1.tar.gz.

File metadata

  • Download URL: paymongo_python-0.2.1.tar.gz
  • Upload date:
  • Size: 3.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for paymongo_python-0.2.1.tar.gz
Algorithm Hash digest
SHA256 1029a0571b6749bdd15fc7273ba0d9665d9d4e3094e2fb766a66b70d6960e6ab
MD5 8a1920e46e23e73a87f5a0181878bd1b
BLAKE2b-256 e1983827a1ef6def6351fdce82fb68b882000a19d2ee3dbdd312afccd5cc182b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for paymongo_python-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 207a17f704bf2e0e14a990100bef6eb2690cea1936e7c7e072e149aa66d03913
MD5 c83427abf79b11bf0ceda412d7dd5fcc
BLAKE2b-256 cafce1c77e5b86919903ad098054c755057d8368aeaa1899bf2bc657885af5ef

See more details on using hashes here.

Supported by

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