Advertisement
joshng

pu

May 7th, 2017
538
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import os
  3. import random
  4. from traceback import print_exc
  5. from suds.client import Client
  6. from suds.client import Client, sys
  7. from suds.sax.element import Element
  8. from suds.sax.attribute import Attribute
  9. from suds.xsd.sxbasic import Import
  10. from suds.wsse import UsernameToken, Security, Token, Timestamp
  11. import logging
  12. logging.getLogger('suds.client').setLevel(logging.DEBUG)
  13. from suds.plugin import MessagePlugin
  14. import uuid
  15. import json
  16.  
  17.  
  18. def payuMeaDoTransactionApiCall(args):
  19.  
  20. urlToQuery = 'https://secure.payu.co.za/service/PayUAPI?wsdl'
  21. if (args['store']['environment'] == 'staging') :
  22. urlToQuery = 'https://staging.payu.co.za/service/PayUAPI?wsdl'
  23.  
  24. class LogPlugin(MessagePlugin):
  25. #def sending(self, context):
  26. # print(str(context.envelope))
  27. def received(self, context):
  28. #print(str(context.reply))
  29. return str(context.reply)
  30.  
  31. #------------------------------------- CREATING CLIENT OBJECT--------------------------------------
  32. client = Client(urlToQuery, faults=False, plugins=[LogPlugin()])
  33.  
  34. #------------------------------------- CREATING CUSTOM HEADER--------------------------------------
  35. wsse = ('wsse','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd')
  36.  
  37. mustAttributeSecurity = Attribute('SOAP-ENV:mustUnderstand', '1')
  38. addressAttributeSecurity = Attribute('xmlns:wsse', 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd')
  39. AttributeUsernameToken1 = Attribute('wsu:Id','UsernameToken-9')
  40. addressAttributeUsernameToken = Attribute('xmlns:wsu','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd')
  41. addressAttributePassword = Attribute('Type','http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText')
  42.  
  43. msgUsername = Element('Username', ns=wsse).setText(args['store']['soapUsername'])
  44. msgPassword = Element('Password', ns=wsse).setText(args['store']['soapPassword']).append(addressAttributePassword)
  45.  
  46. msgUsernameToken = Element('UsernameToken', ns=wsse)
  47. msgUsernameToken.append(AttributeUsernameToken1)
  48. msgUsernameToken.append(addressAttributeUsernameToken)
  49. msgUsernameToken.insert(msgPassword).insert(msgUsername)
  50.  
  51. msgSecurity = Element('Security', ns=wsse).addPrefix(p='SOAP-ENC', u='http://www.w3.org/2003/05/soap-encoding')
  52. msgSecurity.append(mustAttributeSecurity)
  53. msgSecurity.append(addressAttributeSecurity)
  54. msgSecurity.insert(msgUsernameToken)
  55.  
  56. client.set_options(soapheaders=[msgSecurity])
  57.  
  58. #------------------------------------- CREATING SOAP CALL DETAILS HERE--------------------------------------
  59. transaction = {}
  60. transaction['Api'] = 'ONE_ZERO';
  61. transaction['Safekey'] = args['store']['safekey'];
  62. transaction['TransactionType'] = 'PAYMENT';
  63. transaction['AdditionalInformation'] = args['additionalInformation']
  64. transaction['Basket'] = args['basket']
  65. transaction['Customer'] = args['customer']
  66. transaction['Creditcard'] = args['creditcard']
  67.  
  68. #------------------------------------- DOING SOAP CALL HERE--------------------------------------
  69. try:
  70. setTransaction = client.service.doTransaction(** transaction)
  71. trx_details = (setTransaction)[1]
  72. message = trx_details['displayMessage']
  73. payUReference = trx_details['payUReference']
  74. amountInCents = trx_details['paymentMethodsUsed'][0]['amountInCents']
  75.  
  76. print amountInCents
  77.  
  78. except Exception, e:
  79. print "----------------"
  80. print 'type is:', e.__class__.__name__
  81. print_exc()
  82. print "----------------"
  83.  
  84.  
  85. if __name__ == '__main__':
  86. try:
  87. doTransactionDetails = {}
  88. doTransactionDetails['store'] = {}
  89. doTransactionDetails['store']['soapUsername'] = '200208 '
  90. doTransactionDetails['store']['soapPassword'] = 'g1Kzk8GY'
  91. doTransactionDetails['store']['safekey'] = '{A580B3C7-3EF3-47F1-9B90-4047CE0EC54C}'
  92. doTransactionDetails['store']['environment'] = 'staging'
  93.  
  94. doTransactionDetails['additionalInformation'] = {}
  95. doTransactionDetails['additionalInformation']['merchantReference'] = '02345670123' #random.randrange(1,10+1)
  96. doTransactionDetails['additionalInformation']['payUReference'] = str(uuid.uuid4())[:11]#11999149346
  97. doTransactionDetails['additionalInformation']['secure3d'] = False
  98.  
  99. doTransactionDetails['basket'] = {}
  100. doTransactionDetails['basket']['description'] = "Product Description"
  101. doTransactionDetails['basket']['amountInCents'] = "10000"
  102. doTransactionDetails['basket']['currencyCode'] = 'NGN'
  103.  
  104. doTransactionDetails['creditcard'] = {}
  105. doTransactionDetails['creditcard']['nameOnCard'] = "Mr John Doe"
  106. doTransactionDetails['creditcard']['cardNumber'] = "5453010000064261"
  107. doTransactionDetails['creditcard']['cardExpiry'] = "052017"
  108. doTransactionDetails['creditcard']['cvv'] = "123"
  109. doTransactionDetails['creditcard']['amountInCents'] = doTransactionDetails['basket']['amountInCents']
  110.  
  111. doTransactionDetails['customer'] = {}
  112. doTransactionDetails['customer']['email'] = "john@doe.com"
  113. doTransactionDetails['customer']['firstName'] = 'John'
  114. doTransactionDetails['customer']['lastName'] = 'Doe'
  115. doTransactionDetails['customer']['mobile'] = '012345678'#'+2347035706380'
  116.  
  117. payuMeaDoTransactionApiCall(doTransactionDetails)
  118.  
  119. except Exception, e:
  120. print 'type is:', e.__class__.__name__
  121. print_exc()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement