Skip to content

Commit 76b3476

Browse files
authored
Merge pull request #2 from RootStudio/missing-files
Adds missing runtime files
2 parents 9148806 + 83e5e1a commit 76b3476

File tree

2 files changed

+253
-0
lines changed

2 files changed

+253
-0
lines changed

perch_shop/runtime/forms.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
function perch_shop_form_handler($SubmittedForm)
3+
{
4+
if ($SubmittedForm->validate()) {
5+
6+
$API = new PerchAPI(1.0, 'perch_shop');
7+
$ShopRuntime = PerchShop_Runtime::fetch();
8+
9+
switch($SubmittedForm->formID) {
10+
case 'cart':
11+
$ShopRuntime->update_cart_from_form($SubmittedForm);
12+
break;
13+
14+
case 'discount_code':
15+
$ShopRuntime->set_discount_code_from_form($SubmittedForm);
16+
break;
17+
18+
case 'location':
19+
$ShopRuntime->set_location_from_form($SubmittedForm);
20+
break;
21+
22+
case 'currency':
23+
$ShopRuntime->set_currency_from_form($SubmittedForm);
24+
break;
25+
26+
case 'shipping_method':
27+
$ShopRuntime->set_shipping_method_from_form($SubmittedForm);
28+
break;
29+
30+
case 'add_to_cart':
31+
$ShopRuntime->add_to_cart_from_form($SubmittedForm);
32+
break;
33+
34+
case 'order_address':
35+
$ShopRuntime->set_addresses_from_form($SubmittedForm);
36+
break;
37+
38+
case 'address':
39+
$ShopRuntime->edit_address_from_form($SubmittedForm);
40+
break;
41+
42+
case 'register':
43+
$ShopRuntime->register_customer_from_form($SubmittedForm);
44+
break;
45+
46+
case 'profile':
47+
$ShopRuntime->update_customer_from_form($SubmittedForm);
48+
break;
49+
}
50+
51+
// For all forms
52+
$ShopRuntime->set_cart_properties_from_form($SubmittedForm);
53+
54+
$Tag = $SubmittedForm->get_form_attributes();
55+
if ($Tag && $Tag->next()) {
56+
$Perch = Perch::fetch();
57+
if (!$Perch->get_form_errors($SubmittedForm->formID)) {
58+
PerchUtil::redirect($Tag->next());
59+
}
60+
}
61+
}
62+
63+
$Perch = Perch::fetch();
64+
$errors = $Perch->get_form_errors($SubmittedForm->formID);
65+
if ($errors) PerchUtil::debug($errors);
66+
}
67+
68+
function perch_shop_form($template, $return=false)
69+
{
70+
$API = new PerchAPI(1.0, 'perch_shop');
71+
$Template = $API->get('Template');
72+
$Template->set('shop'.DIRECTORY_SEPARATOR.$template, 'shop');
73+
$html = $Template->render(array());
74+
$html = $Template->apply_runtime_post_processing($html);
75+
76+
if ($return) return $html;
77+
echo $html;
78+
}
79+
80+
function perch_shop_location_form($opts=array(), $return=false)
81+
{
82+
$API = new PerchAPI(1.0, 'perch_shop');
83+
84+
$defaults = [];
85+
$defaults['template'] = 'tax/location_form.html';
86+
87+
if (is_array($opts)) {
88+
$opts = array_merge($defaults, $opts);
89+
}else{
90+
$opts = $defaults;
91+
}
92+
93+
PerchSystem::set_var('locations_list', PerchShop_TaxLocations::get_list_options());
94+
95+
$Template = $API->get('Template');
96+
$Template->set('shop/'.$opts['template'], 'shop');
97+
$html = $Template->render(array());
98+
$html = $Template->apply_runtime_post_processing($html);
99+
100+
if ($return) return $html;
101+
echo $html;
102+
}
103+
104+
function perch_shop_currency_form($opts=array(), $return=false)
105+
{
106+
$API = new PerchAPI(1.0, 'perch_shop');
107+
108+
$defaults = [];
109+
$defaults['template'] = 'currencies/currency_form.html';
110+
111+
if (is_array($opts)) {
112+
$opts = array_merge($defaults, $opts);
113+
}else{
114+
$opts = $defaults;
115+
}
116+
117+
PerchSystem::set_var('currency_list', PerchShop_Currencies::get_list_options());
118+
119+
$Template = $API->get('Template');
120+
$Template->set('shop/'.$opts['template'], 'shop');
121+
$html = $Template->render(array());
122+
$html = $Template->apply_runtime_post_processing($html);
123+
124+
if ($return) return $html;
125+
echo $html;
126+
}
127+
128+
function perch_shop_shipping_method_form($opts=array(), $return=false)
129+
{
130+
$API = new PerchAPI(1.0, 'perch_shop');
131+
132+
$defaults = [];
133+
$defaults['template'] = 'shippings/method_form.html';
134+
135+
if (is_array($opts)) {
136+
$opts = array_merge($defaults, $opts);
137+
}else{
138+
$opts = $defaults;
139+
}
140+
141+
$ShopRuntime = PerchShop_Runtime::fetch();
142+
143+
PerchSystem::set_var('shippings_list', $ShopRuntime->get_shipping_list_options());
144+
145+
$Template = $API->get('Template');
146+
$Template->set('shop/'.$opts['template'], 'shop');
147+
$html = $Template->render(array());
148+
$html = $Template->apply_runtime_post_processing($html);
149+
150+
if ($return) return $html;
151+
echo $html;
152+
}
153+

perch_shop/runtime/gateways.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
function perch_shop_payment_form($gateway, $opts=[], $return=false)
4+
{
5+
switch ($gateway) {
6+
case 'stripe':
7+
return perch_shop_stripe_payment_form($opts, $return);
8+
break;
9+
case 'stripe_intents':
10+
$opts = array_merge([
11+
'template' => 'gateways/stripe_intents_payment_form.html'
12+
], $opts);
13+
return perch_shop_stripe_payment_form($opts, $return);
14+
break;
15+
16+
case 'braintree':
17+
return perch_shop_braintree_payment_form($opts, $return);
18+
break;
19+
}
20+
}
21+
22+
23+
function perch_shop_stripe_payment_form($opts=[], $return=false)
24+
{
25+
$default_opts = [
26+
'template' => 'gateways/stripe_payment_form.html',
27+
'skip-template' => false,
28+
'cache' => true,
29+
'cache-ttl' => 900,
30+
];
31+
32+
$opts = PerchUtil::extend($default_opts, $opts);
33+
34+
if (isset($opts['template'])) {
35+
$opts['template'] = 'shop/'.$opts['template'];
36+
}
37+
38+
if ($opts['skip-template']==true) $return = true;
39+
40+
$API = new PerchAPI(1.0, 'perch_shop');
41+
$Template = $API->get('Template', 'shop');
42+
$Template->set($opts['template'], 'shop');
43+
44+
$Gateway = PerchShop_Gateways::get('stripe');
45+
$config = PerchShop_Config::get('gateways', 'stripe');
46+
$key = $Gateway->get_public_api_key($config);
47+
48+
$ShopRuntime = PerchShop_Runtime::fetch();
49+
50+
$html = $Template->render([
51+
'amount' => floatval($ShopRuntime->get_cart_val('grand_total', [], []))*100,
52+
'amount_formatted' => $ShopRuntime->get_cart_val('grand_total_formatted', [], []),
53+
'currency' => $ShopRuntime->get_cart_val('currency_code', [], []),
54+
'publishable_key' => $key,
55+
'shop_name' => 'Shop',
56+
]);
57+
$r = $Template->apply_runtime_post_processing($html);
58+
59+
if ($return) return $r;
60+
echo $r;
61+
}
62+
63+
function perch_shop_braintree_payment_form($opts=[], $return=false)
64+
{
65+
$default_opts = [
66+
'template' => 'gateways/braintree_payment_form.html',
67+
'skip-template' => false,
68+
'cache' => true,
69+
'cache-ttl' => 900,
70+
];
71+
72+
$opts = PerchUtil::extend($default_opts, $opts);
73+
74+
if (isset($opts['template'])) {
75+
$opts['template'] = 'shop/'.$opts['template'];
76+
}
77+
78+
if ($opts['skip-template']==true) $return = true;
79+
80+
$API = new PerchAPI(1.0, 'perch_shop');
81+
$Template = $API->get('Template', 'shop');
82+
$Template->set($opts['template'], 'shop');
83+
84+
$Gateway = PerchShop_Gateways::get('braintree');
85+
$config = PerchShop_Config::get('gateways', 'braintree');
86+
$key = $Gateway->get_public_api_key($config);
87+
88+
$ShopRuntime = PerchShop_Runtime::fetch();
89+
90+
$html = $Template->render([
91+
'amount' => number_format(floatval($ShopRuntime->get_cart_val('grand_total', [], [])),2, '.', ''),
92+
'amount_formatted' => $ShopRuntime->get_cart_val('grand_total_formatted', [], []),
93+
'currency' => $ShopRuntime->get_cart_val('currency_code', [], []),
94+
'client_token' => $key,
95+
]);
96+
$r = $Template->apply_runtime_post_processing($html);
97+
98+
if ($return) return $r;
99+
echo $r;
100+
}

0 commit comments

Comments
 (0)