Changeset View
Changeset View
Standalone View
Standalone View
src/applications/phortune/provider/PhortuneStripePaymentProvider.php
| Show First 20 Lines • Show All 227 Lines • ▼ Show 20 Lines | /* -( Adding Payment Methods )--------------------------------------------- */ | ||||
| * @phutil-external-symbol class Stripe_Customer | * @phutil-external-symbol class Stripe_Customer | ||||
| */ | */ | ||||
| public function createPaymentMethodFromRequest( | public function createPaymentMethodFromRequest( | ||||
| AphrontRequest $request, | AphrontRequest $request, | ||||
| PhortunePaymentMethod $method, | PhortunePaymentMethod $method, | ||||
| array $token) { | array $token) { | ||||
| $this->loadStripeAPILibraries(); | $this->loadStripeAPILibraries(); | ||||
| $errors = array(); | |||||
| $secret_key = $this->getSecretKey(); | $secret_key = $this->getSecretKey(); | ||||
| $stripe_token = $token['stripeCardToken']; | $stripe_token = $token['stripeCardToken']; | ||||
| // First, make sure the token is valid. | // First, make sure the token is valid. | ||||
| $info = id(new Stripe_Token())->retrieve($stripe_token, $secret_key); | $info = id(new Stripe_Token())->retrieve($stripe_token, $secret_key); | ||||
| $account_phid = $method->getAccountPHID(); | $account_phid = $method->getAccountPHID(); | ||||
| $author_phid = $method->getAuthorPHID(); | $author_phid = $method->getAuthorPHID(); | ||||
| $params = array( | $params = array( | ||||
| 'card' => $stripe_token, | 'card' => $stripe_token, | ||||
| 'description' => $account_phid.':'.$author_phid, | 'description' => $account_phid.':'.$author_phid, | ||||
| ); | ); | ||||
| // Then, we need to create a Customer in order to be able to charge | // 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; | // the card more than once. We create one Customer for each card; | ||||
| // they do not map to PhortuneAccounts because we allow an account to | // they do not map to PhortuneAccounts because we allow an account to | ||||
| // have more than one active card. | // have more than one active card. | ||||
| try { | |||||
| $customer = Stripe_Customer::create($params, $secret_key); | $customer = Stripe_Customer::create($params, $secret_key); | ||||
| } catch (Stripe_CardError $ex) { | |||||
| $display_exception = $this->newDisplayExceptionFromCardError($ex); | |||||
| if ($display_exception) { | |||||
| throw $display_exception; | |||||
| } | |||||
| throw $ex; | |||||
| } | |||||
| $card = $info->card; | $card = $info->card; | ||||
| $method | $method | ||||
| ->setBrand($card->brand) | ->setBrand($card->brand) | ||||
| ->setLastFourDigits($card->last4) | ->setLastFourDigits($card->last4) | ||||
| ->setExpires($card->exp_year, $card->exp_month) | ->setExpires($card->exp_year, $card->exp_month) | ||||
| ->setMetadata( | ->setMetadata( | ||||
| array( | array( | ||||
| 'type' => 'stripe.customer', | 'type' => 'stripe.customer', | ||||
| 'stripe.customerID' => $customer->id, | 'stripe.customerID' => $customer->id, | ||||
| 'stripe.cardToken' => $stripe_token, | 'stripe.cardToken' => $stripe_token, | ||||
| )); | )); | ||||
| return $errors; | |||||
| } | } | ||||
| public function renderCreatePaymentMethodForm( | public function renderCreatePaymentMethodForm( | ||||
| AphrontRequest $request, | AphrontRequest $request, | ||||
| array $errors) { | array $errors) { | ||||
| $src = 'https://js.stripe.com/v2/'; | $src = 'https://js.stripe.com/v2/'; | ||||
| ▲ Show 20 Lines • Show All 98 Lines • ▼ Show 20 Lines | public function getCreatePaymentMethodErrorMessage($error_code) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| private function loadStripeAPILibraries() { | private function loadStripeAPILibraries() { | ||||
| $root = dirname(phutil_get_library_root('phabricator')); | $root = dirname(phutil_get_library_root('phabricator')); | ||||
| require_once $root.'/externals/stripe-php/lib/Stripe.php'; | require_once $root.'/externals/stripe-php/lib/Stripe.php'; | ||||
| } | } | ||||
| private function newDisplayExceptionFromCardError(Stripe_CardError $ex) { | |||||
| $body = $ex->getJSONBody(); | |||||
| if (!$body) { | |||||
| return null; | |||||
| } | |||||
| $map = idx($body, 'error'); | |||||
| if (!$map) { | |||||
| return null; | |||||
| } | |||||
| $view = array(); | |||||
| $message = idx($map, 'message'); | |||||
| $view[] = id(new PHUIInfoView()) | |||||
| ->setErrors(array($message)); | |||||
| $view[] = phutil_tag( | |||||
| 'div', | |||||
| array( | |||||
| 'class' => 'mlt mlb', | |||||
| ), | |||||
| pht('Additional details about this error:')); | |||||
| $rows = array(); | |||||
| $rows[] = array( | |||||
| pht('Error Code'), | |||||
| idx($map, 'code'), | |||||
| ); | |||||
| $rows[] = array( | |||||
| pht('Error Type'), | |||||
| idx($map, 'type'), | |||||
| ); | |||||
| $param = idx($map, 'param'); | |||||
| if (strlen($param)) { | |||||
| $rows[] = array( | |||||
| pht('Error Param'), | |||||
| $param, | |||||
| ); | |||||
| } | |||||
| $decline_code = idx($map, 'decline_code'); | |||||
| if (strlen($decline_code)) { | |||||
| $rows[] = array( | |||||
| pht('Decline Code'), | |||||
| $decline_code, | |||||
| ); | |||||
| } | |||||
| $doc_url = idx($map, 'doc_url'); | |||||
| if ($doc_url) { | |||||
| $rows[] = array( | |||||
| pht('Learn More'), | |||||
| phutil_tag( | |||||
| 'a', | |||||
| array( | |||||
| 'href' => $doc_url, | |||||
| 'target' => '_blank', | |||||
| ), | |||||
| $doc_url), | |||||
| ); | |||||
| } | |||||
| $view[] = id(new AphrontTableView($rows)) | |||||
| ->setColumnClasses( | |||||
| array( | |||||
| 'header', | |||||
| 'wide', | |||||
| )); | |||||
| return id(new PhortuneDisplayException(get_class($ex))) | |||||
| ->setView($view); | |||||
| } | |||||
| } | } | ||||