借助第三方库m2crypto,在linux下安装使用sudo apt-get install m2crypto即可。
实现方法:
paypal代码
相关的PIN代码,网上很多,不赘述了。
from M2Crypto import BIO, SMIME, X509
#from django.conf import settings
#DKY7UM4QRRD5Y
#seller_1301033066_per@163.com
def paypal_encrypt(attributes):
plaintext = ''
for key, value in attributes.items():
plaintext += u'%s=%s\n' % (key, value)
plaintext = plaintext.encode('utf-8')
# Instantiate an SMIME object.
s = SMIME.SMIME()
# Load signer's key and cert. Sign the buffer.
# s.load_key_bio(BIO.openfile(settings.MY_KEYPAIR), BIO.openfile(settings.MY_CERT))
s.load_key_bio(BIO.openfile("facebook-ppy-prvkey.pem"), BIO.openfile("facebook-ppy-pubcert.pem"))
p7 = s.sign(BIO.MemoryBuffer(plaintext), flags=SMIME.PKCS7_BINARY)
# Load target cert to encrypt the signed message to.
x509 = X509.load_cert_bio(BIO.openfile("paypal_cert_pem_mouren.txt"))
sk = X509.X509_Stack()
sk.push(x509)
s.set_x509_stack(sk)
# Set cipher: 3-key triple-DES in CBC mode.
s.set_cipher(SMIME.Cipher('des_ede3_cbc'))
# Create a temporary buffer.
tmp = BIO.MemoryBuffer()
# Write the signed message into the temporary buffer.
p7.write_der(tmp)
# Encrypt the temporary buffer.
p7 = s.encrypt(tmp, flags=SMIME.PKCS7_BINARY)
# Output p7 in mail-friendly format.
out = BIO.MemoryBuffer()
p7.write(out)
return out.read()
def pay(request, invoice_id):
"""This view displays an encrypted PayPal 'buy now' button"""
# invoice = get_object_or_404(Invoice, id = 1)
attributes = {}
attributes['cmd'] = '_xclick'
attributes['business'] = 'xxx.li@xxx.com'
# attributes['item_name'] = invoice.item_name
attributes['item_name'] = 'test'
# attributes['amount'] = invoice.amount
attributes['amount'] = 5
attributes['currency_code'] = 'GBP'
attributes['cert_id'] = 'DKY7UM4QRRD5Y'
encrypted_block = paypal_encrypt(attributes)
return render_to_response('pay.html', {'encrypted_block': encrypted_block})
#<form target="paypal" action="https://www.paypal.com/cgi-bin/webscr" method="post">
# <input type="hidden" name="cmd" value="_s-xclick" />
# <input type="hidden" name="encrypted" value="{{ encrypted_block }}" />
# <input class="button pay" type="submit" name="submit" value="Pay Invoice" />
#</form>
if __name__ == "__main__":
attributes = {}
attributes['cmd'] = '_xclick'
attributes['business'] = 'seller_1301033066_per@163.com'
attributes['item_name'] = 'test'
attributes['amount'] = 10
attributes['currency_code'] = 'USD'
attributes['cert_id'] = '494G534LA7XZA'
encrypted_block = paypal_encrypt(attributes)
print encrypted_block