Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Moved card prompts to settings
  • Loading branch information
chadsmith committed Jun 26, 2011
1 parent 6e2aefd commit fae4381
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 23 deletions.
27 changes: 15 additions & 12 deletions applets/payment/twiml.php
Expand Up @@ -14,13 +14,13 @@
'card' => array()
);

$api_key = PluginData::get('api_key');
$settings = PluginData::get('settings');
$amount = AppletInstance::getValue('amount');
$description = AppletInstance::getValue('description');
$digits = isset($_REQUEST['Digits']) ? $_REQUEST['Digits'] : false;
$finishOnKey = '#';
$timeout = 15;
$method = 'POST';

$card_errors = array(
'invalid_number' => STATE_GATHER_CARD,
'incorrect_number' => STATE_GATHER_CARD,
Expand All @@ -31,6 +31,9 @@
'incorrect_cvc' => STATE_GATHER_CVC
);

if(is_object($settings))
$settings = get_object_vars($settings);

if(isset($_COOKIE[PAYMENT_COOKIE])) {
$state = json_decode(str_replace(', $Version=0', '', $_COOKIE[PAYMENT_COOKIE]), true);
if(is_object($state))
Expand All @@ -49,7 +52,7 @@
break;
case STATE_GATHER_YEAR:
$state['card']['exp_year'] = $digits;
$state[PAYMENT_ACTION] = STATE_GATHER_CVC;
$state[PAYMENT_ACTION] = $settings['require_cvc'] ? STATE_GATHER_CVC : STATE_SEND_PAYMENT;
break;
case STATE_GATHER_CVC:
$state['card']['cvc'] = $digits;
Expand All @@ -60,24 +63,24 @@

switch($state[PAYMENT_ACTION]) {
case STATE_GATHER_CARD:
$gather = $response->addGather(compact('finishOnKey', 'timeout', 'method'));
$gather->addSay("Please enter your credit card number followed by the pound sign.");
$gather = $response->addGather(compact('finishOnKey', 'timeout'));
$gather->addSay($settings['card_prompt']);
break;
case STATE_GATHER_MONTH:
$gather = $response->addGather(compact('finishOnKey', 'timeout', 'method'));
$gather->addSay("Please enter the month of the card's expiration date followed by the pound sign.");
$gather = $response->addGather(compact('finishOnKey', 'timeout'));
$gather->addSay($settings['month_prompt']);
break;
case STATE_GATHER_YEAR:
$gather = $response->addGather(compact('finishOnKey', 'timeout', 'method'));
$gather->addSay("Please enter the year of the expiration date followed by the pound sign.");
$gather = $response->addGather(compact('finishOnKey', 'timeout'));
$gather->addSay($settings['year_prompt']);
break;
case STATE_GATHER_CVC:
$gather = $response->addGather(compact('finishOnKey', 'timeout', 'method'));
$gather->addSay("Please enter the card's security code followed by the pound sign.");
$gather = $response->addGather(compact('finishOnKey', 'timeout'));
$gather->addSay($settings['cvc_prompt']);
break;
case STATE_SEND_PAYMENT:
require_once(dirname(dirname(dirname(__FILE__))) . '/stripe-php/lib/Stripe.php');
Stripe::setApiKey($api_key);
Stripe::setApiKey($settings['api_key']);
try {
$charge = Stripe_Charge::create(array(
'card' => $state['card'],
Expand Down
14 changes: 8 additions & 6 deletions applets/payment/ui.php
@@ -1,30 +1,32 @@
<?php
$api_key = PluginData::get('api_key');
$settings = PluginData::get('settings');
if(is_object($settings))
$settings = get_object_vars($settings);
?>
<div class="vbx-applet">
<?php if(!empty($api_key)): ?>
<?php if(!empty($settings) && !empty($settings['api_key'])): ?>
<div class="vbx-full-pane">
<h3>Amount</h3>
<p>A positive integer in cents representing much to charge the card.</p>
<fieldset class="vbx-input-container">
<input type="text" name="amount" class="medium" value="<?php echo AppletInstance::getValue('amount'); ?>" />
<input type="text" name="amount" class="medium" value="<?php echo AppletInstance::getValue('amount', 50); ?>" />
</fieldset>
<h3>Description</h3>
<fieldset class="vbx-input-container">
<input type="text" name="description" class="medium" value="<?php echo AppletInstance::getValue('description'); ?>" />
</fieldset>
</div>
<h2>Payment successful</h2>
<h2>After the payment</h2>
<div class="vbx-full-pane">
<?php echo AppletUI::DropZone('success'); ?>
</div><!-- .vbx-full-pane -->
<h2>Payment failed</h2>
<h2>If the payment fails</h2>
<div class="vbx-full-pane">
<?php echo AppletUI::DropZone('fail'); ?>
</div><!-- .vbx-full-pane -->
<?php else: ?>
<div class="vbx-full-pane">
<h3>Please set your Stripe.com API key first.</h3>
<h3>Please set your Stripe.com settings first.</h3>
</div>
<?php endif; ?>
</div><!-- .vbx-applet -->
7 changes: 7 additions & 0 deletions stripe.js
@@ -0,0 +1,7 @@
$(function() {
$('.vbx-stripe :checkbox').click(function() {
$('.vbx-stripe form p').eq(5).slideToggle();
});
if(!$('.vbx-stripe :checked').length)
$('.vbx-stripe form p').eq(5).hide();
});
55 changes: 50 additions & 5 deletions stripe.php
@@ -1,23 +1,68 @@
<?php
if(!empty($_POST['api_key']))
PluginData::set('api_key', $_POST['api_key']);
$api_key = PluginData::get('api_key');
if(count($_POST))
PluginData::set('settings', array(
'api_key' => $_POST['api_key'],
'card_prompt' => $_POST['card_prompt'],
'month_prompt' => $_POST['month_prompt'],
'year_prompt' => $_POST['year_prompt'],
'require_cvc' => isset($_POST['require_cvc']),
'cvc_prompt' => $_POST['cvc_prompt']
));
$settings = PluginData::get('settings', array(
'api_key' => null,
'card_prompt' => "Please enter your credit card number followed by the pound sign.",
'month_prompt' => "Please enter the month of the card's expiration date followed by the pound sign.",
'year_prompt' => "Please enter the year of the expiration date followed by the pound sign.",
'require_cvc' => true,
'cvc_prompt' => "Please enter the card's security code followed by the pound sign."
));
if(is_object($settings))
$settings = get_object_vars($settings);
OpenVBX::addJS('stripe.js');
?>
<style>
.vbx-stripe form {
padding: 20px 5%;
}
.vbx-stripe form p {
margin: 20px 0;
}
</style>
<div class="vbx-content-main">
<div class="vbx-content-menu vbx-content-menu-top">
<h2 class="vbx-content-heading">Stripe.com Settings</h2>
<h2 class="vbx-content-heading">Stripe Settings</h2>
</div>
<div class="vbx-table-section vbx-stripe">
<form method="post" action="">
<fieldset class="vbx-input-container">
<p>
<label class="field-label">API Key<br/>
<input type="password" name="api_key" class="medium" value="<?php echo htmlentities($api_key); ?>" />
<input type="password" name="api_key" class="medium" value="<?php echo htmlentities($settings['api_key']); ?>" />
</label>
</p>
<p>
<label class="field-label">Credit card prompt<br/>
<textarea rows="10" cols="100" name="card_prompt" class="medium"><?php echo htmlentities($settings['card_prompt']); ?></textarea>
</label>
</p>
<p>
<label class="field-label">Expiration month prompt<br/>
<textarea rows="10" cols="100" name="month_prompt" class="medium"><?php echo htmlentities($settings['month_prompt']); ?></textarea>
</label>
</p>
<p>
<label class="field-label">Expiration year prompt<br/>
<textarea rows="10" cols="100" name="year_prompt" class="medium"><?php echo htmlentities($settings['year_prompt']); ?></textarea>
</label>
</p>
<p>
<label class="field-label">
<input type="checkbox" name="require_cvc" <?php echo $settings['require_cvc'] ? ' checked="checked"' : ''; ?> /> Require CVC
</label>
</p>
<p>
<label class="field-label">Card CVC prompt<br/>
<textarea rows="10" cols="100" name="cvc_prompt" class="medium"><?php echo htmlentities($settings['cvc_prompt']); ?></textarea>
</label>
</p>
<p><button type="submit" class="submit-button"><span>Save</span></button></p>
Expand Down

0 comments on commit fae4381

Please sign in to comment.