<commit_before><?php defined('SYSPATH') OR die('No direct script access.');

/**
 * @package    Openbuildings\Purchases
 * @author     <NAME> <<EMAIL>>
 * @copyright  (c) 2013 OpenBuildings Ltd.
 * @license    http://spdx.org/licenses/BSD-3-Clause
 */
class Kohana_Model_Payment_Emp_Vbv extends Model_Payment_Emp {

	/**
	 * Use the current purchase to generate an "authorize url" where you can go and approve the purchase through paypal's interface.
	 * @param  array  $params must provide callback_url
	 * @return Model_Payment_Emp         self
	 */
	public function authorize_processor(array $params = array())
	{
		$currency = $this->purchase->display_currency() ?: $this->purchase->currency();

		$purchase_params = array(
			'refernece'        => $this->purchase->number,
			'amount'           => $this->purchase->total_price(array('is_payable' => TRUE))->as_string($currency),
			'currency'         => $currency,
		);

		$request_params = array_merge($params, $purchase_params);

		try
		{
			$response = Emp::api()
				->request(Openbuildings\Emp\Api::VBVMC3D_AUTH, $request_params);
		}
		catch (Openbuildings\Emp\Exception $exception)
		{
			throw new Exception_Payment('Payment gateway error: :error', array(':error' => $exception->getMessage()), 0, $exception);
		}

		if (Arr::path($response, 'raw.enrollmentstatus') !== 'Y')
			throw new Exception_Payment('Credit card not enrolled in VBV/3D Secure');

		$this->_authorize_url = Arr::path($response, 'raw.bouncerURL');

		$this->set(array(
			'payment_id' => Arr::path($response, 'raw.requestid'),
			'raw_response' => Arr::get($response, 'raw'),
			'status' => Model_Payment::PENDING,
		));

		return $this;
	}

	/**
	 * Perform a payment of the current purchase, set the reponse in payment_id, raw_response and status.
	 * @param  array  $params
	 * @return Model_Payment_Emp         self
	 */
	public function execute_processor(array $params = array())
	{
		$auth_result_params = array(
			'reference' => $this->purchase->number,
			'requestid' => $this->payment_id,
		);

		try
		{
			$auth_result_response = Emp::api()
				->request(Openbuildings\Emp\Api::VBVMC3D_RESULT, $auth_result_params);

			if (Arr::path($auth_result_response, 'raw.authenticationstatus') !== 'Y')
				throw new Exception_Payment('Authentication not complete');

			$vbv_auth_params = Arr::extract($auth_result_response['raw'], array('eci', 'xid', 'cavv'));
<commit_msg>add better errors for VBV<commit_after><?php defined('SYSPATH') OR die('No direct script access.');

/**
 * @package    Openbuildings\Purchases
 * @author     <NAME> <<EMAIL>>
 * @copyright  (c) 2013 OpenBuildings Ltd.
 * @license    http://spdx.org/licenses/BSD-3-Clause
 */
class Kohana_Model_Payment_Emp_Vbv extends Model_Payment_Emp {

	public static $auth_result_codes = array(
		'Y' => 'Authentication Successful',
		'A' => 'Attempts Processing Performed',
		'N' => 'Authentication Failed',
		'U' => 'Authentication Could Not Be Performed',
	);

	/**
	 * Use the current purchase to generate an "authorize url" where you can go and approve the purchase through paypal's interface.
	 * @param  array  $params must provide callback_url
	 * @return Model_Payment_Emp         self
	 */
	public function authorize_processor(array $params = array())
	{
		$currency = $this->purchase->display_currency() ?: $this->purchase->currency();

		$purchase_params = array(
			'refernece'        => $this->purchase->number,
			'amount'           => $this->purchase->total_price(array('is_payable' => TRUE))->as_string($currency),
			'currency'         => $currency,
		);

		$request_params = array_merge($params, $purchase_params);

		try
		{
			$response = Emp::api()
				->request(Openbuildings\Emp\Api::VBVMC3D_AUTH, $request_params);
		}
		catch (Openbuildings\Emp\Exception $exception)
		{
			throw new Exception_Payment('Payment gateway error: :error', array(':error' => $exception->getMessage()), 0, $exception);
		}

		if (Arr::path($response, 'raw.enrollmentstatus') !== 'Y')
			throw new Exception_Payment('Credit card not enrolled in VBV/3D Secure');

		$this->_authorize_url = Arr::path($response, 'raw.bouncerURL');

		$this->set(array(
			'payment_id' => Arr::path($response, 'raw.requestid'),
			'raw_response' => Arr::get($response, 'raw'),
			'status' => Model_Payment::PENDING,
		));

		return $this;
	}

	/**
	 * Perform a payment of the current purchase, set the reponse in payment_id, raw_response and status.
	 * @param  array  $params
	 * @return Model_Payment_Emp         self
	 */
	public function execute_processor(array $params = array())
	{
		$auth_result_params = array(
			'reference' => $this->purchase->number,
			'requestid' => $this->payment_id,
		);

		try
		{
			$auth_response = Emp::api()
				->request(Openbuildings\Emp\Api::VBVMC3D_RESULT, $auth_result_params);

			$result_code = Arr::path($auth_response, 'raw.authenticationstatus');

			if ($result_code !== 'Y')
				throw new Exception_Payment(
					Arr::get(self::$auth_result_codes, $result_code, 'Authentication not complete'),
					0,
					NULL,
					$auth_response['raw']
				);

			$vbv_auth_params = Arr::extract($auth_response['raw'], array('eci', 'xid', 'cavv'));

{'fraction_non_alphanumeric': 0.1560928433268859, 'fraction_numerical': 0.0036750483558994195, 'mean_word_length': 4.233805668016195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2125788', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1655, 'n_words': 368}