-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#5: Added a Block plugin that will display he Stripe checkout form(bu…
…tton) with configuration. Also began the charge servie build out for #3.
- Loading branch information
Showing
6 changed files
with
354 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |