Package: mobilpy
mobilPymobilPy helps you with everything you need to implementmobilPay’s online payment
solution. Steps: - create an account - get approved - create a merchant
- download keys (private and public) - copy the signature (just a
string)NOTEYou will have two private keys: for testing and production. The
public key is the same.InstallTo install the library, run:pipinstallmobilpyUsagefrommobilpyimportClient# the signature for your accountsignature='DLAN-1R5V-19EN-XXXX-NFJA'# the path to the public keypublic_key="./sandbox.DLAN-1R5V-19EN-XXXX-NFJA.public.cer"# the private_keyprivate_key="./sandbox.DLAN-1R5V-19EN-XXXX-NFJA.private.key"client=Client(signature=signature,public_key=public_key,private_key=private_key)# optional dict containing details about the customer# if they are sent, the customer will have a short checkout by skipping the second step in the payment flowbilling_details={"first_name":"","last_name":"","address":"","phone":"","email":""}# optional dict with details that you would need internally# these are returned when the webhook is calledparams={"subscription_id":"","basket_id":"",etc.}options={"order_id":""# int/string, max 64 length"currency":"RON",# string, RON or other"amount":1,# float, between 0.10 and 99999"customer_id":"",# int/string"details":"",# string, description for this transaction"billing":billing_details,# dict, OPTIONAL"params":params,# dict, OPTIONAL,# the webhook where the response from mobilPay will be sent"confirm_url":"",# the url where the user will be redirected"return_url":""}response=client.create_payment_data(**options)Theresponseis an dict that has two keys:env_keyanddata.
These need to be used in the front end and make the request to mobilPay.For example, the HTML might look like this:<formaction="http://sandboxsecure.mobilpay.ro"method="post"><inputtype="hidden"name="env_key"value="{{ env_key }}"><inputtype="hidden"name="data"value="{{ data }}"><inputtype="submit"value="Send"></form>The POST urls for the form are:testing:http://sandboxsecure.mobilpay.roproduction:https://secure.mobilpay.roWebhookmobilPay will make aPOSTrequest to the url you set asconfirm_url. mobilPy has methods to help you parse it and offer a
response.post=# get the post dataenv_key=post.get('env_key')data=post.get('data')client=Client(signature=signature,public_key=public_key,private_key=private_key)request_xml=client.decrypt_message(env_key,data)request_object=client.parse_webhook_request(request_xml)# do some magicIn order to check if the transaction was successful you need to checkerror_codeANDaction:transaction_successful=request_object.get('error_code')=='0'andrequest_object.get('action')=='confirmed'iftransaction_successful:# everything is okCreating a responseYou need to let mobilPay know if everything is ok on your end or if
something weird happened. If everything is ok you can create a response
like this:message="All good captain"response_xml=client.create_reponse(message=message)If you had an error:# message that will help you debug. it will appear in your dashboardmessage="Everything is on fire"# this can be "1" (temporary error) or "2" (permanent error)error_type="1"# your internal error code. a number maybe. OPTIONALerror_code=''response_xml=client.create_reponse(message=message,error_type=error_type,error_code=error_code)The response doesn’t need to be encrypted. Just respond with the xml.CreditingIf a transaction was credited from the Dashboard, mobilPay will make a
new webhookPOST. You can check for that:# if the transaction was credited from mobilpayifrequest_object.get('action')=='credit':# do something# return a reponseTODOSome things that still need to be done:testsadd support for: instalements, recurrencemaybe add prefilled credit card data payments?DisclaimerThis library is not associated in any way with mobilPay