1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-09-22 18:28:47 +02:00
phorge-phorge/src/applications/phortune/provider/PhortuneWePayPaymentProvider.php
epriestley 49ef13e876 Add WePay as a one-time payment provider
Summary:
Ref T2787.

I //think// we could also use WePay as a recurring payment provider, but this is somewhat messy (OAuth + requires account) -- basically it's "add a WePay account" instead of "add a credit card".

The WePay checkout workflow is a bit upsell-y but basically reasonable.

I like that their API just has a `request($method, $params)` method instead of 30,000 lines of methods for each request type. I did hit one bug; I'll send a pull for that.

Test Plan: Got as far as the charge callback in testing; the rest isn't implemented for any provider yet.

Reviewers: btrahan, vrana, chad

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2787

Differential Revision: https://secure.phabricator.com/D5982
2013-05-21 15:34:46 -07:00

187 lines
4.8 KiB
PHP

<?php
final class PhortuneWePayPaymentProvider extends PhortunePaymentProvider {
public function isEnabled() {
return $this->getWePayClientID() &&
$this->getWePayClientSecret() &&
$this->getWePayAccessToken() &&
$this->getWePayAccountID();
}
public function getProviderType() {
return 'wepay';
}
public function getProviderDomain() {
return 'wepay.com';
}
public function getPaymentMethodDescription() {
return pht('Credit Card or Bank Account');
}
public function getPaymentMethodIcon() {
return 'rsrc/phortune/wepay.png';
}
public function getPaymentMethodProviderDescription() {
return "WePay";
}
public function canHandlePaymentMethod(PhortunePaymentMethod $method) {
$type = $method->getMetadataValue('type');
return ($type == 'wepay');
}
protected function executeCharge(
PhortunePaymentMethod $payment_method,
PhortuneCharge $charge) {
throw new Exception("!");
}
private function getWePayClientID() {
return PhabricatorEnv::getEnvConfig('phortune.wepay.client-id');
}
private function getWePayClientSecret() {
return PhabricatorEnv::getEnvConfig('phortune.wepay.client-secret');
}
private function getWePayAccessToken() {
return PhabricatorEnv::getEnvConfig('phortune.wepay.access-token');
}
private function getWePayAccountID() {
return PhabricatorEnv::getEnvConfig('phortune.wepay.account-id');
}
/* -( One-Time Payments )-------------------------------------------------- */
public function canProcessOneTimePayments() {
return true;
}
public function renderOneTimePaymentButton(
PhortuneAccount $account,
PhortuneCart $cart,
PhabricatorUser $user) {
$uri = $this->getControllerURI(
'checkout',
array(
'cartID' => $cart->getID(),
));
return phabricator_form(
$user,
array(
'action' => $uri,
'method' => 'POST',
),
phutil_tag(
'button',
array(
'class' => 'green',
'type' => 'submit',
),
pht('Pay with WePay')));
}
/* -( Controllers )-------------------------------------------------------- */
public function canRespondToControllerAction($action) {
switch ($action) {
case 'checkout':
case 'charge':
case 'cancel':
return true;
}
return parent::canRespondToControllerAction();
}
/**
* @phutil-external-symbol class WePay
*/
public function processControllerRequest(
PhortuneProviderController $controller,
AphrontRequest $request) {
$cart = $controller->loadCart($request->getInt('cartID'));
if (!$cart) {
return new Aphront404Response();
}
$root = dirname(phutil_get_library_root('phabricator'));
require_once $root.'/externals/wepay/wepay.php';
WePay::useStaging(
$this->getWePayClientID(),
$this->getWePayClientSecret());
$wepay = new WePay($this->getWePayAccessToken());
switch ($controller->getAction()) {
case 'checkout':
$return_uri = $this->getControllerURI(
'charge',
array(
'cartID' => $cart->getID(),
));
$cancel_uri = $this->getControllerURI(
'cancel',
array(
'cartID' => $cart->getID(),
));
$total_in_cents = $cart->getTotalInCents();
$price = PhortuneCurrency::newFromUSDCents($total_in_cents);
$params = array(
'account_id' => $this->getWePayAccountID(),
'short_description' => 'Services', // TODO
'type' => 'SERVICE',
'amount' => $price->formatBareValue(),
'long_description' => 'Services', // TODO
'reference_id' => $cart->getPHID(),
'app_fee' => 0,
'fee_payer' => 'Payee',
'redirect_uri' => $return_uri,
'fallback_uri' => $cancel_uri,
'auto_capture' => false,
'require_shipping' => 0,
'shipping_fee' => 0,
'charge_tax' => 0,
'mode' => 'regular',
'funding_sources' => 'bank,cc'
);
$result = $wepay->request('checkout/create', $params);
// NOTE: We might want to store "$result->checkout_id" on the Cart.
$uri = new PhutilURI($result->checkout_uri);
return id(new AphrontRedirectResponse())->setURI($uri);
case 'charge':
// NOTE: We get $_REQUEST['checkout_id'] here, but our parameters are
// dropped so we should stop depending on them or shove them into the
// URI.
var_dump($_REQUEST);
break;
case 'cancel':
var_dump($_REQUEST);
break;
}
throw new Exception("The rest of this isn't implemented yet.");
}
}