Skip to content

Instantly share code, notes, and snippets.

@btribouillet
Created July 9, 2021 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save btribouillet/08feff4cef3015f5c1bb52f928dc16fc to your computer and use it in GitHub Desktop.
Save btribouillet/08feff4cef3015f5c1bb52f928dc16fc to your computer and use it in GitHub Desktop.
import requests
from decouple import config
from ..helpers.vipps_json_generators import get_base_ecom_headers, get_initiate_payment_http_body, \
get_capture_payment_http_body, get_order_ecom_headers, get_order_ecom_cancel_body
base_url = 'https://apitest.vipps.no'
def token_request():
"""
Request a Access Token from Vipps.
:return: A Access Token
"""
headers = {
'client_id': config('VIPPS_CLIENT_ID'),
'client_secret': config('VIPPS_CLIENT_SECRET'),
'Ocp-Apim-Subscription-Key': config('VIPPS_SUBSCRIPTION_KEY_PRIMARY'),
}
url = base_url + '/accesstoken/get'
response = requests.post(url, headers=headers)
return response.json()
def create_payment(order_id, transaction_amount, transaction_text, customer_number,
merchant_serial_number, express_checkout=False, is_app=False):
"""
Creates a initiate payment request to Vipps.
:param express_checkout: Boolean for enabling express checkout.
:param order_id: ID for the transaction.
:param transaction_amount: The amount to reserve for the transaction.
:param transaction_text: The text attached to the transaction.
:param customer_number: The number for the Vipps account that will pay for the transaction.
:param merchant_serial_number: The merchant serial number that will receive the transaction.
:return: The response for the initiate payment request, as JSON.
"""
url = base_url + '/ecomm/v2/payments'
headers = get_base_ecom_headers(token_request())
body = get_initiate_payment_http_body(order_id, transaction_amount, transaction_text, customer_number,
express_checkout=express_checkout, is_app=is_app,
merchant_serial_number=merchant_serial_number)
response = requests.post(url=url, headers=headers, json=body)
return response.json()
def capture_payment(order_id, merchant_serial_number, transaction_amount=0, transaction_text="Capture"):
"""
Captures the reserved payment for the provided order id.
:param order_id: ID for the transaction.
:param access_token: A token for authorizing the request to Vipps.
:param transaction_amount: The amount of the payment to capture, 0 for capturing all.
:param transaction_text: The associated text for the capture.
:param merchant_serial_number: The merchant serial number that will receive the transaction.
:return: Response for the capture request, as JSON.
"""
url = base_url + \
'/ecomm/v2/payments/{order_id}/capture'.format(order_id=order_id)
headers = get_order_ecom_headers(order_id, token_request())
body = get_capture_payment_http_body(
transaction_amount, transaction_text, merchant_serial_number)
response = requests.post(url=url, headers=headers, json=body)
return response.json()
def order_status(order_id):
"""
Requests the status of a transaction.
:param order_id: ID for the transaction.
:return: Response for the status request, as JSON.
"""
url = base_url + \
'/ecomm/v2/payments/{order_id}/status'.format(order_id=order_id)
headers = get_order_ecom_headers(order_id, token_request())
response = requests.get(url=url, headers=headers)
return response.json()
def cancel_order(order_id, transaction_text):
"""
Sends a cancel order request for the provided order id.
:param order_id: ID for the transaction
:param transaction_text: Text describing the cancel request.
:return: Response for the cancel order request, as JSON.
"""
url = base_url + \
'/ecomm/v2/payments/{order_id}/cancel'.format(order_id=order_id)
headers = get_order_ecom_headers(order_id, token_request())
body = get_order_ecom_cancel_body(transaction_text)
response = requests.put(url, headers=headers, json=body)
return response.json()
def order_details(order_id):
"""
Requests the order details of a transaction.
:param order_id: ID for the transaction.
:return: Response for the status request, as JSON.
"""
url = base_url + \
'/ecomm/v2/payments/{order_id}/details'.format(order_id=order_id)
headers = get_order_ecom_headers(order_id, token_request())
response = requests.get(url=url, headers=headers)
return response.json()
def refund_payment(order_id, merchant_serial_number, transaction_amount=0, transaction_text="Refund"):
"""
Refunds a already captured payment.
:param order_id: ID for the transaction.
:param transaction_amount: The amount of the payment to capture, 0 for capturing all.
:param transaction_text: The associated text for the capture.
:param merchant_serial_number: The merchant serial number that will receive the transaction.
:return: Response for the refund request, as JSON.
"""
url = base_url + \
'/ecomm/v2/payments/{order_id}/refund'.format(order_id=order_id)
headers = get_order_ecom_headers(order_id, token_request())
body = get_capture_payment_http_body(
transaction_amount, transaction_text, merchant_serial_number)
response = requests.post(url=url, headers=headers, json=body)
return response.json()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment