Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add view helper for publishable key #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public function getConfig()

return [
'service_manager' => $configProvider->getDependencies(),
'view_helpers' => [
'factories' => [
'stripeKey' => \ZfrStripeModule\View\Helper\StripeKeyFactory::class,
],
],
];
}
}
5 changes: 4 additions & 1 deletion config/zfr_stripe.local.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ return array(
* Secret key
*/
// 'secret_key' => '',

/**
* Publishable key
*/
// 'publishable_key' => '',
/**
* Stripe SDK version to use
*/
Expand Down
24 changes: 24 additions & 0 deletions src/View/Helper/StripeKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace ZfrStripeModule\View\Helper;

use Zend\View\Helper\AbstractHelper;

class StripeKey extends AbstractHelper
{
private $publishableKey;

public function __construct($publishableKey)
{
if (strpos($publishableKey, 'sk') === 0) {
throw new \Exception('You appear to have set a secret key as your publishable key, please check config');
}

$this->publishableKey = $publishableKey;
}

public function __invoke()
{
return sprintf("Stripe.setPublishableKey('%s');", $this->publishableKey);
}
}
15 changes: 15 additions & 0 deletions src/View/Helper/StripeKeyFactory
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace ZfrStripeModule\View\Helper;

use Interop\Container\ContainerInterface;
use Zend\ServiceManager\Factory\FactoryInterface;

class StripeKeyFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('Config');
return new StripeKey($config['zfr_stripe']['publishable_key']);
}
}