2014-08-11 21:07:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhortunePaymentMethodCreateController
|
|
|
|
extends PhortuneController {
|
|
|
|
|
|
|
|
private $accountID;
|
|
|
|
|
|
|
|
public function willProcessRequest(array $data) {
|
|
|
|
$this->accountID = $data['accountID'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function processRequest() {
|
|
|
|
$request = $this->getRequest();
|
2014-10-07 23:41:59 +02:00
|
|
|
$viewer = $request->getUser();
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
$account = id(new PhortuneAccountQuery())
|
2014-10-07 23:41:59 +02:00
|
|
|
->setViewer($viewer)
|
2014-08-11 21:07:35 +02:00
|
|
|
->withIDs(array($this->accountID))
|
|
|
|
->executeOne();
|
|
|
|
if (!$account) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
2015-03-02 22:01:08 +01:00
|
|
|
$account_id = $account->getID();
|
2014-08-11 21:07:35 +02:00
|
|
|
|
2014-10-07 23:41:59 +02:00
|
|
|
$merchant = id(new PhortuneMerchantQuery())
|
|
|
|
->setViewer($viewer)
|
|
|
|
->withIDs(array($request->getInt('merchantID')))
|
|
|
|
->executeOne();
|
|
|
|
if (!$merchant) {
|
|
|
|
return new Aphront404Response();
|
|
|
|
}
|
|
|
|
|
2014-10-08 00:07:01 +02:00
|
|
|
$cart_id = $request->getInt('cartID');
|
2015-03-02 22:01:08 +01:00
|
|
|
$subscription_id = $request->getInt('subscriptionID');
|
2014-10-08 00:07:01 +02:00
|
|
|
if ($cart_id) {
|
|
|
|
$cancel_uri = $this->getApplicationURI("cart/{$cart_id}/checkout/");
|
2015-03-02 22:01:08 +01:00
|
|
|
} else if ($subscription_id) {
|
|
|
|
$cancel_uri = $this->getApplicationURI(
|
|
|
|
"{$account_id}/subscription/edit/{$subscription_id}/");
|
2014-10-08 00:07:01 +02:00
|
|
|
} else {
|
|
|
|
$cancel_uri = $this->getApplicationURI($account->getID().'/');
|
|
|
|
}
|
2014-08-11 21:07:35 +02:00
|
|
|
|
2014-10-07 23:41:59 +02:00
|
|
|
$providers = $this->loadCreatePaymentMethodProvidersForMerchant($merchant);
|
2014-08-11 21:07:35 +02:00
|
|
|
if (!$providers) {
|
|
|
|
throw new Exception(
|
|
|
|
'There are no payment providers enabled that can add payment '.
|
|
|
|
'methods.');
|
|
|
|
}
|
|
|
|
|
2015-03-02 22:01:08 +01:00
|
|
|
if (count($providers) == 1) {
|
|
|
|
// If there's only one provider, always choose it.
|
|
|
|
$provider_id = head_key($providers);
|
|
|
|
} else {
|
|
|
|
$provider_id = $request->getInt('providerID');
|
|
|
|
if (empty($providers[$provider_id])) {
|
|
|
|
$choices = array();
|
|
|
|
foreach ($providers as $provider) {
|
|
|
|
$choices[] = $this->renderSelectProvider($provider);
|
|
|
|
}
|
2014-08-11 21:07:35 +02:00
|
|
|
|
2015-03-02 22:01:08 +01:00
|
|
|
$content = phutil_tag(
|
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phortune-payment-method-list',
|
|
|
|
),
|
|
|
|
$choices);
|
|
|
|
|
|
|
|
return $this->newDialog()
|
|
|
|
->setRenderDialogAsDiv(true)
|
|
|
|
->setTitle(pht('Add Payment Method'))
|
|
|
|
->appendParagraph(pht('Choose a payment method to add:'))
|
|
|
|
->appendChild($content)
|
|
|
|
->addCancelButton($cancel_uri);
|
|
|
|
}
|
2014-08-11 21:07:35 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 23:41:59 +02:00
|
|
|
$provider = $providers[$provider_id];
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
if ($request->isFormPost() && $request->getBool('isProviderForm')) {
|
|
|
|
$method = id(new PhortunePaymentMethod())
|
|
|
|
->setAccountPHID($account->getPHID())
|
2014-10-07 23:41:59 +02:00
|
|
|
->setAuthorPHID($viewer->getPHID())
|
|
|
|
->setMerchantPHID($merchant->getPHID())
|
|
|
|
->setProviderPHID($provider->getProviderConfig()->getPHID())
|
|
|
|
->setStatus(PhortunePaymentMethod::STATUS_ACTIVE);
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$errors = $this->processClientErrors(
|
|
|
|
$provider,
|
|
|
|
$request->getStr('errors'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$client_token_raw = $request->getStr('token');
|
2015-05-05 12:20:11 +02:00
|
|
|
$client_token = null;
|
|
|
|
try {
|
|
|
|
$client_token = phutil_json_decode($client_token_raw);
|
|
|
|
} catch (PhutilJSONParserException $ex) {
|
2014-08-11 21:07:35 +02:00
|
|
|
$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')));
|
|
|
|
}
|
2015-05-05 12:20:11 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2014-08-11 21:07:35 +02:00
|
|
|
if (!$errors) {
|
|
|
|
$errors = $provider->createPaymentMethodFromRequest(
|
|
|
|
$request,
|
|
|
|
$method,
|
|
|
|
$client_token);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$errors) {
|
|
|
|
$method->save();
|
|
|
|
|
2014-10-07 23:41:59 +02:00
|
|
|
// If we added this method on a cart flow, return to the cart to
|
|
|
|
// check out.
|
|
|
|
if ($cart_id) {
|
|
|
|
$next_uri = $this->getApplicationURI(
|
|
|
|
"cart/{$cart_id}/checkout/?paymentMethodID=".$method->getID());
|
2015-03-02 22:01:08 +01:00
|
|
|
} else if ($subscription_id) {
|
|
|
|
$next_uri = $cancel_uri;
|
2014-10-07 23:41:59 +02:00
|
|
|
} else {
|
2014-10-08 00:07:01 +02:00
|
|
|
$account_uri = $this->getApplicationURI($account->getID().'/');
|
2014-10-07 23:41:59 +02:00
|
|
|
$next_uri = new PhutilURI($account_uri);
|
|
|
|
$next_uri->setFragment('payment');
|
|
|
|
}
|
|
|
|
|
|
|
|
return id(new AphrontRedirectResponse())->setURI($next_uri);
|
2014-08-11 21:07:35 +02:00
|
|
|
} else {
|
|
|
|
$dialog = id(new AphrontDialogView())
|
2014-10-07 23:41:59 +02:00
|
|
|
->setUser($viewer)
|
2014-08-11 21:07:35 +02:00
|
|
|
->setTitle(pht('Error Adding Payment Method'))
|
2015-03-01 23:45:56 +01:00
|
|
|
->appendChild(id(new PHUIInfoView())->setErrors($errors))
|
2014-08-11 21:07:35 +02:00
|
|
|
->addCancelButton($request->getRequestURI());
|
|
|
|
|
|
|
|
return id(new AphrontDialogResponse())->setDialog($dialog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$form = $provider->renderCreatePaymentMethodForm($request, $errors);
|
|
|
|
|
|
|
|
$form
|
2014-10-07 23:41:59 +02:00
|
|
|
->setUser($viewer)
|
2014-08-11 21:07:35 +02:00
|
|
|
->setAction($request->getRequestURI())
|
|
|
|
->setWorkflow(true)
|
2014-10-07 23:41:59 +02:00
|
|
|
->addHiddenInput('providerID', $provider_id)
|
|
|
|
->addHiddenInput('cartID', $request->getInt('cartID'))
|
2015-03-02 22:01:08 +01:00
|
|
|
->addHiddenInput('subscriptionID', $request->getInt('subscriptionID'))
|
2014-08-11 21:07:35 +02:00
|
|
|
->addHiddenInput('isProviderForm', true)
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->setValue(pht('Add Payment Method'))
|
2014-10-08 00:07:01 +02:00
|
|
|
->addCancelButton($cancel_uri));
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
$box = id(new PHUIObjectBoxView())
|
|
|
|
->setHeaderText($provider->getPaymentMethodDescription())
|
|
|
|
->setForm($form);
|
|
|
|
|
|
|
|
$crumbs = $this->buildApplicationCrumbs();
|
|
|
|
$crumbs->addTextCrumb(pht('Add Payment Method'));
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
|
|
|
array(
|
|
|
|
$crumbs,
|
|
|
|
$box,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'title' => $provider->getPaymentMethodDescription(),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renderSelectProvider(
|
|
|
|
PhortunePaymentProvider $provider) {
|
|
|
|
|
|
|
|
$request = $this->getRequest();
|
2014-10-07 23:41:59 +02:00
|
|
|
$viewer = $request->getUser();
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
$description = $provider->getPaymentMethodDescription();
|
|
|
|
$icon_uri = $provider->getPaymentMethodIcon();
|
|
|
|
$details = $provider->getPaymentMethodProviderDescription();
|
|
|
|
|
|
|
|
$this->requireResource('phortune-css');
|
|
|
|
|
|
|
|
$icon = id(new PHUIIconView())
|
2014-10-08 00:07:01 +02:00
|
|
|
->setSpriteSheet(PHUIIconView::SPRITE_LOGIN)
|
|
|
|
->setSpriteIcon($provider->getPaymentMethodIcon());
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
$button = id(new PHUIButtonView())
|
|
|
|
->setSize(PHUIButtonView::BIG)
|
|
|
|
->setColor(PHUIButtonView::GREY)
|
|
|
|
->setIcon($icon)
|
|
|
|
->setText($description)
|
2014-10-08 00:07:01 +02:00
|
|
|
->setSubtext($details)
|
|
|
|
->setMetadata(array('disableWorkflow' => true));
|
2014-08-11 21:07:35 +02:00
|
|
|
|
|
|
|
$form = id(new AphrontFormView())
|
2014-10-07 23:41:59 +02:00
|
|
|
->setUser($viewer)
|
2014-10-08 00:07:01 +02:00
|
|
|
->setAction($request->getRequestURI())
|
2014-10-07 23:41:59 +02:00
|
|
|
->addHiddenInput('providerID', $provider->getProviderConfig()->getID())
|
2014-08-11 21:07:35 +02:00
|
|
|
->appendChild($button);
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function processClientErrors(
|
|
|
|
PhortunePaymentProvider $provider,
|
|
|
|
$client_errors_raw) {
|
|
|
|
|
|
|
|
$errors = array();
|
|
|
|
|
2015-05-05 12:20:11 +02:00
|
|
|
$client_errors = null;
|
|
|
|
try {
|
|
|
|
$client_errors = phutil_json_decode($client_errors_raw);
|
|
|
|
} catch (PhutilJSONParserException $ex) {
|
2014-08-11 21:07:35 +02:00
|
|
|
$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->getCreatePaymentMethodErrorMessage(
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|