Skip to content

Commit

Permalink
Hash Transition (#93)
Browse files Browse the repository at this point in the history
* Fix SyntaxWarning error

* All services switched to Authorization V2

* signature verification added to existing samples

* new service samples including signature verification added

* Supported versions update

* SignatureStrip&CheckoutformPreAuth Modified

---------

Co-authored-by: Osman Keser <[email protected]>
  • Loading branch information
byasarcse and osman-keser authored Nov 4, 2024
1 parent f9c075a commit 2439fb3
Show file tree
Hide file tree
Showing 15 changed files with 458 additions and 29 deletions.
28 changes: 20 additions & 8 deletions iyzipay/iyzipay_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@
import importlib
import json
import random
import re
import string

import iyzipay


class IyzipayResource:
RANDOM_STRING_SIZE = 8
RE_SEARCH_V2 = r'/v2/'
header = {
"Accept": "application/json",
"Content-type": "application/json",
Expand All @@ -22,6 +19,23 @@ class IyzipayResource:
def __init__(self):
self.httplib = importlib.import_module('http.client')

def strip_zero(self, number):
has_zero = number.endswith('.0')
return number.replace('.0', '') if has_zero else number

def calculate_hmac_sha256_signature(self, params, secret_key):
secret_key = bytes(secret_key.encode('utf-8'))
msg = ':'.join(params).encode('utf-8')

hmac_obj = hmac.new(secret_key, digestmod=hashlib.sha256)
hmac_obj.update(msg)
return hmac_obj.hexdigest()

def verify_signature(self, params, secret_key, signature):
calculated_signature = self.calculate_hmac_sha256_signature(params, secret_key)
verified = signature == calculated_signature
print('Signature verified:', verified)

def connect(self, method, url, options, request_body_dict=None, pki=None):
connection = self.httplib.HTTPSConnection(options['base_url'])
body_str = json.dumps(request_body_dict)
Expand All @@ -32,15 +46,13 @@ def connect(self, method, url, options, request_body_dict=None, pki=None):
def get_http_header(self, url, options=None, body_str=None, pki_string=None):
random_str = self.generate_random_string(self.RANDOM_STRING_SIZE)
self.header.update({'x-iyzi-rnd': random_str})
if re.search(self.RE_SEARCH_V2, url, re.IGNORECASE) is not None:
return self.get_http_header_v2(url, options, random_str, body_str)
else:
return self.get_http_header_v1(options, pki_string, random_str)
self.get_http_header_v1(options, pki_string, random_str)
return self.get_http_header_v2(url, options, random_str, body_str)

def get_http_header_v1(self, options, pki_string, random_str=None):
if pki_string is not None:
self.header.update(
{'Authorization': self.prepare_auth_string(options, random_str, pki_string)})
{'Authorization_Fallback': self.prepare_auth_string(options, random_str, pki_string)})
return self.header

def get_http_header_v2(self, url, options, random_str, body_str):
Expand Down
4 changes: 2 additions & 2 deletions iyzipay/pki_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def append(self, key, value=None):
return self

def append_price(self, key, value=None):
if value is not None and value is not "":
if value is not None and value != "":
self.append_key_value(key, str(round(float(value), 2)))
return self

Expand All @@ -22,7 +22,7 @@ def append_array(self, key, array=None):
return self

def append_key_value(self, key, value=None):
if value is not None and value is not "":
if value is not None and value != "":
self.request_string = self.request_string + key + "=" + str(value) + ","

def remove_trailing_comma(self):
Expand Down
17 changes: 15 additions & 2 deletions samples/create_payment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import iyzipay

options = {
Expand Down Expand Up @@ -83,6 +84,18 @@
'basketItems': basket_items
}

payment = iyzipay.Payment().create(request, options)
payment = iyzipay.Payment()
payment_result = payment.create(request, options)
payment_result_response = json.load(payment_result)
print('response:', payment_result_response)

print(payment.read().decode('utf-8'))
if payment_result_response['status'] == 'success':
secret_key = options['secret_key']
paymentId = payment_result_response['paymentId']
currency = payment_result_response['currency']
basketId = payment_result_response['basketId']
conversationId = payment_result_response['conversationId']
paidPrice = payment.strip_zero(str(payment_result_response['paidPrice']))
price = payment.strip_zero(str(payment_result_response['price']))
signature = payment_result_response['signature']
payment.verify_signature([paymentId, currency, basketId, conversationId, paidPrice, price],secret_key, signature)
33 changes: 33 additions & 0 deletions samples/create_payment_postauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import json
import iyzipay

options = {
'api_key': iyzipay.api_key,
'secret_key': iyzipay.secret_key,
'base_url': iyzipay.base_url
}

request = {
'locale': 'tr',
'conversationId': '123456789',
'paymentId': '1',
'paidPrice': '1.2',
'ip': '85.34.78.112',
'currency': 'TRY'
}

payment_postauth = iyzipay.PaymentPostAuth()
payment_postauth_result = payment_postauth.create(request, options)
payment_postauth_result_response = json.load(payment_postauth_result)
print('response:', payment_postauth_result_response)

if payment_postauth_result_response['status'] == 'success':
secret_key = options['secret_key']
paymentId = payment_postauth_result_response['paymentId']
currency = payment_postauth_result_response['currency']
basketId = payment_postauth_result_response['basketId']
conversationId = payment_postauth_result_response['conversationId']
paidPrice = payment_postauth.strip_zero(str(payment_postauth_result_response['paidPrice']))
price = payment_postauth.strip_zero(str(payment_postauth_result_response['price']))
signature = payment_postauth_result_response['signature']
payment_postauth.verify_signature([paymentId, currency, basketId, conversationId, paidPrice, price], secret_key, signature)
101 changes: 101 additions & 0 deletions samples/create_payment_preauth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import json
import iyzipay

options = {
'api_key': iyzipay.api_key,
'secret_key': iyzipay.secret_key,
'base_url': iyzipay.base_url
}

payment_card = {
'cardHolderName': 'John Doe',
'cardNumber': '5528790000000008',
'expireMonth': '12',
'expireYear': '2030',
'cvc': '123',
'registerCard': '0'
}

buyer = {
'id': 'BY789',
'name': 'John',
'surname': 'Doe',
'gsmNumber': '+905350000000',
'email': '[email protected]',
'identityNumber': '74300864791',
'lastLoginDate': '2015-10-05 12:43:35',
'registrationDate': '2013-04-21 15:12:09',
'registrationAddress': 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
'ip': '85.34.78.112',
'city': 'Istanbul',
'country': 'Turkey',
'zipCode': '34732'
}

address = {
'contactName': 'Jane Doe',
'city': 'Istanbul',
'country': 'Turkey',
'address': 'Nidakule Göztepe, Merdivenköy Mah. Bora Sok. No:1',
'zipCode': '34732'
}

basket_items = [
{
'id': 'BI101',
'name': 'Binocular',
'category1': 'Collectibles',
'category2': 'Accessories',
'itemType': 'PHYSICAL',
'price': '0.3'
},
{
'id': 'BI102',
'name': 'Game code',
'category1': 'Game',
'category2': 'Online Game Items',
'itemType': 'VIRTUAL',
'price': '0.5'
},
{
'id': 'BI103',
'name': 'Usb',
'category1': 'Electronics',
'category2': 'Usb / Cable',
'itemType': 'PHYSICAL',
'price': '0.2'
}
]

request = {
'locale': 'tr',
'conversationId': '123456789',
'price': '1',
'paidPrice': '1.2',
'currency': 'TRY',
'installment': '1',
'basketId': 'B67832',
'paymentChannel': 'WEB',
'paymentGroup': 'PRODUCT',
'paymentCard': payment_card,
'buyer': buyer,
'shippingAddress': address,
'billingAddress': address,
'basketItems': basket_items
}

payment_preauth = iyzipay.PaymentPreAuth()
payment_preauth_result = payment_preauth.create(request, options)
payment_preauth_result_response = json.load(payment_preauth_result)
print('response:', payment_preauth_result_response)

if payment_preauth_result_response['status'] == 'success':
secret_key = options['secret_key']
paymentId = payment_preauth_result_response['paymentId']
currency = payment_preauth_result_response['currency']
basketId = payment_preauth_result_response['basketId']
conversationId = payment_preauth_result_response['conversationId']
paidPrice = payment_preauth.strip_zero(str(payment_preauth_result_response['paidPrice']))
price = payment_preauth.strip_zero(str(payment_preauth_result_response['price']))
signature = payment_preauth_result_response['signature']
payment_preauth.verify_signature([paymentId, currency, basketId, conversationId, paidPrice, price],secret_key, signature)
19 changes: 16 additions & 3 deletions samples/create_threeds_payment.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import iyzipay

options = {
Expand All @@ -10,9 +11,21 @@
'locale': 'tr',
'conversationId': '123456789',
'paymentId': '1',
'conversationData': 'conversation data'
'conversationData': ''
}

threeds_payment = iyzipay.ThreedsPayment().create(request, options)
threeds_payment = iyzipay.ThreedsPayment()
threeds_payment_result = threeds_payment.create(request, options)
threeds_payment_response = json.load(threeds_payment_result)
print('response:', threeds_payment_response)

print(threeds_payment.read().decode('utf-8'))
if threeds_payment_response['status'] == 'success':
secret_key = options['secret_key']
paymentId = threeds_payment_response['paymentId']
currency = threeds_payment_response['currency']
basketId = threeds_payment_response['basketId']
conversationId = threeds_payment_response['conversationId']
paidPrice = threeds_payment.strip_zero(str(threeds_payment_response['paidPrice']))
price = threeds_payment.strip_zero(str(threeds_payment_response['price']))
signature = threeds_payment_response['signature']
threeds_payment.verify_signature([paymentId, currency, basketId, conversationId, paidPrice, price],secret_key, signature)
13 changes: 11 additions & 2 deletions samples/initialize_bkm.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import iyzipay

options = {
Expand Down Expand Up @@ -71,6 +72,14 @@
'basketItems': basket_items
}

bkm_initialize = iyzipay.BkmInitialize().create(request, options)
bkm_initialize = iyzipay.BkmInitialize()
bkm_initialize_result = bkm_initialize.create(request, options)
bkm_initialize_response = json.load(bkm_initialize_result)
print('response:', bkm_initialize_response)

print(bkm_initialize.read().decode('utf-8'))
if bkm_initialize_response['status'] == 'success':
secret_key = options['secret_key']
conversationId = bkm_initialize_response['conversationId']
token = bkm_initialize_response['token']
signature = bkm_initialize_response['signature']
bkm_initialize.verify_signature([token, conversationId], secret_key, signature)
13 changes: 11 additions & 2 deletions samples/initialize_checkout_form.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import iyzipay

options = {
Expand Down Expand Up @@ -73,6 +74,14 @@
'basketItems': basket_items
}

checkout_form_initialize = iyzipay.CheckoutFormInitialize().create(request, options)
checkout_form_initialize = iyzipay.CheckoutFormInitialize()
checkout_form_initialize_result = checkout_form_initialize.create(request, options)
checkout_form_initialize_response = json.load(checkout_form_initialize_result)
print('response:', checkout_form_initialize_response)

print(checkout_form_initialize.read().decode('utf-8'))
if checkout_form_initialize_response['status'] == 'success':
secret_key = options['secret_key']
conversationId = checkout_form_initialize_response['conversationId']
token = checkout_form_initialize_response['token']
signature = checkout_form_initialize_response['signature']
checkout_form_initialize.verify_signature([conversationId, token], secret_key, signature)
Loading

0 comments on commit 2439fb3

Please sign in to comment.