/** * Get the gateway object that will be used for the given payment method. * The gateway class is automatically retrieved based on configuration * * @param String $methodName * @return PaymentGateway */ public static function get_gateway($methodName) { // Get the gateway environment setting $environment = PaymentGateway::get_environment(); // Get the custom class configuration if applicable. // If not, apply naming convention. $methodConfig = self::get_factory_config($methodName); $gatewayClassConfig = $methodConfig['gateway_classes']; if (isset($gatewayClassConfig[$environment])) { $gatewayClass = $gatewayClassConfig[$environment]; } else { switch ($environment) { case 'live': $gatewayClass = $methodName . 'Gateway_Production'; break; case 'dev': $gatewayClass = $methodName . 'Gateway_Dev'; break; case 'test': $gatewayClass = $methodName . 'Gateway_Mock'; break; } } if (class_exists($gatewayClass)) { return new $gatewayClass(); } else { throw new Exception("{$gatewayClass} class does not exists."); } }
/** * Get the supported methods array set by the yaml configuraion * * @return array */ public static function get_supported_methods() { $methodConfig = Config::inst()->get('PaymentProcessor', 'supported_methods'); $environment = PaymentGateway::get_environment(); // Check if all methods are defined in factory foreach ($methodConfig[$environment] as $method) { if (!PaymentFactory::get_factory_config($method)) { user_error("Method {$method} not defined in factory", E_USER_ERROR); } } return $methodConfig[$environment]; }
/** * Send a request to the gateway to process the payment * @param {array} $data Data to be passed to the gateway * @return {PaymentGateway_Result} Payment result object */ public function process($data) { $api_key = ''; if (PaymentGateway::get_environment() == 'dev') { $api_key = Config::inst()->get('StripeGateway', 'test_api_secret'); } else { $api_key = Config::inst()->get('StripeGateway', 'api_secret'); } if (empty($api_key)) { return new PaymentGateway_Failure(403, _t('StripeGateway.NO_API_KEY', '_No api key configured, you must configure StripeGateway.api_secret for live and StripeGateway.test_api_secret for dev')); } //Set the api key in the Stripe library Stripe::setApiKey($api_key); //Create the Stripe charge try { $response = Stripe_Charge::create(array('amount' => intval($data['Amount']) * 100, 'currency' => $data['Currency'], 'card' => array('number' => implode('', $data['CardNumber']), 'exp_month' => $data['MonthExpiry'], 'exp_year' => $data['YearExpiry'], 'cvc' => $data['Cvc2'], 'name' => $data['FirstName'] . ' ' . $data['LastName'], 'address_line1' => $data['BillingAddress'], 'address_line2' => $data['BillingAddress2'], 'address_city' => $data['BillingCity'], 'address_zip' => $data['BillingZIPCode'], 'address_state' => $data['BillingState'], 'address_country' => $data['BillingCountry']), 'description' => _t('StripeGateway.PAYMENT_DESCRIPTION', '_{sitetitle} Store Sale', array('sitetitle' => class_exists('SiteConfig') ? SiteConfig::get()->first()->Title : 'SilverStripe')))); if ($response->paid) { return new PaymentGateway_Success($status); } else { if ($response->cvc_check == 'fail' || $response->address_line1_check == 'fail' || $response->address_zip_check == 'fail') { $errors = array(); if ($response->cvc_check == 'fail') { $errors[] = _t('StripeGateway.FAIL_CVC', '_The credit card\'s security code (cvc) is invalid'); } if ($response->address_line1_check == 'fail') { $errors[] = _t('StripeGateway.FAIL_ADDRESS', '_Your bank declined the charge based on your address information, please verify the information and try again'); } if ($response->address_zip_check == 'fail') { $errors[] = _t('StripeGateway.FAIL_ZIP_CODE', '_Your bank declined the charge based on your postal code/zip code, please verify it and try again'); } return new PaymentGateway_Failure(null, $errors); } else { return new PaymentGateway_Incomplete(); } } } catch (Stripe_Error $e) { return new PaymentGateway_Failure(new SS_HTTPResponse('', $e->getHttpStatus()), array($e->getCode() => $e->getMessage())); } return new PaymentGateway_Failure(); }