diff --git a/Controller/StripeController.php b/Controller/StripeController.php new file mode 100644 index 0000000..ca1560e --- /dev/null +++ b/Controller/StripeController.php @@ -0,0 +1,15 @@ +root('ns_stripe'); + + // Here you should define the parameters that are allowed to + // configure your bundle. See the documentation linked above for + // more information on that topic. + + return $treeBuilder; + } +} diff --git a/DependencyInjection/NSStripeExtension.php b/DependencyInjection/NSStripeExtension.php new file mode 100644 index 0000000..eaf00a5 --- /dev/null +++ b/DependencyInjection/NSStripeExtension.php @@ -0,0 +1,29 @@ +processConfiguration($configuration, $configs); + + $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('services.yml'); + } +} diff --git a/Entity/CreditCard.php b/Entity/CreditCard.php new file mode 100644 index 0000000..209eabb --- /dev/null +++ b/Entity/CreditCard.php @@ -0,0 +1,141 @@ +name; + } + + /** + * + * @return string + */ + public function getNumber() + { + return $this->number; + } + + /** + * + * @return integer + */ + public function getExpiryYear() + { + return $this->expiryYear; + } + + /** + * + * @return integer + */ + public function getExpiryMonth() + { + return $this->expiryMonth; + } + + /** + * + * @return integer + */ + public function getCvv() + { + return $this->cvv; + } + + /** + * + * @param string $name + * @return CreditCard + */ + public function setName($name) + { + $this->name = $name; + return $this; + } + + /** + * + * @param string $number + * @return CreditCard + */ + public function setNumber($number) + { + $this->number = $number; + return $this; + } + + /** + * + * @param integer $expiryYear + * @return CreditCard + */ + public function setExpiryYear($expiryYear) + { + $this->expiryYear = $expiryYear; + return $this; + } + + /** + * + * @param integer $expiryMonth + * @return CreditCard + */ + public function setExpiryMonth($expiryMonth) + { + $this->expiryMonth = $expiryMonth; + return $this; + } + + /** + * + * @param integer $cvv + * @return CreditCard + */ + public function setCvv($cvv) + { + $this->cvv = $cvv; + return $this; + } +} \ No newline at end of file diff --git a/Entity/StripeResponse.php b/Entity/StripeResponse.php new file mode 100644 index 0000000..12380be --- /dev/null +++ b/Entity/StripeResponse.php @@ -0,0 +1,167 @@ +token; + } + + /** + * + * @return string + */ + public function getLast4() + { + return $this->last4; + } + + /** + * + * @return string + */ + public function getBrand() + { + return $this->brand; + } + + /** + * + * @return integer + */ + public function getExpMonth() + { + return $this->exp_month; + } + + /** + * + * @return integer + */ + public function getExpYear() + { + return $this->exp_year; + } + + /** + * + * @return string + */ + public function getClientIp() + { + return $this->client_ip; + } + + /** + * + * @param string $token + */ + public function setToken($token) + { + $this->token = $token; + } + + /** + * + * @param string $last4 + */ + public function setLast4($last4) + { + $this->last4 = $last4; + } + + /** + * + * @param string $brand + */ + public function setBrand($brand) + { + $this->brand = $brand; + } + + /** + * + * @param integer $exp_month + */ + public function setExpMonth($exp_month) + { + $this->exp_month = $exp_month; + } + + /** + * + * @param integer $exp_year + */ + public function setExpYear($exp_year) + { + $this->exp_year = $exp_year; + } + + /** + * + * @param string $client_ip + */ + public function setClientIp($client_ip) + { + $this->client_ip = $client_ip; + } + + public function fromResponse($response) + { + $this->token = $response->id; + $this->last4 = $response->card->last4; + $this->brand = $response->card->brand; + $this->exp_month = $response->card->exp_month; + $this->exp_year = $response->card->exp_year; + $this->client_ip = $response->client_ip; + } +} \ No newline at end of file diff --git a/Form/CreditCardType.php b/Form/CreditCardType.php new file mode 100644 index 0000000..e396838 --- /dev/null +++ b/Form/CreditCardType.php @@ -0,0 +1,56 @@ + '01', '02', '03', '04', '05', '06', '07', '08', '09', + '10', '11', '12'); + + $builder + ->add('name', null, array('label'=>'Cardholder Name', 'attr'=>array('data-stripe'=>'name'))) + ->add('number', null, array('label'=>'Card Number', 'attr'=>array('data-stripe'=>'number'))) + ->add('expiryYear', 'choice', array('label'=>'Year', 'choices' => $years, 'attr'=>array('data-stripe'=>'exp-year'))) + ->add('expiryMonth', 'choice', array('label'=>'Month', 'choices' => $choices, 'attr'=>array('data-stripe'=>'exp-month'))) + ->add('cvv', null, array('label'=>'CVV Code', 'attr'=>array('data-stripe'=>'cvv'))) + ; + } + + /** + * {@inheritdoc} + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + $resolver->setDefaults(array('data_class' => 'NS\StripBundle\Entity\CreditCard')); + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return 'CreditCardType'; + } +} \ No newline at end of file diff --git a/NSStripeBundle.php b/NSStripeBundle.php new file mode 100644 index 0000000..13a99dc --- /dev/null +++ b/NSStripeBundle.php @@ -0,0 +1,9 @@ +').val(response.id)); + $form.append($('').val(JSON.stringify(response))); + success = true; + break; + case 400: + alert('Some required payment information was missing. Please double check that you have your correct credit card number, CVC, and expiry date.'); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + break; + case 401: + alert('No valid API key provided.'); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + break; + case 402: + alert('Card declined. Please double check your card number, CVC and expiry date.'); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + break; + case 404: + alert('Payment processor not found.'); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + break; + case 500: + case 502: + case 503: + case 504: + alert('Payment processor server error. Please try again in a few minutes.'); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + break; + default: + alert('Unkown error. Please try again in a few minutes.'); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + break; + } + + if(success) + { + allowSubmit = true; + $form.submit(); + } + }); + + return false; + }); +}); \ No newline at end of file diff --git a/Service/StripeProcessor.php b/Service/StripeProcessor.php new file mode 100644 index 0000000..49999e0 --- /dev/null +++ b/Service/StripeProcessor.php @@ -0,0 +1,73 @@ +private_key = $private_key; + $this->public_key = $public_key; + + $this->init(); + } + + public function init() + { + Stripe::setApiKey($this->private_key); + } + + public function getPublicKey() + { + return $this->public_key; + } + + public function charge($amount, $token = false, $currency = "cad", $description = "", $receipt_email = null, $otherOptions = array()) + { + if(!$token) + { + throw new CardError('No transaction token provided'); + } + + $amount = $this->formatAmount($amount); + + try + { + $charge = $this->_charge($amount, $token, $currency, $description, $receipt_email, $otherOptions); + } + catch (CardError $ex) + { + throw $ex; + } + + return $charge; + } + + private function _charge($amount, $token, $currency, $description, $receipt_email, $otherOptions = array()) + { + return Charge::create(array_merge($otherOptions, array( + 'amount' => $amount, + 'source' => $token, + 'currency' => $currency, + 'description' => $description, + 'receipt_email' => $receipt_email + ))); + } + + public function formatAmount($amount) + { + if(is_float($amount)) + { + return $amount * 100; + } + + return $amount; + } +} \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..6e58705 --- /dev/null +++ b/composer.json @@ -0,0 +1,23 @@ +{ + "name": "ns/stripe-bundle", + "description": "Simple symfony integration of Stripe PHP library", + "keywords": ["template"], + "homepage": "https://github.com/NobletSolutions/StripeBundle", + "authors": [ + { + "name": "Mark Woynarski", + "email": "mark.woynarski@nobletdesign.com" + }, + { + "name": "Nathanael David Noblet", + "email": "nathanael@noblet.ca" + } + ], + "require": { + "stripe/stripe-php": "^3.15" + }, + "autoload": { + "psr-4": { "NS\\StripeBundle\\": "" } + } +} +