Skip to content

Commit

Permalink
Merge pull request #2 from RootStudio/missing-files
Browse files Browse the repository at this point in the history
Adds missing runtime files
  • Loading branch information
byron-roots authored Sep 1, 2020
2 parents 9148806 + 83e5e1a commit 76b3476
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
153 changes: 153 additions & 0 deletions perch_shop/runtime/forms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php
function perch_shop_form_handler($SubmittedForm)
{
if ($SubmittedForm->validate()) {

$API = new PerchAPI(1.0, 'perch_shop');
$ShopRuntime = PerchShop_Runtime::fetch();

switch($SubmittedForm->formID) {
case 'cart':
$ShopRuntime->update_cart_from_form($SubmittedForm);
break;

case 'discount_code':
$ShopRuntime->set_discount_code_from_form($SubmittedForm);
break;

case 'location':
$ShopRuntime->set_location_from_form($SubmittedForm);
break;

case 'currency':
$ShopRuntime->set_currency_from_form($SubmittedForm);
break;

case 'shipping_method':
$ShopRuntime->set_shipping_method_from_form($SubmittedForm);
break;

case 'add_to_cart':
$ShopRuntime->add_to_cart_from_form($SubmittedForm);
break;

case 'order_address':
$ShopRuntime->set_addresses_from_form($SubmittedForm);
break;

case 'address':
$ShopRuntime->edit_address_from_form($SubmittedForm);
break;

case 'register':
$ShopRuntime->register_customer_from_form($SubmittedForm);
break;

case 'profile':
$ShopRuntime->update_customer_from_form($SubmittedForm);
break;
}

// For all forms
$ShopRuntime->set_cart_properties_from_form($SubmittedForm);

$Tag = $SubmittedForm->get_form_attributes();
if ($Tag && $Tag->next()) {
$Perch = Perch::fetch();
if (!$Perch->get_form_errors($SubmittedForm->formID)) {
PerchUtil::redirect($Tag->next());
}
}
}

$Perch = Perch::fetch();
$errors = $Perch->get_form_errors($SubmittedForm->formID);
if ($errors) PerchUtil::debug($errors);
}

function perch_shop_form($template, $return=false)
{
$API = new PerchAPI(1.0, 'perch_shop');
$Template = $API->get('Template');
$Template->set('shop'.DIRECTORY_SEPARATOR.$template, 'shop');
$html = $Template->render(array());
$html = $Template->apply_runtime_post_processing($html);

if ($return) return $html;
echo $html;
}

function perch_shop_location_form($opts=array(), $return=false)
{
$API = new PerchAPI(1.0, 'perch_shop');

$defaults = [];
$defaults['template'] = 'tax/location_form.html';

if (is_array($opts)) {
$opts = array_merge($defaults, $opts);
}else{
$opts = $defaults;
}

PerchSystem::set_var('locations_list', PerchShop_TaxLocations::get_list_options());

$Template = $API->get('Template');
$Template->set('shop/'.$opts['template'], 'shop');
$html = $Template->render(array());
$html = $Template->apply_runtime_post_processing($html);

if ($return) return $html;
echo $html;
}

function perch_shop_currency_form($opts=array(), $return=false)
{
$API = new PerchAPI(1.0, 'perch_shop');

$defaults = [];
$defaults['template'] = 'currencies/currency_form.html';

if (is_array($opts)) {
$opts = array_merge($defaults, $opts);
}else{
$opts = $defaults;
}

PerchSystem::set_var('currency_list', PerchShop_Currencies::get_list_options());

$Template = $API->get('Template');
$Template->set('shop/'.$opts['template'], 'shop');
$html = $Template->render(array());
$html = $Template->apply_runtime_post_processing($html);

if ($return) return $html;
echo $html;
}

function perch_shop_shipping_method_form($opts=array(), $return=false)
{
$API = new PerchAPI(1.0, 'perch_shop');

$defaults = [];
$defaults['template'] = 'shippings/method_form.html';

if (is_array($opts)) {
$opts = array_merge($defaults, $opts);
}else{
$opts = $defaults;
}

$ShopRuntime = PerchShop_Runtime::fetch();

PerchSystem::set_var('shippings_list', $ShopRuntime->get_shipping_list_options());

$Template = $API->get('Template');
$Template->set('shop/'.$opts['template'], 'shop');
$html = $Template->render(array());
$html = $Template->apply_runtime_post_processing($html);

if ($return) return $html;
echo $html;
}

100 changes: 100 additions & 0 deletions perch_shop/runtime/gateways.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

function perch_shop_payment_form($gateway, $opts=[], $return=false)
{
switch ($gateway) {
case 'stripe':
return perch_shop_stripe_payment_form($opts, $return);
break;
case 'stripe_intents':
$opts = array_merge([
'template' => 'gateways/stripe_intents_payment_form.html'
], $opts);
return perch_shop_stripe_payment_form($opts, $return);
break;

case 'braintree':
return perch_shop_braintree_payment_form($opts, $return);
break;
}
}


function perch_shop_stripe_payment_form($opts=[], $return=false)
{
$default_opts = [
'template' => 'gateways/stripe_payment_form.html',
'skip-template' => false,
'cache' => true,
'cache-ttl' => 900,
];

$opts = PerchUtil::extend($default_opts, $opts);

if (isset($opts['template'])) {
$opts['template'] = 'shop/'.$opts['template'];
}

if ($opts['skip-template']==true) $return = true;

$API = new PerchAPI(1.0, 'perch_shop');
$Template = $API->get('Template', 'shop');
$Template->set($opts['template'], 'shop');

$Gateway = PerchShop_Gateways::get('stripe');
$config = PerchShop_Config::get('gateways', 'stripe');
$key = $Gateway->get_public_api_key($config);

$ShopRuntime = PerchShop_Runtime::fetch();

$html = $Template->render([
'amount' => floatval($ShopRuntime->get_cart_val('grand_total', [], []))*100,
'amount_formatted' => $ShopRuntime->get_cart_val('grand_total_formatted', [], []),
'currency' => $ShopRuntime->get_cart_val('currency_code', [], []),
'publishable_key' => $key,
'shop_name' => 'Shop',
]);
$r = $Template->apply_runtime_post_processing($html);

if ($return) return $r;
echo $r;
}

function perch_shop_braintree_payment_form($opts=[], $return=false)
{
$default_opts = [
'template' => 'gateways/braintree_payment_form.html',
'skip-template' => false,
'cache' => true,
'cache-ttl' => 900,
];

$opts = PerchUtil::extend($default_opts, $opts);

if (isset($opts['template'])) {
$opts['template'] = 'shop/'.$opts['template'];
}

if ($opts['skip-template']==true) $return = true;

$API = new PerchAPI(1.0, 'perch_shop');
$Template = $API->get('Template', 'shop');
$Template->set($opts['template'], 'shop');

$Gateway = PerchShop_Gateways::get('braintree');
$config = PerchShop_Config::get('gateways', 'braintree');
$key = $Gateway->get_public_api_key($config);

$ShopRuntime = PerchShop_Runtime::fetch();

$html = $Template->render([
'amount' => number_format(floatval($ShopRuntime->get_cart_val('grand_total', [], [])),2, '.', ''),
'amount_formatted' => $ShopRuntime->get_cart_val('grand_total_formatted', [], []),
'currency' => $ShopRuntime->get_cart_val('currency_code', [], []),
'client_token' => $key,
]);
$r = $Template->apply_runtime_post_processing($html);

if ($return) return $r;
echo $r;
}

0 comments on commit 76b3476

Please sign in to comment.