Write a payment gateway for web2py

49 views
Skip to first unread message

Mohammad Rostami

unread,
May 26, 2019, 8:23:35 AM5/26/19
to web2py-users
I'm going to write an Iranian payment gateway for the Web2py. I received information from the bank. 

I have a key that contains 56 characters. And exactly that should be the same value, that is, it can not shorten the key. This key is encrypted in base64 format. If there is a way that the key is shorter, the actual value of the key should not be changed. Now I need to encrypt a string with the triple(DES3, pkcs7) algorithm using this key and send it in base64 format.


Let me emphasize that the key is used as an identifier.And I can not use another algorithm.


init_key = "YTAzZTYyNDNiMTljMzg0YzYxY2NhMGU4NjU1ODc2N2FkYTAwMGJiOQ==" 


We got some fairly good function by guiding friends. I will code it below. But in this function, the key has been shortened to make no error. I read about this algorithm by writing an input of 56 bytes or ... .

Apparently this key is correct. My method is probably wrong, or it has to shorten the key, for example, on that loop. Because the same key is used with the PHP code as follows.

function encrypt_pkcs7 ($str, $key)
    {
        $key = base64_decode($key);
        $cipherText = OpenSSL_encrypt($str, "DES-EDE3", $key, 
        OPENSSL_RAW_DATA);
        return base64_encode($cipherText);
    }



Now I'm asking you to give me guidance on how to implement this problem. Maybe I should use a module other than DES3. Thank you for being sympathetic to me.


def pad(text,pad_size=16):
    text_length = len(text)
    last_block_size = text_length % pad_size
    remaining_space = pad_size - last_block_size

    text = text + '='*remaining_space

    return text

def encrypt_DES3(terminal_id,order_id,amount):
    """

    :param terminal_id: String-for example: EUDuTQrp
    :param order_id: integer- for example: 123456
    :param amount: integer - for example: 60000
    :return: encrypt "terminal_id;oreder_id;integer"
    """
    secret_key_text = "YTAzZTYyND122331"   ## you can only have key of size 16 or 24
    text = terminal_id + ';' + str(order_id) + ';' + str(amount)
    text = pad(text,8)
    cipher = DES3.new(secret_key_text, DES3.MODE_ECB)
    my_export = cipher.encrypt(text)

    return base64.b64encode(my_export)



But the error that I continue to receive is this.




File "<pyshell#18>", line 1, in <module>
encrypt_DES3('EUDuTQrp',123456,60000)
File "<pyshell#17>", line 17, in encrypt_DES3
cipher = DES3.new(key, DES3.MODE_ECB)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/DES3.py", line 
113, in new
return DES3Cipher(key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/DES3.py", line 
76, in __init__
blockalgo.BlockAlgo.__init__(self, _DES3, key, *args, **kwargs)
File "/usr/lib/python3/dist-packages/Crypto/Cipher/blockalgo.py", 
line 141, in __init__
self._cipher = factory.new(key, *args, **kwargs)
ValueError: Invalid key size (must be either 16 or 24 bytes long)








pbreit

unread,
May 27, 2019, 6:26:24 PM5/27/19
to web2py-users
Does the payment service provide any public documentation?

Does this help?

Mohammad Rostami

unread,
May 28, 2019, 12:38:57 AM5/28/19
to web...@googlegroups.com
Hello. thanks for the reply. Yes. There is documentation, but in PDF format and in Persian language. I attached the documentation file. I have already read the link you sent. It also says that for keys other than 16 bytes and 24 bytes, we get the error.

The program is written in the following php language. I'll link it below. Hope it can help.


I started this gateway open source.

 I also got an account from a simulator site for development. This account is valid for one month. You can also test your localhost. I attach my attempt to write to the gateway. As an plugin. Install the program and add this plugin. Then you will see a form at the address below. Write and submit the price in the form. If everything works, you should connect to the payment port. There are currently no store plans so let's assume that this form is sponsored by the site.

I will also include information about the gateway testing. 

I hope we can provide this gateway and more for Persian speakers.


در سه‌شنبه 28 مهٔ 2019، ساعت 2:56:24 (UTC+4:30)، pbreit نوشته:
راهنماي پياده سازي فرآيند خريد(نسخه پذیرندگان) ویرایش 1.3.pdf
web2py.plugin.melligateway (1).w2p
inform_test_gateway.txt
Reply all
Reply to author
Forward
0 new messages