From a81a2a92b7ff11835fd1dc87b5670f8980d9822e Mon Sep 17 00:00:00 2001 From: "Nathanael d. Noblet" Date: Tue, 27 Aug 2024 11:09:56 -0600 Subject: [PATCH] Restored current version --- Controller/StripeController.php | 15 ++ DependencyInjection/Configuration.php | 29 ++++ DependencyInjection/NSStripeExtension.php | 29 ++++ Entity/CreditCard.php | 141 ++++++++++++++++++ Entity/StripeResponse.php | 167 ++++++++++++++++++++++ Form/CreditCardType.php | 56 ++++++++ NSStripeBundle.php | 9 ++ README.md | 1 + Resources/config/services.yml | 4 + Resources/public/images/stripe.png | Bin 0 -> 1110 bytes Resources/public/js/payment.js | 66 +++++++++ Service/StripeProcessor.php | 73 ++++++++++ composer.json | 23 +++ 13 files changed, 613 insertions(+) create mode 100644 Controller/StripeController.php create mode 100644 DependencyInjection/Configuration.php create mode 100644 DependencyInjection/NSStripeExtension.php create mode 100644 Entity/CreditCard.php create mode 100644 Entity/StripeResponse.php create mode 100644 Form/CreditCardType.php create mode 100644 NSStripeBundle.php create mode 100644 README.md create mode 100644 Resources/config/services.yml create mode 100755 Resources/public/images/stripe.png create mode 100644 Resources/public/js/payment.js create mode 100644 Service/StripeProcessor.php create mode 100644 composer.json 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 @@ +=ZEy&?SvbD9jU^%UoKk0#nCDta+*x5xcq=>>`dT%}WX5}! z;HdL@@IcrX9<`FVCEjS8Vt`tG3bpo=do zT&7M_#9gKwN8B&U&%is)KtL@4i+fyr#H{NPfK$cgJTcd{-VBm;4(u_OC2XBut-K&q zg<)ZXV;f2Ym=_Ky7u1Ugcq$y>I~G#S6gSMt7O~$Twj*o_-NF?vXq-AZAxrrlK1;ZY z7{{y(x77s^ydW8ZDGxCP-EOS8j?$0*+y281FrIe-9jWV8)2;iIrh zogHC;@+^T5A+C-6oIVby6JXW&PC13B!;Im;L)>4&i131Oj|z9R;CC>Yp15STtu4I~ zBfE+&%d!In;?(6Ku4=c6UBq6)UdH~4{i)I7mKo?*0u(^JqN+#G%9Q7sjpJlOiHqDP zBQI&$p4N7fy@=~*yUzjab?WA@qx7Ar^PU@~eM$&2rVnI<5RUQR6ecMb zbRo#b1kAXJ%drw7c9GpFd}(|`Jf_+I5|{RUT80eZ>&Lc*36B>LL9Q!H{cnc2>9dMU z-@DjZ{8{W({A$&SHCJ39P`37P-mUqT)P_i9s`0FOLj?v_gqSHV3~8Goo4!McrTW+c zH*wc#r!pgH_;ZXkYPRJ(RkCJNo3wB7Z(*w}v+5C-e)lx{A^u5YcfKtg;Xk)GR8jTp zcSgDetB&W)sN3UIjqPLnn&Z!VY*Z{JUe%b0o%rvli3ibo#5E?yYh!%Rn2Zkm5=Q;k zkF_yMMjzJj<}J+Rin(QXt&AISNiwzkoR5dHG#8F2e3J?DRvez8EW cocU4uA6B^QCL@3fod5s;07*qoM6N<$f}(RF3;+NC literal 0 HcmV?d00001 diff --git a/Resources/public/js/payment.js b/Resources/public/js/payment.js new file mode 100644 index 0000000..99b12ed --- /dev/null +++ b/Resources/public/js/payment.js @@ -0,0 +1,66 @@ +$(document).ready(function() +{ + var allowSubmit = false; + + $form = $('form[data-nsStripePayment]'); + $form.prop('action', $form.data('action')); + $($form.data('button')).prop('disabled', false).html($form.data('defaulttext')); + + $form.submit(function(event) + { + if(allowSubmit) + return true; + + event.preventDefault(); + + $($form.data('button')).prop('disabled', true).html($form.data('loadingtext')); + var success = false; + + Stripe.card.createToken($form, function(status, response) + { + switch(status) + { + case 200: + $form.append($('').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\\": "" } + } +} +