I was wondering if it is possible to integrate a PayPal checkout into my kivy app? I want the total to be determined based on a variable in my python kivy code named cart
. So far, I haven’t seen anything online with this subject. Any help would be appreciated!
I have this simple code that can describe more what I want to achieve
.py
from kivy.app import App from kivy.uix.label import Label from kivy.uix.screenmanager import Screen, ScreenManager ,SlideTransition from kivy.properties import NumericProperty import webbrowser y = 0.20 class MenuScreen(Screen): def item1(self): global y y -= 0.02 App.get_running_app().cart += 7 App.get_running_app().root.get_screen("cart").add_widget(Label(text="Item 1", font_size=20, pos_hint={"x": 0, "y": y})) class CartScreen(Screen): def PayPal(self): webbrowser.open_new_tab("PayPal.html") def menu(self,button): self.manager.transition = SlideTransition(direction="right") self.manager.current = "menu" class WindowManager(ScreenManager): pass class ExampleApp(App): cart = NumericProperty() def build(self): return WindowManager() if __name__ == "__main__": ExampleApp().run()
.kv
#:import SlideTransition kivy.uix.screenmanager.SlideTransition <WindowManager>: MenuScreen: CartScreen: <MenuScreen>: name: "menu" FloatLayout: Label: text: "MENU" font_size: 40 pos_hint: {"x": 0, "y": 0.3} Label: text: "$ " + str(app.cart) font_size: 20 Button: text: "Add to cart" size_hint: 0.3,0.08 pos_hint: {"x": 0.5, "y": 0.3} on_release: root.item1() Button: text: "Cart" size_hint: 0.3,0.08 pos_hint: {"x": 0.5, "y": 0.2} on_release: app.root.transition = SlideTransition(direction = "right") app.root.current = "cart" <CartScreen>: name: "cart" FloatLayout: Label: text: "CART" font_size: 40 pos_hint: {"x": 0, "y": 0.3} Label: text: "$ " + str(app.cart) font_size: 20 Button: text: "PayPal Checkout" size_hint: 0.3,0.08 pos_hint: {"x": 0.5, "y": 0.3} on_release: root.PayPal() Button: text: "Back" size_hint: 0.3,0.08 pos_hint: {"x": 0.5, "y": 0.2} on_release: app.root.transition = SlideTransition(direction = "left") app.root.current = "menu"
PayPal HTML file
<div id="smart-button-container"> <div style="text-align: center"><label for="description"> </label><input type="text" name="descriptionInput" id="description" maxlength="127" value=""></div> <p id="descriptionError" style="visibility: hidden; color:red; text-align: center;">Please enter a description</p> <div style="text-align: center"><label for="amount"> </label><input name="amountInput" type="number" id="amount" value="" ><span> CAD</span></div> <p id="priceLabelError" style="visibility: hidden; color:red; text-align: center;">Please enter a price</p> <div id="invoiceidDiv" style="text-align: center; display: none;"><label for="invoiceid"> </label><input name="invoiceid" maxlength="127" type="text" id="invoiceid" value="" ></div> <p id="invoiceidError" style="visibility: hidden; color:red; text-align: center;">Please enter an Invoice ID</p> <div style="text-align: center; margin-top: 0.625rem;" id="paypal-button-container"></div> </div> <script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=CAD" data-sdk-integration-source="button-factory"></script> <script> function initPayPalButton() { var description = document.querySelector('#smart-button-container #description'); var amount = document.querySelector('#smart-button-container #amount'); var descriptionError = document.querySelector('#smart-button-container #descriptionError'); var priceError = document.querySelector('#smart-button-container #priceLabelError'); var invoiceid = document.querySelector('#smart-button-container #invoiceid'); var invoiceidError = document.querySelector('#smart-button-container #invoiceidError'); var invoiceidDiv = document.querySelector('#smart-button-container #invoiceidDiv'); var elArr = [description, amount]; if (invoiceidDiv.firstChild.innerHTML.length > 1) { invoiceidDiv.style.display = "block"; } var purchase_units = []; purchase_units[0] = {}; purchase_units[0].amount = {}; function validate(event) { return event.value.length > 0; } paypal.Buttons({ style: { color: 'black', shape: 'rect', label: 'checkout', layout: 'vertical', }, onInit: function (data, actions) { actions.disable(); if(invoiceidDiv.style.display === "block") { elArr.push(invoiceid); } elArr.forEach(function (item) { item.addEventListener('keyup', function (event) { var result = elArr.every(validate); if (result) { actions.enable(); } else { actions.disable(); } }); }); }, onClick: function () { if (description.value.length < 1) { descriptionError.style.visibility = "visible"; } else { descriptionError.style.visibility = "hidden"; } if (amount.value.length < 1) { priceError.style.visibility = "visible"; } else { priceError.style.visibility = "hidden"; } if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") { invoiceidError.style.visibility = "visible"; } else { invoiceidError.style.visibility = "hidden"; } purchase_units[0].description = description.value; purchase_units[0].amount.value = amount.value; if(invoiceid.value !== '') { purchase_units[0].invoice_id = invoiceid.value; } }, createOrder: function (data, actions) { return actions.order.create({ purchase_units: purchase_units, }); }, onApprove: function (data, actions) { return actions.order.capture().then(function (details) { alert('Transaction completed by ' + details.payer.name.given_name + '!'); }); }, onError: function (err) { console.log(err); } }).render('#paypal-button-container'); } initPayPalButton(); </script>
Advertisement
Answer
PayPal and similar services has an api which you should be able to communicate with using kivy.network.urlrequest module. Or other option is to use Checkout-Python-SDK refer to the link https://github.com/paypal/Checkout-Python-SDK.