class TestCardToken(unittest.TestCase): """ Test Module: Card Token """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966" ) def test_all(self): """ Test Function: Card Token """ card_token_object = { "card_number": "4074090000000004", "security_code": "123", "expiration_year": datetime.now().strftime("%Y"), "expiration_month": "12", "cardholder": { "name": "APRO", "identification": { "CPF": "19119119100" } } } card_token_created = self.sdk.card_token().create(card_token_object) self.assertEqual(card_token_created["status"], 201) self.assertEqual( self.sdk.card_token().get( card_token_created["response"]["id"])["status"], 200)
def mercadopago_pix(request): print(f'antes do sdk') sdk = mercadopago.SDK( "TEST-4458147267707345-101313-a90d00320a2823cb5a9161348e5ebcf6-98517282") print(f'sdk') # alunoId = request.data['alunoId'] payment_data = { "transaction_amount": 100, "description": "Título do produto", "payment_method_id": "pix", "payer": { "email": "*****@*****.**", "first_name": "Test", "last_name": "User", "identification": { "type": "CPF", "number": "191191191-00" }, "address": { "zip_code": "06233-200", "street_name": "Av. das Nações Unidas", "street_number": "3003", "neighborhood": "Bonfim", "city": "Osasco", "federal_unit": "SP" } } } print(f'payment_daya = {payment_data}') payment_response = sdk.payment().create(payment_data) payment = payment_response["response"] print(f'payment = {payment}') return Response(payment)
def save(self): cd = self.cleaned_data mp = mercadopago.SDK(settings.MERCADO_PAGO_ACCESS_TOKEN) payment_data = { "transaction_amount": float(self.order.get_total_price()), "token": cd["token"], "description": self.order.get_description(), "installments": cd["installments"], "payment_method_id": cd["payment_method_id"], "payer": { "email": cd["email"], "identification": {"type": "CPF", "number": cd["doc_number"]}, }, } payment = mp.payment().create(payment_data) if payment["status"] == 201: self.instance.order = self.order self.instance.mercado_pago_id = payment["response"]["id"] self.instance.mercado_pago_status_detail = payment["response"][ "status_detail" ] self.instance.mercado_pago_status = payment["response"]["status"] if payment["response"]["status"] == "approved": self.order.paid = True self.order.save() self.instance.save()
class TestPreApproval(unittest.TestCase): """ Test Module: PreApproval """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966" ) def test_create(self): """
class TestPaymentMethods(unittest.TestCase): """ Test Module: Payment Methods """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966") def test_find(self): """ Test Function: Payment Methods """ self.assertEqual(self.sdk.payment_methods().list_all()["status"], 200)
def index(req, **kwargs): preference = { "items": [ { "title": "sdk-python", "quantity": 1, # "currency_id": "ARS", "unit_price": 10.5 }, { "title": "otro", "quantity": 3, # "currency_id": "ARS", "unit_price": 30.5 }, { "title": "nuevo", "quantity": 4, # "currency_id": "ARS", "unit_price": 500 } ], 'back_urls': { 'pending': 'www.gmail.com', 'success': 'http://localhost:5000/', 'failure': 'http://www.google.com' }, 'auto_return': 'approved' } sdk = mercadopago.SDK("ACCESS_TOKEN") #mp = mercadopago.SDK("CLIENT_ID", "CLIENT_SECRET") preferenceResult = sdk.preference().create(preference) print(preferenceResult) url = preferenceResult["response"]["sandbox_init_point"] output = """ <!doctype html> <html> <head> <title>MercadoPago SDK - Create Preference and Show Checkout Example</title> </head> <body> <a href="{url}" name="MP-Checkout" class="blue-l-arall-rn">Pagar</a> <script type="text/javascript" src="//resources.mlstatic.com/mptools/render.js"></script> </body> </html> """.format(url=url) return url
class TestIdentificationType(unittest.TestCase): """ Test Module: Identification Type """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966" ) def test_find_all(self): """ Test Function: Identification Type """ identifications = self.sdk.identification_type().list_all() self.assertEqual(identifications["status"], 200)
class TestCustomer(unittest.TestCase): """ Test Module: Customer """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966" ) def test_all(self): """ Test Function: Customer """ customer_object = { "email": "*****@*****.**", "first_name": "Rafa", "last_name": "Williner", "phone": { "area_code": "03492", "number": "432334" }, "identification": { "type": "DNI", "number": "29804555" }, "address": { "zip_code": "2300", "street_name": "some street" }, "description": "customer description" } customer_saved = self.sdk.customer().create(customer_object) self.assertEqual(customer_saved["status"], 201) customer_update = self.sdk.customer().update( customer_saved["response"]["id"], {"last_name": "Payer"}) self.assertEqual(customer_update["status"], 200) customer_updated = self.sdk.customer().get( customer_saved["response"]["id"]) self.assertEqual(customer_updated["response"]["last_name"], "Payer") customer_deleted = self.sdk.customer().delete( customer_saved["response"]["id"]) self.assertEqual(customer_deleted["status"], 200)
def save(self): cd = self.cleaned_data mp = mercadopago.SDK(settings.MERCADO_PAGO_ACCESS_TOKEN) if cd["action"] == "payment.updated": mercado_pago_id = cd["data"]["id"] payment = Payment.objects.get(mercado_pago_id=mercado_pago_id) payment_mp = mp.payment().get(mercado_pago_id) payment.mercado_pago_status = payment_mp["response"]["status"] payment.mercado_pago_status_detail = payment_mp["response"]["status_detail"] if payment_mp["response"]["status"] == "approved": payment.order.paid = True else: payment.order.paid = False payment.order.save() payment.save()
def mercado_pago(request): sdk = mercadopago.SDK( "TEST-7464376773457318-041401-5b77309039187c1aa6183f465644f152-742154279" ) preference_data = { "items": [{ "title": "Prueba", "quantity": 1, "currency_id": "MXN", "unit_price": 100.00, }], "back_urls": [{ "success": "http://127.0.0.1:8000/checkout/hecho", "failure": "http://127.0.0.1:8000/checkout/cancelado", "pending": "http://127.0.0.1:8000/checkout/cancelado", }], "auto_return": "approved" } preference_response = sdk.preference().create(preference_data) preference = preference_response["response"]
class TestPreference(unittest.TestCase): """ Test Module: Preference """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966" ) def test_all(self): """ Test Module: Preference """ preference_object = { "items": [{ "description": "Test Update Success", "id": "456", "picture_url": "http://product1.image.png", "quantity": 1, "title": "Item 1", "currency_id": "BRL", "unit_price": 20.5 }] } preference_saved = self.sdk.preference().create(preference_object) self.assertEqual(preference_saved["status"], 201) preference_object["items"][0]["title"] = "Testando 1 2 3" preference_update = self.sdk.preference().update( preference_saved["response"]["id"], preference_object) self.assertEqual(preference_update["status"], 200) preference_saved = self.sdk.preference().get( preference_saved["response"]["id"]) self.assertEqual(preference_saved["response"]["items"][0]["title"], preference_object["items"][0]["title"])
class TestUser(unittest.TestCase): #pylint: disable=missing-class-docstring sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966") def test_find_user(self): #pylint: disable=missing-function-docstring self.assertEqual(self.sdk.user().get()["status"], 200)
import mercadopago #Creamos una preferencia delaArticulo # Agrego el SDK DE PYTHON sdk = mercadopago.SDK("8714496216339736") payment_data = { "transaction_amount": float(request.POST.get("transaction_amount")), "token": request.POST.get("token"), "description": request.POST.get("description"), "installments": int(request.POST.get("installments")), "payment_method_id": request.POST.get("payment_method_id"), "payer": { "email": request.POST.get("email"), "identification": { "type": request.POST.get("type"), "number": request.POST.get("number") } } } preference_data = { "items": [ { "title": "Cursos", "quantity": , "unit_price": } ] }
class TestMerchantOrder(unittest.TestCase): """ Test Module: Merchant Order """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966" ) def test_all(self): """ Test Function: Merchant Order """ preference_object = { "items": [{ "description": "Test Update Success", "id": "5678", "picture_url": "http://product1.image.png", "quantity": 1, "title": "Item 1", "currency_id": "R$", "unit_price": 20.5 }] } preference_saved = self.sdk.preference().create(preference_object) merchant_order_object = { "preference_id": preference_saved["response"]["id"], "site_id": "MLB", "notification_url": "https://seller/notification", "additional_info": "Aditional info", "external_reference": str(uuid.uuid4().int), "marketplace": "NONE", "items": [{ "description": "Test Update Success", "id": "5678", "picture_url": "http://product1.image.png", "quantity": 1, "title": "Item 1", "currency_id": "BRL", "unit_price": 20.5 }] } merchant_order_created = self.sdk.merchant_order().create( merchant_order_object) self.assertEqual(merchant_order_created["status"], 201) merchant_order_updated = self.sdk.merchant_order().update( merchant_order_created["response"]["id"], {"additional_info": "Info 2"}) self.assertEqual(merchant_order_updated["status"], 200) merchant_order_finded = self.sdk.merchant_order().get( merchant_order_created["response"]["id"]) self.assertEqual(merchant_order_finded["status"], 200) self.assertEqual(merchant_order_finded["response"]["additional_info"], "Info 2")
class TestPayment(unittest.TestCase): """ Test Module: Payment """ sdk = mercadopago.SDK( "APP_USR-558881221729581-091712-44fdc612e60e3e638775d8b4003edd51-471763966") def test_create_and_find(self): """ Test Function: Payment """ card_token_object = { "card_number": "4074090000000004", "security_code": "123", "expiration_year": datetime.now().strftime("%Y"), "expiration_month": "12", "cardholder": { "name": "APRO", "identification": { "CPF": "19119119100" } } } card_token_created = self.sdk.card_token().create(card_token_object) payment_object = { "token": card_token_created["response"]["id"], "installments":1, "transaction_amount":58.80, "description":"Point Mini a maquininha que dá o dinheiro de suas vendas na hora", "payment_method_id":"visa", "payer":{ "email":"*****@*****.**", "identification": { "number": "19119119100", "type": "CPF" } }, "notification_url":"https://www.suaurl.com/notificacoes/", "sponsor_id": None, "binary_mode": False, "external_reference":"MP0001", "statement_descriptor":"MercadoPago", "additional_info":{ "items":[ { "id":"PR0001", "title":"Point Mini", "description": "Producto Point para cobros con tarjetas mediante bluetooth", "picture_url":"https://http2.mlstatic.com/resources/frontend/statics/growth-sellers-landings/[email protected]", #pylint: disable=line-too-long "category_id": "electronics", "quantity":1, "unit_price":58.80 } ], "payer":{ "first_name":"Nome", "last_name":"Sobrenome", "address":{ "zip_code":"06233-200", "street_name":"Av das Nacoes Unidas", "street_number":3003 }, "registration_date":"2019-01-01T12:01:01.000-03:00", "phone":{ "area_code":"011", "number":"987654321" } }, "shipments":{ "receiver_address":{ "street_name":"Av das Nacoes Unidas", "street_number":3003, "zip_code":"06233200", "city_name": "Buzios", "state_name": "Rio de Janeiro" } } } } payment_created = self.sdk.payment().create(payment_object) self.assertEqual(payment_created["status"], 201) payment_found = self.sdk.payment().get(payment_created["response"]["id"]) self.assertEqual(payment_found["status"], 200)