Skip to content

Commit

Permalink
#5: Added a Block plugin that will display he Stripe checkout form(bu…
Browse files Browse the repository at this point in the history
…tton) with configuration. Also began the charge servie build out for #3.
  • Loading branch information
cfblack committed Apr 6, 2017
1 parent 9a45e36 commit 59ebb56
Show file tree
Hide file tree
Showing 6 changed files with 354 additions and 1 deletion.
13 changes: 12 additions & 1 deletion badcamp_stripe_payment.module
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ function badcamp_stripe_payment_help($route_name, RouteMatchInterface $route_mat
/**
* Implements hook_theme().
*/
function badcamp_stripe_payment_theme() {
function badcamp_stripe_payment_theme($existing, $type, $theme, $path) {
$theme = [];

$theme['stripe_payment'] = array(
'render element' => 'elements',
'file' => 'stripe_payment.page.inc',
Expand All @@ -38,6 +39,16 @@ function badcamp_stripe_payment_theme() {
'variables' => ['content' => NULL],
'file' => 'stripe_payment.page.inc',
];
$theme['stripe_checkout'] = [
'variables' => [
'data_key' => NULL,
'amount' => NULL,
'org_name' => NULL,
'data_zip_code' => NULL
],
'template' => 'stripe_checkout'
];

return $theme;
}

Expand Down
5 changes: 5 additions & 0 deletions badcamp_stripe_payment.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
services:
badcamp_stripe_payment.charge:
class: Drupal\badcamp_stripe_payment\BadCampStripePaymentCharge
arguments: ['@stripe_api.stripe_api']

175 changes: 175 additions & 0 deletions src/BadCampStripePaymentCharge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
<?php

namespace Drupal\badcamp_stripe_payment;

use Drupal\stripe_api\StripeApiService;
use Stripe\Charge;
use Stripe\Stripe;

/**
* Class BadCampStripePaymentCharge.
*
* @package Drupal\badcamp_stripe_payment
*/
class BadCampStripePaymentCharge implements BadCampStripePaymentChargeInterface {

/**
* Drupal\stripe_api\StripeApiService definition.
*
* @var \Drupal\stripe_api\StripeApiService
*/
protected $stripeApi;

/**
* @var \Stripe\Charge
*/
protected $id;

/**
* @var \Stripe\Charge
*/
protected $amount;

/**
* @var \Stripe\Charge
*/
protected $currency;

/**
* @var \Stripe\Charge
*/
protected $description;

/**
* @var \Stripe\Charge
*/
protected $receipt_email;

/**
* @var \Stripe\Charge
*/
protected $source;

/**
* @var \Stripe\Charge
*/
protected $statement_descriptor;

/**
* Constructor.
*/
public function __construct(StripeApiService $stripe_api_stripe_api) {
$this->stripeApi = $stripe_api_stripe_api;
}

public function charge() {

$charge = Charge::create($params);
}

/**
* @return \Stripe\Charge
*/
public function getId() {
return $this->id;
}

/**
* @param \Stripe\Charge $id
*/
public function setId($id) {
$this->id = $id;
}

/**
* @return \Stripe\Charge
*/
public function getAmount() {
return $this->amount;
}

/**
* @param \Stripe\Charge $amount
*/
public function setAmount($amount) {
$this->amount = $amount;
}

/**
* @return \Stripe\Charge
*/
public function getCurrency() {
return $this->currency;
}

/**
* @param \Stripe\Charge $currency
*/
public function setCurrency($currency) {
$this->currency = $currency;
}

/**
* @return \Stripe\Charge
*/
public function getDescription() {
return $this->description;
}

/**
* @param \Stripe\Charge $description
*/
public function setDescription($description) {
$this->description = $description;
}

/**
* @return \Stripe\Charge
*/
public function getReceiptEmail() {
return $this->receipt_email;
}

/**
* @param \Stripe\Charge $receipt_email
*/
public function setReceiptEmail($receipt_email) {
$this->receipt_email = $receipt_email;
}

/**
* @return \Stripe\Charge
*/
public function getSource() {
return $this->source;
}

/**
* @param \Stripe\Charge $source
*/
public function setSource($exp_month, $exp_year, $cardholder_name, $number,
$object = 'card', $cvc, $address_line1, $address_line2, $address_city,
$address_state, $address_country, $address_zip, $statement_descriptor) {
$source = [
''
];
$this->source = $source;
}

/**
* @return \Stripe\Charge
*/
public function getStatementDescriptor() {
return $this->statement_descriptor;
}

/**
* @param \Stripe\Charge $statement_descriptor
*/
public function setStatementDescriptor($statement_descriptor) {
$this->statement_descriptor = $statement_descriptor;
}



}
13 changes: 13 additions & 0 deletions src/BadCampStripePaymentChargeInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Drupal\badcamp_stripe_payment;

/**
* Interface BadCampStripePaymentChargeInterface.
*
* @package Drupal\badcamp_stripe_payment
*/
interface BadCampStripePaymentChargeInterface {


}
135 changes: 135 additions & 0 deletions src/Plugin/Block/StripeCheckoutBlock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Drupal\badcamp_stripe_payment\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\stripe_api\StripeApiService;

/**
* Provides a 'StripeCheckoutBlock' block.
*
* @Block(
* id = "stripe_checkout_block",
* admin_label = @Translation("Stripe checkout block"),
* )
*/
class StripeCheckoutBlock extends BlockBase implements ContainerFactoryPluginInterface {

/**
* Drupal\stripe_api\StripeApiService definition.
*
* @var \Drupal\stripe_api\StripeApiService
*/
protected $stripeApi;

/**
* Construct.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param string $plugin_definition
* The plugin implementation definition.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
StripeApiService $stripe_api
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->stripeApi = $stripe_api;
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('stripe_api.stripe_api')
);
}

/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'turn_on_avs' => 1,
] + parent::defaultConfiguration();

}

/**
* {@inheritdoc}
*/
public function blockForm($form, FormStateInterface $form_state) {
$form['turn_on_avs'] = [
'#type' => 'checkbox',
'#title' => $this->t('Turn on street and zip/postal code checks
(address verification or AVS)?'),
'#description' => $this->t('Address verification checks (also known as
Address Verification System or AVS) provide additional levels of
confirmation that the person using the card is the legitimate owner of
the card, useful to help identify and avoid fraud.'),
'#default_value' => $this->configuration['turn_on_avs'],
'#weight' => '1',
];

$form['amount'] = [
'#type' => 'number',
'#required' => TRUE,
'#title' => $this->t('Amount'),
'#description' => $this->t('ONLY NUMBERS ARE ALLOWED! The dollar amount
that should be charged IN CENTS, e.g:($20 = 2000)'),
'#default_value' => $this->configuration['amount'],
'#weight' => '2',
];

$form['description'] = array(
'#type' => 'textfield',
'#required' => FALSE,
'#title' => $this->t('Description'),
'#description' => $this->t('The description of the item you are asking
the buyer to pay for.'),
'#default_value' => $this->configuration['description'],
'#weight' => '3',
);
return $form;
}

/**
* {@inheritdoc}
*/
public function blockSubmit($form, FormStateInterface $form_state) {
$this->configuration['turn_on_avs'] = $form_state->getValue('turn_on_avs');
$this->configuration['amount'] = $form_state->getValue('amount');
$this->configuration['description'] = $form_state->getValue('description');
}

/**
* {@inheritdoc}
*/
public function build() {
$build = [];

$build['stripe_checkout'] = [
'#theme' => 'stripe_checkout',
'#data_key' => $this->stripeApi->getPubKey(),
'#amount' => $this->configuration['amount'],
'#org_name' => $this->label(),
'#data_description' => $this->configuration['description'],
'#data_zip_code' => $this->configuration['turn_on_avs']
];

return $build;
}

}
14 changes: 14 additions & 0 deletions templates/stripe_checkout.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<form action="/checkout/charge" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{{ data_key }}"
data-amount="{{ amount }}"
data-name="{{ org_name }}"
data-description="{{ data_description }}"
data-image="https://s3.amazonaws.com/stripe-uploads/acct_16LjmVIcdSas0QJqmerchant-icon-1439944455254-Screen%20Shot%202015-08-18%20at%205.33.41%20PM.png"
data-locale="auto"
{% if data_zip_code %}
data-zip-code="true">
{% endif %}
</script>
</form>

0 comments on commit 59ebb56

Please sign in to comment.