Projects
Infrastructure
phabricator
0007-Unbundle-stripe.patch
Log In
Username
Password
Overview
Repositories
Revisions
Requests
Users
Attributes
Meta
File 0007-Unbundle-stripe.patch of Package phabricator
From 41bdd5296f05bc4f5bc770875ce62bf643a26674 Mon Sep 17 00:00:00 2001 From: "Jeroen van Meeuwen (Kolab Systems)" <vanmeeuwen@kolabsys.com> Date: Wed, 10 Aug 2016 12:12:21 +0200 Subject: [PATCH 07/15] Unbundle stripe --- resources/celerity/map.php | 2 - src/__phutil_library_map__.php | 2 - .../provider/PhortuneStripePaymentProvider.php | 380 --------------------- src/view/phui/PHUIIconView.php | 1 - webroot/rsrc/css/font/font-awesome.css | 3 - .../phortune/behavior-stripe-payment-form.js | 56 --- 6 files changed, 444 deletions(-) delete mode 100644 src/applications/phortune/provider/PhortuneStripePaymentProvider.php delete mode 100644 webroot/rsrc/js/application/phortune/behavior-stripe-payment-form.js diff --git a/resources/celerity/map.php b/resources/celerity/map.php index 9c8fa7b..02bc1ea 100644 --- a/resources/celerity/map.php +++ b/resources/celerity/map.php @@ -430,7 +430,6 @@ return array( 'rsrc/js/application/passphrase/passphrase-credential-control.js' => '3cb0b2fc', 'rsrc/js/application/pholio/behavior-pholio-mock-edit.js' => 'bee502c8', 'rsrc/js/application/pholio/behavior-pholio-mock-view.js' => 'fbe497e7', - 'rsrc/js/application/phortune/behavior-stripe-payment-form.js' => '3f5d6dbf', 'rsrc/js/application/phortune/behavior-test-payment-form.js' => 'fc91ab6c', 'rsrc/js/application/phortune/phortune-credit-card-form.js' => '2290aeef', 'rsrc/js/application/policy/behavior-policy-control.js' => 'd0c516d5', @@ -711,7 +710,6 @@ return array( 'javelin-behavior-select-on-click' => '4e3e79a6', 'javelin-behavior-setup-check-https' => '491416b3', 'javelin-behavior-slowvote-embed' => '887ad43f', - 'javelin-behavior-stripe-payment-form' => '3f5d6dbf', 'javelin-behavior-test-payment-form' => 'fc91ab6c', 'javelin-behavior-time-typeahead' => '522431f7', 'javelin-behavior-toggle-class' => '92b9ec77', diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index d9b6844..e286f70 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -4253,7 +4253,6 @@ phutil_register_library_map(array( 'PhortunePurchasePHIDType' => 'applications/phortune/phid/PhortunePurchasePHIDType.php', 'PhortunePurchaseQuery' => 'applications/phortune/query/PhortunePurchaseQuery.php', 'PhortuneSchemaSpec' => 'applications/phortune/storage/PhortuneSchemaSpec.php', - 'PhortuneStripePaymentProvider' => 'applications/phortune/provider/PhortuneStripePaymentProvider.php', 'PhortuneSubscription' => 'applications/phortune/storage/PhortuneSubscription.php', 'PhortuneSubscriptionCart' => 'applications/phortune/cart/PhortuneSubscriptionCart.php', 'PhortuneSubscriptionEditController' => 'applications/phortune/controller/PhortuneSubscriptionEditController.php', @@ -9547,7 +9546,6 @@ phutil_register_library_map(array( 'PhortunePurchasePHIDType' => 'PhabricatorPHIDType', 'PhortunePurchaseQuery' => 'PhabricatorCursorPagedPolicyAwareQuery', 'PhortuneSchemaSpec' => 'PhabricatorConfigSchemaSpec', - 'PhortuneStripePaymentProvider' => 'PhortunePaymentProvider', 'PhortuneSubscription' => array( 'PhortuneDAO', 'PhabricatorPolicyInterface', diff --git a/src/applications/phortune/provider/PhortuneStripePaymentProvider.php b/src/applications/phortune/provider/PhortuneStripePaymentProvider.php deleted file mode 100644 index 1e7c348..0000000 --- a/src/applications/phortune/provider/PhortuneStripePaymentProvider.php +++ /dev/null @@ -1,380 +0,0 @@ -<?php - -final class PhortuneStripePaymentProvider extends PhortunePaymentProvider { - - const STRIPE_PUBLISHABLE_KEY = 'stripe.publishable-key'; - const STRIPE_SECRET_KEY = 'stripe.secret-key'; - - public function isAcceptingLivePayments() { - return preg_match('/_live_/', $this->getPublishableKey()); - } - - public function getName() { - return pht('Stripe'); - } - - public function getConfigureName() { - return pht('Add Stripe Payments Account'); - } - - public function getConfigureDescription() { - return pht( - 'Allows you to accept credit or debit card payments with a '. - 'stripe.com account.'); - } - - public function getConfigureProvidesDescription() { - return pht('This merchant accepts credit and debit cards via Stripe.'); - } - - public function getPaymentMethodDescription() { - return pht('Add Credit or Debit Card (US and Canada)'); - } - - public function getPaymentMethodIcon() { - return 'Stripe'; - } - - public function getPaymentMethodProviderDescription() { - return pht('Processed by Stripe'); - } - - public function getDefaultPaymentMethodDisplayName( - PhortunePaymentMethod $method) { - return pht('Credit/Debit Card'); - } - - public function getAllConfigurableProperties() { - return array( - self::STRIPE_PUBLISHABLE_KEY, - self::STRIPE_SECRET_KEY, - ); - } - - public function getAllConfigurableSecretProperties() { - return array( - self::STRIPE_SECRET_KEY, - ); - } - - public function processEditForm( - AphrontRequest $request, - array $values) { - - $errors = array(); - $issues = array(); - - if (!strlen($values[self::STRIPE_SECRET_KEY])) { - $errors[] = pht('Stripe Secret Key is required.'); - $issues[self::STRIPE_SECRET_KEY] = pht('Required'); - } - - if (!strlen($values[self::STRIPE_PUBLISHABLE_KEY])) { - $errors[] = pht('Stripe Publishable Key is required.'); - $issues[self::STRIPE_PUBLISHABLE_KEY] = pht('Required'); - } - - return array($errors, $issues, $values); - } - - public function extendEditForm( - AphrontRequest $request, - AphrontFormView $form, - array $values, - array $issues) { - - $form - ->appendChild( - id(new AphrontFormTextControl()) - ->setName(self::STRIPE_SECRET_KEY) - ->setValue($values[self::STRIPE_SECRET_KEY]) - ->setError(idx($issues, self::STRIPE_SECRET_KEY, true)) - ->setLabel(pht('Stripe Secret Key'))) - ->appendChild( - id(new AphrontFormTextControl()) - ->setName(self::STRIPE_PUBLISHABLE_KEY) - ->setValue($values[self::STRIPE_PUBLISHABLE_KEY]) - ->setError(idx($issues, self::STRIPE_PUBLISHABLE_KEY, true)) - ->setLabel(pht('Stripe Publishable Key'))); - } - - public function getConfigureInstructions() { - return pht( - "To configure Stripe, register or log in to an existing account on ". - "[[https://stripe.com | stripe.com]]. Once logged in:\n\n". - " - Go to {nav icon=user, name=Your Account > Account Settings ". - "> API Keys}\n". - " - Copy the **Secret Key** and **Publishable Key** into the fields ". - "above.\n\n". - "You can either use the test keys to add this provider in test mode, ". - "or the live keys to accept live payments."); - } - - public function canRunConfigurationTest() { - return true; - } - - public function runConfigurationTest() { - $this->loadStripeAPILibraries(); - - $secret_key = $this->getSecretKey(); - $account = Stripe_Account::retrieve($secret_key); - } - - /** - * @phutil-external-symbol class Stripe_Charge - * @phutil-external-symbol class Stripe_CardError - * @phutil-external-symbol class Stripe_Account - */ - protected function executeCharge( - PhortunePaymentMethod $method, - PhortuneCharge $charge) { - $this->loadStripeAPILibraries(); - - $price = $charge->getAmountAsCurrency(); - - $secret_key = $this->getSecretKey(); - $params = array( - 'amount' => $price->getValueInUSDCents(), - 'currency' => $price->getCurrency(), - 'customer' => $method->getMetadataValue('stripe.customerID'), - 'description' => $charge->getPHID(), - 'capture' => true, - ); - - $stripe_charge = Stripe_Charge::create($params, $secret_key); - - $id = $stripe_charge->id; - if (!$id) { - throw new Exception(pht('Stripe charge call did not return an ID!')); - } - - $charge->setMetadataValue('stripe.chargeID', $id); - $charge->save(); - } - - protected function executeRefund( - PhortuneCharge $charge, - PhortuneCharge $refund) { - $this->loadStripeAPILibraries(); - - $charge_id = $charge->getMetadataValue('stripe.chargeID'); - if (!$charge_id) { - throw new Exception( - pht('Unable to refund charge; no Stripe chargeID!')); - } - - $refund_cents = $refund - ->getAmountAsCurrency() - ->negate() - ->getValueInUSDCents(); - - $secret_key = $this->getSecretKey(); - $params = array( - 'amount' => $refund_cents, - ); - - $stripe_charge = Stripe_Charge::retrieve($charge_id, $secret_key); - $stripe_refund = $stripe_charge->refunds->create($params); - - $id = $stripe_refund->id; - if (!$id) { - throw new Exception(pht('Stripe refund call did not return an ID!')); - } - - $charge->setMetadataValue('stripe.refundID', $id); - $charge->save(); - } - - public function updateCharge(PhortuneCharge $charge) { - $this->loadStripeAPILibraries(); - - $charge_id = $charge->getMetadataValue('stripe.chargeID'); - if (!$charge_id) { - throw new Exception( - pht('Unable to update charge; no Stripe chargeID!')); - } - - $secret_key = $this->getSecretKey(); - $stripe_charge = Stripe_Charge::retrieve($charge_id, $secret_key); - - // TODO: Deal with disputes / chargebacks / surprising refunds. - - } - - private function getPublishableKey() { - return $this - ->getProviderConfig() - ->getMetadataValue(self::STRIPE_PUBLISHABLE_KEY); - } - - private function getSecretKey() { - return $this - ->getProviderConfig() - ->getMetadataValue(self::STRIPE_SECRET_KEY); - } - - -/* -( Adding Payment Methods )--------------------------------------------- */ - - - public function canCreatePaymentMethods() { - return true; - } - - - /** - * @phutil-external-symbol class Stripe_Token - * @phutil-external-symbol class Stripe_Customer - */ - public function createPaymentMethodFromRequest( - AphrontRequest $request, - PhortunePaymentMethod $method, - array $token) { - $this->loadStripeAPILibraries(); - - $errors = array(); - - $secret_key = $this->getSecretKey(); - $stripe_token = $token['stripeCardToken']; - - // First, make sure the token is valid. - $info = id(new Stripe_Token())->retrieve($stripe_token, $secret_key); - - $account_phid = $method->getAccountPHID(); - $author_phid = $method->getAuthorPHID(); - - $params = array( - 'card' => $stripe_token, - 'description' => $account_phid.':'.$author_phid, - ); - - // Then, we need to create a Customer in order to be able to charge - // the card more than once. We create one Customer for each card; - // they do not map to PhortuneAccounts because we allow an account to - // have more than one active card. - $customer = Stripe_Customer::create($params, $secret_key); - - $card = $info->card; - - $method - ->setBrand($card->brand) - ->setLastFourDigits($card->last4) - ->setExpires($card->exp_year, $card->exp_month) - ->setMetadata( - array( - 'type' => 'stripe.customer', - 'stripe.customerID' => $customer->id, - 'stripe.cardToken' => $stripe_token, - )); - - return $errors; - } - - public function renderCreatePaymentMethodForm( - AphrontRequest $request, - array $errors) { - - $ccform = id(new PhortuneCreditCardForm()) - ->setSecurityAssurance( - pht('Payments are processed securely by Stripe.')) - ->setUser($request->getUser()) - ->setErrors($errors) - ->addScript('https://js.stripe.com/v2/'); - - Javelin::initBehavior( - 'stripe-payment-form', - array( - 'stripePublishableKey' => $this->getPublishableKey(), - 'formID' => $ccform->getFormID(), - )); - - return $ccform->buildForm(); - } - - private function getStripeShortErrorCode($error_code) { - $prefix = 'cc:stripe:'; - if (strncmp($error_code, $prefix, strlen($prefix))) { - return null; - } - return substr($error_code, strlen($prefix)); - } - - public function validateCreatePaymentMethodToken(array $token) { - return isset($token['stripeCardToken']); - } - - public function translateCreatePaymentMethodErrorCode($error_code) { - $short_code = $this->getStripeShortErrorCode($error_code); - - if ($short_code) { - static $map = array( - 'error:invalid_number' => PhortuneErrCode::ERR_CC_INVALID_NUMBER, - 'error:invalid_cvc' => PhortuneErrCode::ERR_CC_INVALID_CVC, - 'error:invalid_expiry_month' => PhortuneErrCode::ERR_CC_INVALID_EXPIRY, - 'error:invalid_expiry_year' => PhortuneErrCode::ERR_CC_INVALID_EXPIRY, - ); - - if (isset($map[$short_code])) { - return $map[$short_code]; - } - } - - return $error_code; - } - - /** - * See https://stripe.com/docs/api#errors for more information on possible - * errors. - */ - public function getCreatePaymentMethodErrorMessage($error_code) { - $short_code = $this->getStripeShortErrorCode($error_code); - if (!$short_code) { - return null; - } - - switch ($short_code) { - case 'error:incorrect_number': - $error_key = 'number'; - $message = pht('Invalid or incorrect credit card number.'); - break; - case 'error:incorrect_cvc': - $error_key = 'cvc'; - $message = pht('Card CVC is invalid or incorrect.'); - break; - $error_key = 'exp'; - $message = pht('Card expiration date is invalid or incorrect.'); - break; - case 'error:invalid_expiry_month': - case 'error:invalid_expiry_year': - case 'error:invalid_cvc': - case 'error:invalid_number': - // NOTE: These should be translated into Phortune error codes earlier, - // so we don't expect to receive them here. They are listed for clarity - // and completeness. If we encounter one, we treat it as an unknown - // error. - break; - case 'error:invalid_amount': - case 'error:missing': - case 'error:card_declined': - case 'error:expired_card': - case 'error:duplicate_transaction': - case 'error:processing_error': - default: - // NOTE: These errors currently don't recevive a detailed message. - // NOTE: We can also end up here with "http:nnn" messages. - - // TODO: At least some of these should have a better message, or be - // translated into common errors above. - break; - } - - return null; - } - - private function loadStripeAPILibraries() { - $root = dirname(phutil_get_library_root('phabricator')); - require_once $root.'/externals/stripe-php/lib/Stripe.php'; - } - -} diff --git a/src/view/phui/PHUIIconView.php b/src/view/phui/PHUIIconView.php index cdc3825..b9c2cb3 100644 --- a/src/view/phui/PHUIIconView.php +++ b/src/view/phui/PHUIIconView.php @@ -587,7 +587,6 @@ final class PHUIIconView extends AphrontTagView { 'fa-cc-discover', 'fa-cc-amex', 'fa-cc-paypal', - 'fa-cc-stripe', 'fa-bell-slash', 'fa-bell-slash-o', 'fa-trash', diff --git a/webroot/rsrc/css/font/font-awesome.css b/webroot/rsrc/css/font/font-awesome.css index 4157229..ae7bd7e 100644 --- a/webroot/rsrc/css/font/font-awesome.css +++ b/webroot/rsrc/css/font/font-awesome.css @@ -1484,9 +1484,6 @@ .fa-cc-paypal:before { content: "\f1f4"; } -.fa-cc-stripe:before { - content: "\f1f5"; -} .fa-bell-slash:before { content: "\f1f6"; } diff --git a/webroot/rsrc/js/application/phortune/behavior-stripe-payment-form.js b/webroot/rsrc/js/application/phortune/behavior-stripe-payment-form.js deleted file mode 100644 index 7921a52..0000000 --- a/webroot/rsrc/js/application/phortune/behavior-stripe-payment-form.js +++ /dev/null @@ -1,56 +0,0 @@ -/** - * @provides javelin-behavior-stripe-payment-form - * @requires javelin-behavior - * javelin-dom - * phortune-credit-card-form - */ - -JX.behavior('stripe-payment-form', function(config) { - Stripe.setPublishableKey(config.stripePublishableKey); - - function onsubmit(card_data) { - var errors = []; - - if (!Stripe.validateCardNumber(card_data.number)) { - errors.push('cc:invalid:number'); - } - - if (!Stripe.validateCVC(card_data.cvc)) { - errors.push('cc:invalid:cvc'); - } - - if (!Stripe.validateExpiry(card_data.month, card_data.year)) { - errors.push('cc:invalid:expiry'); - } - - if (errors.length) { - ccform.submitForm(errors); - return; - } - - var data = { - number: card_data.number, - cvc: card_data.cvc, - exp_month: card_data.month, - exp_year: card_data.year - }; - - Stripe.createToken(data, onresponse); - } - - function onresponse(status, response) { - var errors = []; - var token = null; - if (status != 200) { - errors.push('cc:stripe:http:' + status); - } else if (response.error) { - errors.push('cc:stripe:error:' + response.error.type); - } else { - token = response.id; - } - - ccform.submitForm(errors, {stripeCardToken: token}); - } - - var ccform = new JX.PhortuneCreditCardForm(JX.$(config.formID), onsubmit); -}); -- 2.9.3
Locations
Projects
Search
Status Monitor
Help
Open Build Service
OBS Manuals
API Documentation
OBS Portal
Reporting a Bug
Contact
Mailing List
Forums
Chat (IRC)
Twitter
Open Build Service (OBS)
is an
openSUSE project
.