<commit_before><?php

use Omnipay\Common\CreditCard;

class PurchaseService extends PaymentService{

	/**
	 * Attempt to make a payment
	 * @param  array $data returnUrl/cancelUrl + customer creditcard
	 * and billing/shipping details.
	 * @return ResponseInterface omnipay's response class,
	 * specific to the chosen gateway.
	 */
	public function purchase($data = array()) {
		if ($this->payment->Status !== "Created") {
			return null; //could be handled better? send payment response?
		}
		if (!$this->payment->isInDB()) {
			$this->payment->write();
		}
		$this->returnurl = isset($data['returnUrl']) ?
							$data['returnUrl'] :
							$this->returnurl;
		$this->cancelurl = isset($data['cancelUrl']) ?
							$data['cancelUrl'] :
							$this->cancelurl;
		$message = $this->createMessage('PurchaseRequest');
		$request = $this->oGateway()->purchase(array(
			'card' => new CreditCard($data),
			'amount' => (float) $this->payment->MoneyAmount,
			'currency' => $this->payment->MoneyCurrency,
			'transactionId' => $message->Identifier,
			'clientIp' => isset($data['clientIp']) ? $data['clientIp'] : null,
			'returnUrl' => PaymentGatewayController::get_return_url(
								$message, 'complete', $this->returnurl
							),
			'cancelUrl' => PaymentGatewayController::get_return_url(
								$message, 'cancel', $this->cancelurl
							)
		));
		$this->logToFile($request->getParameters());
		$gatewayresponse = $this->createGatewayResponse();
		try {
			$response = $this->response = $request->send();
			//update payment model
			if (GatewayInfo::is_manual($this->payment->Gateway)) {
				$this->createMessage('AuthorizedResponse', $response);
				$this->payment->Status = 'Authorized';
				$this->payment->write();
				$gatewayresponse->setMessage("Manual payment authorised");
			} elseif ($response->isSuccessful()) {
				$this->createMessage('PurchasedResponse', $response);
				$this->payment->Status = 'Captured';
				$this->payment->write();
				$gatewayresponse->setMessage("Payment successful");
				$this->extend('onCaptured', $gatewayresponse);
			} elseif ($response->isRedirect()) {
				// redirect to off-site payment gateway
				$this->createMessage('PurchaseRedirectResponse', $response);
				$this->payment->Status = 'Authorized';
				$this->payment->write();
				$gatewayresponse->setMessage("Redirecting to gateway");
			} else {
				$this->createMessage('PurchaseError', $response);
				$gatewayresponse->setMessage(
					"Error (".$response->getCode()."): ".$response->getMessage()
				);
			}
			$gatewayresponse->setOmnipayResponse($response);
		} catch (Exception $e) {
			//TODO: only handle 'some' exceptions, throw the rest
			$this->createMessage('PurchaseError', $e->getMessage());
			$gatewayresponse->setMessage($e->getMessage());
		}
		$gatewayresponse->setRedirectURL($this->getRedirectURL());

		return $gatewayresponse;
	}

	/**
	 * Finalise this payment, after off-site external processing.
	 * This is ususally only called by PaymentGatewayController.
	 * @return PaymentResponse encapsulated response info
	 */
	public function completePurchase() {
		$gatewayresponse = $this->createGatewayResponse();
		$request = $this->oGateway()->completePurchase(array(
			'amount' => (float) $this->payment->MoneyAmount
		));
		$this->createMessage('CompletePurchaseRequest', $request);
		$response = null;
		try {
			$response = $this->response = $request->send();
			if ($response->isSuccessful()) {
				$this->createMessage('PurchasedResponse', $response);
				$this->payment->Status = 'Captured';
				$this->payment->write();
				$this->extend('onCaptured', $gatewayresponse);
			} else {
				$this->createMessage('CompletePurchaseError', $response);
			}
			$gatewayresponse->setOmnipayResponse($response);
		} catch (\Exception $e) {
			$this->createMessage("CompletePurchaseError", $e->getMessage());
		}

		return $gatewayresponse;
	}

}<commit_msg>Make sure only OmnipayExceptions are caught, and not every exception, when processing payments. This means that regular exceptions should be picked up by SS log etc.<commit_after><?php

use Omnipay\Common\CreditCard;

class PurchaseService extends PaymentService{

	/**
	 * Attempt to make a payment
	 * @param  array $data returnUrl/cancelUrl + customer creditcard
	 * and billing/shipping details.
	 * @return ResponseInterface omnipay's response class,
	 * specific to the chosen gateway.
	 */
	public function purchase($data = array()) {
		if ($this->payment->Status !== "Created") {
			return null; //could be handled better? send payment response?
		}
		if (!$this->payment->isInDB()) {
			$this->payment->write();
		}
		$this->returnurl = isset($data['returnUrl']) ?
							$data['returnUrl'] :
							$this->returnurl;
		$this->cancelurl = isset($data['cancelUrl']) ?
							$data['cancelUrl'] :
							$this->cancelurl;
		$message = $this->createMessage('PurchaseRequest');
		$request = $this->oGateway()->purchase(array(
			'card' => new CreditCard($data),
			'amount' => (float) $this->payment->MoneyAmount,
			'currency' => $this->payment->MoneyCurrency,
			'transactionId' => $message->Identifier,
			'clientIp' => isset($data['clientIp']) ? $data['clientIp'] : null,
			'returnUrl' => PaymentGatewayController::get_return_url(
								$message, 'complete', $this->returnurl
							),
			'cancelUrl' => PaymentGatewayController::get_return_url(
								$message, 'cancel', $this->cancelurl
							)
		));
		$this->logToFile($request->getParameters());
		$gatewayresponse = $this->createGatewayResponse();
		try {
			$response = $this->response = $request->send();
			//update payment model
			if (GatewayInfo::is_manual($this->payment->Gateway)) {
				//initiate manual payment
				$this->createMessage('AuthorizedResponse', $response);
				$this->payment->Status = 'Authorized';
				$gatewayresponse->setMessage("Manual payment authorised");
			} elseif ($response->isSuccessful()) {
				//successful payment
				$this->createMessage('PurchasedResponse', $response);
				$this->payment->Status = 'Captured';
				$gatewayresponse->setMessage("Payment successful");
				$this->extend('onCaptured', $gatewayresponse);
			} elseif ($response->isRedirect()) {
				// redirect to off-site payment gateway
				$this->createMessage('PurchaseRedirectResponse', $response);
				$this->payment->Status = 'Authorized';
				$gatewayresponse->setMessage("Redirecting to gateway");
			} else {
				//handle error
				$this->createMessage('PurchaseError', $response);
				$gatewayresponse->setMessage(
					"Error (".$response->getCode()."): ".$response->getMessage()
				);
			}
			$this->payment->write();
			$gatewayresponse->setOmnipayResponse($response);
		} catch (Omnipay\Common\Exception\OmnipayException $e) {
			$this->createMessage('PurchaseError', $e->getMessage());
			$gatewayresponse->setMessage($e->getMessage());
		}
		$gatewayresponse->setRedirectURL($this->getRedirectURL());

		return $gatewayresponse;
	}

	/**
	 * Finalise this payment, after off-site external processing.
	 * This is ususally only called by PaymentGatewayController.
	 * @return PaymentResponse encapsulated response info
	 */
	public function completePurchase() {
		$gatewayresponse = $this->createGatewayResponse();
		$request = $this->oGateway()->completePurchase(array(
			'amount' => (float) $this->payment->MoneyAmount
		));
		$this->createMessage('CompletePurchaseRequest', $request);
		$response = null;
		try {
			$response = $this->response = $request->send();
			if ($response->isSuccessful()) {
				$this->createMessage('PurchasedResponse', $response);
				$this->payment->Status = 'Captured';
				$this->payment->write();
				$this->extend('onCaptured', $gatewayresponse);
			} else {
				$this->createMessage('CompletePurchaseError', $response);
			}
			$gatewayresponse->setOmnipayResponse($response);
		} catch (Omnipay\Common\Exception\OmnipayException $e) {
			$this->createMessage("CompletePurchaseError", $e->getMessage());
		}

		return $gatewayresponse;
	}

}
{'fraction_non_alphanumeric': 0.16911487758945387, 'fraction_numerical': 0.0, 'mean_word_length': 4.471153846153846, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 171, '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': '105982', 'n_tokens_mistral': 2765, 'n_tokens_neox': 2352, 'n_words': 450}