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
JavaScript
x
33
33
1
from kivy.app import App
2
from kivy.uix.label import Label
3
from kivy.uix.screenmanager import Screen, ScreenManager ,SlideTransition
4
from kivy.properties import NumericProperty
5
import webbrowser
6
7
y = 0.20
8
class MenuScreen(Screen):
9
def item1(self):
10
global y
11
y -= 0.02
12
App.get_running_app().cart += 7
13
App.get_running_app().root.get_screen("cart").add_widget(Label(text="Item 1", font_size=20, pos_hint={"x": 0, "y": y}))
14
15
class CartScreen(Screen):
16
def PayPal(self):
17
webbrowser.open_new_tab("PayPal.html")
18
19
def menu(self,button):
20
self.manager.transition = SlideTransition(direction="right")
21
self.manager.current = "menu"
22
23
class WindowManager(ScreenManager):
24
pass
25
26
class ExampleApp(App):
27
cart = NumericProperty()
28
def build(self):
29
return WindowManager()
30
31
if __name__ == "__main__":
32
ExampleApp().run()
33
.kv
JavaScript
1
56
56
1
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
2
3
<WindowManager>:
4
MenuScreen:
5
CartScreen:
6
7
<MenuScreen>:
8
name: "menu"
9
10
FloatLayout:
11
Label:
12
text: "MENU"
13
font_size: 40
14
pos_hint: {"x": 0, "y": 0.3}
15
Label:
16
text: "$ " + str(app.cart)
17
font_size: 20
18
Button:
19
text: "Add to cart"
20
size_hint: 0.3,0.08
21
pos_hint: {"x": 0.5, "y": 0.3}
22
on_release:
23
root.item1()
24
Button:
25
text: "Cart"
26
size_hint: 0.3,0.08
27
pos_hint: {"x": 0.5, "y": 0.2}
28
on_release:
29
app.root.transition = SlideTransition(direction = "right")
30
app.root.current = "cart"
31
32
<CartScreen>:
33
name: "cart"
34
35
FloatLayout:
36
Label:
37
text: "CART"
38
font_size: 40
39
pos_hint: {"x": 0, "y": 0.3}
40
Label:
41
text: "$ " + str(app.cart)
42
font_size: 20
43
Button:
44
text: "PayPal Checkout"
45
size_hint: 0.3,0.08
46
pos_hint: {"x": 0.5, "y": 0.3}
47
on_release:
48
root.PayPal()
49
Button:
50
text: "Back"
51
size_hint: 0.3,0.08
52
pos_hint: {"x": 0.5, "y": 0.2}
53
on_release:
54
app.root.transition = SlideTransition(direction = "left")
55
app.root.current = "menu"
56
PayPal HTML file
JavaScript
1
109
109
1
<div id="smart-button-container">
2
<div style="text-align: center"><label for="description"> </label><input type="text" name="descriptionInput" id="description" maxlength="127" value=""></div>
3
<p id="descriptionError" style="visibility: hidden; color:red; text-align: center;">Please enter a description</p>
4
<div style="text-align: center"><label for="amount"> </label><input name="amountInput" type="number" id="amount" value="" ><span> CAD</span></div>
5
<p id="priceLabelError" style="visibility: hidden; color:red; text-align: center;">Please enter a price</p>
6
<div id="invoiceidDiv" style="text-align: center; display: none;"><label for="invoiceid"> </label><input name="invoiceid" maxlength="127" type="text" id="invoiceid" value="" ></div>
7
<p id="invoiceidError" style="visibility: hidden; color:red; text-align: center;">Please enter an Invoice ID</p>
8
<div style="text-align: center; margin-top: 0.625rem;" id="paypal-button-container"></div>
9
</div>
10
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=CAD" data-sdk-integration-source="button-factory"></script>
11
<script>
12
function initPayPalButton() {
13
var description = document.querySelector('#smart-button-container #description');
14
var amount = document.querySelector('#smart-button-container #amount');
15
var descriptionError = document.querySelector('#smart-button-container #descriptionError');
16
var priceError = document.querySelector('#smart-button-container #priceLabelError');
17
var invoiceid = document.querySelector('#smart-button-container #invoiceid');
18
var invoiceidError = document.querySelector('#smart-button-container #invoiceidError');
19
var invoiceidDiv = document.querySelector('#smart-button-container #invoiceidDiv');
20
21
var elArr = [description, amount];
22
23
if (invoiceidDiv.firstChild.innerHTML.length > 1) {
24
invoiceidDiv.style.display = "block";
25
}
26
27
var purchase_units = [];
28
purchase_units[0] = {};
29
purchase_units[0].amount = {};
30
31
function validate(event) {
32
return event.value.length > 0;
33
}
34
35
paypal.Buttons({
36
style: {
37
color: 'black',
38
shape: 'rect',
39
label: 'checkout',
40
layout: 'vertical',
41
42
},
43
44
onInit: function (data, actions) {
45
actions.disable();
46
47
if(invoiceidDiv.style.display === "block") {
48
elArr.push(invoiceid);
49
}
50
51
elArr.forEach(function (item) {
52
item.addEventListener('keyup', function (event) {
53
var result = elArr.every(validate);
54
if (result) {
55
actions.enable();
56
} else {
57
actions.disable();
58
}
59
});
60
});
61
},
62
63
onClick: function () {
64
if (description.value.length < 1) {
65
descriptionError.style.visibility = "visible";
66
} else {
67
descriptionError.style.visibility = "hidden";
68
}
69
70
if (amount.value.length < 1) {
71
priceError.style.visibility = "visible";
72
} else {
73
priceError.style.visibility = "hidden";
74
}
75
76
if (invoiceid.value.length < 1 && invoiceidDiv.style.display === "block") {
77
invoiceidError.style.visibility = "visible";
78
} else {
79
invoiceidError.style.visibility = "hidden";
80
}
81
82
purchase_units[0].description = description.value;
83
purchase_units[0].amount.value = amount.value;
84
85
if(invoiceid.value !== '') {
86
purchase_units[0].invoice_id = invoiceid.value;
87
}
88
},
89
90
createOrder: function (data, actions) {
91
return actions.order.create({
92
purchase_units: purchase_units,
93
});
94
},
95
96
onApprove: function (data, actions) {
97
return actions.order.capture().then(function (details) {
98
alert('Transaction completed by ' + details.payer.name.given_name + '!');
99
});
100
},
101
102
onError: function (err) {
103
console.log(err);
104
}
105
}).render('#paypal-button-container');
106
}
107
initPayPalButton();
108
</script>
109
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.