2013-03-28 17:11:42 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortunePaymentMethodEditController
|
|
|
|
extends PhortuneController {
|
|
|
|
|
|
|
|
private $accountID;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->accountID = $data['accountID'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
|
|
|
|
|
|
|
$account = id(new PhortuneAccountQuery())
|
|
|
|
->setViewer($user)
|
|
|
|
->withIDs(array($this->accountID))
|
|
|
|
->executeOne();
|
|
|
|
if (!$account) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$cancel_uri = $this->getApplicationURI($account->getID().'/');
|
2013-03-28 17:11:42 +01:00
|
|
|
$account_uri = $this->getApplicationURI($account->getID().'/');
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$providers = PhortunePaymentProvider::getProvidersForAddPaymentMethod();
|
|
|
|
if (!$providers) {
|
|
|
|
throw new Exception(
|
2014-06-09 20:36:49 +02:00
|
|
|
'There are no payment providers enabled that can add payment '.
|
|
|
|
'methods.');
|
2013-04-25 18:46:32 +02:00
|
|
|
}
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$provider_key = $request->getStr('providerKey');
|
|
|
|
if (empty($providers[$provider_key])) {
|
|
|
|
$choices = array();
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
$choices[] = $this->renderSelectProvider($provider);
|
2013-03-28 17:11:42 +01:00
|
|
|
}
|
2013-04-25 18:46:32 +02:00
|
|
|
return $this->buildResponse($choices, $account_uri);
|
|
|
|
}
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$provider = $providers[$provider_key];
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$errors = array();
|
|
|
|
if ($request->isFormPost() && $request->getBool('isProviderForm')) {
|
|
|
|
$method = id(new PhortunePaymentMethod())
|
|
|
|
->setAccountPHID($account->getPHID())
|
|
|
|
->setAuthorPHID($user->getPHID())
|
|
|
|
->setStatus(PhortunePaymentMethod::STATUS_ACTIVE)
|
2013-04-25 18:49:32 +02:00
|
|
|
->setProviderType($provider->getProviderType())
|
|
|
|
->setProviderDomain($provider->getProviderDomain());
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
if (!$errors) {
|
|
|
|
$errors = $this->processClientErrors(
|
|
|
|
$provider,
|
|
|
|
$request->getStr('errors'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$client_token_raw = $request->getStr('token');
|
|
|
|
$client_token = json_decode($client_token_raw, true);
|
|
|
|
if (!is_array($client_token)) {
|
|
|
|
$errors[] = pht(
|
|
|
|
'There was an error decoding token information submitted by the '.
|
|
|
|
'client. Expected a JSON-encoded token dictionary, received: %s.',
|
|
|
|
nonempty($client_token_raw, pht('nothing')));
|
|
|
|
} else {
|
|
|
|
if (!$provider->validateCreatePaymentMethodToken($client_token)) {
|
|
|
|
$errors[] = pht(
|
|
|
|
'There was an error with the payment token submitted by the '.
|
|
|
|
'client. Expected a valid dictionary, received: %s.',
|
|
|
|
$client_token_raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!$errors) {
|
|
|
|
$errors = $provider->createPaymentMethodFromRequest(
|
|
|
|
$request,
|
|
|
|
$method,
|
|
|
|
$client_token);
|
|
|
|
}
|
|
|
|
}
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
if (!$errors) {
|
|
|
|
$method->save();
|
|
|
|
|
|
|
|
$save_uri = new PhutilURI($account_uri);
|
|
|
|
$save_uri->setFragment('payment');
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($save_uri);
|
|
|
|
} else {
|
|
|
|
$dialog = id(new AphrontDialogView())
|
|
|
|
->setUser($user)
|
|
|
|
->setTitle(pht('Error Adding Payment Method'))
|
|
|
|
->appendChild(id(new AphrontErrorView())->setErrors($errors))
|
|
|
|
->addCancelButton($request->getRequestURI());
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
2013-03-28 17:11:42 +01:00
|
|
|
}
|
2013-04-25 18:46:32 +02:00
|
|
|
}
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$form = $provider->renderCreatePaymentMethodForm($request, $errors);
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$form
|
|
|
|
->setUser($user)
|
|
|
|
->setAction($request->getRequestURI())
|
|
|
|
->setWorkflow(true)
|
|
|
|
->addHiddenInput('providerKey', $provider_key)
|
|
|
|
->addHiddenInput('isProviderForm', true)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->setValue(pht('Add Payment Method'))
|
|
|
|
->addCancelButton($account_uri));
|
2013-03-28 17:11:42 +01:00
|
|
|
|
|
|
|
if ($errors) {
|
|
|
|
$errors = id(new AphrontErrorView())
|
|
|
|
->setErrors($errors);
|
|
|
|
}
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
return $this->buildResponse(
|
|
|
|
array($errors, $form),
|
|
|
|
$account_uri);
|
|
|
|
}
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
private function renderSelectProvider(
|
|
|
|
PhortunePaymentProvider $provider) {
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$request = $this->getRequest();
|
|
|
|
$user = $request->getUser();
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$description = $provider->getPaymentMethodDescription();
|
|
|
|
$icon = $provider->getPaymentMethodIcon();
|
|
|
|
$details = $provider->getPaymentMethodProviderDescription();
|
|
|
|
|
|
|
|
$button = phutil_tag(
|
|
|
|
'button',
|
|
|
|
array(
|
|
|
|
'class' => 'grey',
|
|
|
|
),
|
2013-03-28 17:11:42 +01:00
|
|
|
array(
|
2013-04-25 18:46:32 +02:00
|
|
|
$description,
|
|
|
|
phutil_tag('br'),
|
|
|
|
$icon,
|
|
|
|
$details,
|
2013-03-28 17:11:42 +01:00
|
|
|
));
|
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($user)
|
|
|
|
->addHiddenInput('providerKey', $provider->getProviderKey())
|
|
|
|
->appendChild($button);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildResponse($content, $account_uri) {
|
|
|
|
$request = $this->getRequest();
|
|
|
|
|
2013-03-28 17:11:42 +01:00
|
|
|
$title = pht('Add Payment Method');
|
2013-09-17 18:12:37 +02:00
|
|
|
$header = id(new PHUIHeaderView())
|
2013-04-25 18:46:32 +02:00
|
|
|
->setHeader($title);
|
2013-03-28 17:11:42 +01:00
|
|
|
|
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
2013-12-19 02:47:34 +01:00
|
|
|
$crumbs->addTextCrumb(pht('Account'), $account_uri);
|
|
|
|
$crumbs->addTextCrumb(pht('Payment Methods'), $request->getRequestURI());
|
2013-03-28 17:11:42 +01:00
|
|
|
|
2013-04-25 18:46:32 +02:00
|
|
|
return $this->buildApplicationPage(
|
|
|
|
array(
|
|
|
|
$crumbs,
|
|
|
|
$header,
|
|
|
|
$content,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => $title,
|
|
|
|
'device' => true,
|
|
|
|
));
|
2013-03-28 17:11:42 +01:00
|
|
|
}
|
|
|
|
|
2013-04-25 18:49:32 +02:00
|
|
|
private function processClientErrors(
|
|
|
|
PhortunePaymentProvider $provider,
|
|
|
|
$client_errors_raw) {
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
|
|
|
|
$client_errors = json_decode($client_errors_raw, true);
|
|
|
|
if (!is_array($client_errors)) {
|
|
|
|
$errors[] = pht(
|
|
|
|
'There was an error decoding error information submitted by the '.
|
|
|
|
'client. Expected a JSON-encoded list of error codes, received: %s.',
|
|
|
|
nonempty($client_errors_raw, pht('nothing')));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array_unique($client_errors) as $key => $client_error) {
|
|
|
|
$client_errors[$key] = $provider->translateCreatePaymentMethodErrorCode(
|
|
|
|
$client_error);
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (array_unique($client_errors) as $client_error) {
|
|
|
|
switch ($client_error) {
|
|
|
|
case PhortuneErrCode::ERR_CC_INVALID_NUMBER:
|
|
|
|
$message = pht(
|
|
|
|
'The card number you entered is not a valid card number. Check '.
|
|
|
|
'that you entered it correctly.');
|
|
|
|
break;
|
|
|
|
case PhortuneErrCode::ERR_CC_INVALID_CVC:
|
|
|
|
$message = pht(
|
|
|
|
'The CVC code you entered is not a valid CVC code. Check that '.
|
|
|
|
'you entered it correctly. The CVC code is a 3-digit or 4-digit '.
|
|
|
|
'numeric code which usually appears on the back of the card.');
|
|
|
|
break;
|
|
|
|
case PhortuneErrCode::ERR_CC_INVALID_EXPIRY:
|
|
|
|
$message = pht(
|
|
|
|
'The card expiration date is not a valid expiration date. Check '.
|
|
|
|
'that you entered it correctly. You can not add an expired card '.
|
|
|
|
'as a payment method.');
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$message = $provider->getCreatePaymentErrorMessage($client_error);
|
|
|
|
if (!$message) {
|
|
|
|
$message = pht(
|
|
|
|
"There was an unexpected error ('%s') processing payment ".
|
|
|
|
"information.",
|
|
|
|
$client_error);
|
|
|
|
|
|
|
|
phlog($message);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
$errors[$client_error] = $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $errors;
|
|
|
|
}
|
|
|
|
|
2013-03-28 17:11:42 +01:00
|
|
|
}
|