/**
  * Attempt to make a payment.
  * 
  * @param  array $data returnUrl/cancelUrl + customer creditcard and billing/shipping details.
  * 	Some keys (e.g. "amount") are overwritten with data from the associated {@link $payment}.
  *  If this array is constructed from user data (e.g. a form submission), please take care
  *  to whitelist accepted fields, in order to ensure sensitive gateway parameters like "freeShipping" can't be set.
  *  If using {@link Form->getData()}, only fields which exist in the form are returned,
  *  effectively whitelisting against arbitrary user input.
  * @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();
     }
     $message = $this->createMessage('PurchaseRequest');
     $message->SuccessURL = isset($data['returnUrl']) ? $data['returnUrl'] : $this->returnurl;
     $message->FailureURL = isset($data['cancelUrl']) ? $data['cancelUrl'] : $this->cancelurl;
     $message->write();
     $request = $this->oGateway()->purchase(array_merge($data, array('card' => $this->getCreditCard($data), 'amount' => (double) $this->payment->MoneyAmount, 'currency' => $this->payment->MoneyCurrency, 'transactionId' => $message->Identifier, 'clientIp' => isset($data['clientIp']) ? $data['clientIp'] : null, 'returnUrl' => PaymentGatewayController::get_return_url($message, 'complete'), 'notifyUrl' => PaymentGatewayController::get_return_url($message, 'notify'), 'cancelUrl' => PaymentGatewayController::get_return_url($message, 'cancel'))));
     $this->logToFile($request->getParameters(), "PurchaseRequest_post");
     $gatewayresponse = $this->createGatewayResponse();
     try {
         $response = $this->response = $request->send();
         $gatewayresponse->setOmnipayResponse($response);
         //update payment model
         if (GatewayInfo::is_manual($this->payment->Gateway)) {
             //initiate manual payment
             $this->createMessage('AuthorizedResponse', $response);
             $this->payment->Status = 'Authorized';
             $this->payment->write();
             $gatewayresponse->setMessage("Manual payment authorised");
         } elseif ($response->isSuccessful()) {
             //successful payment
             $this->createMessage('PurchasedResponse', $response);
             $this->payment->Status = 'Captured';
             $gatewayresponse->setMessage("Payment successful");
             $this->payment->write();
             $this->payment->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 {
             //handle error
             $this->createMessage('PurchaseError', $response);
             $this->payment->Status = 'Void';
             $this->payment->write();
             $gatewayresponse->setMessage("Error (" . $response->getCode() . "): " . $response->getMessage());
         }
     } catch (Omnipay\Common\Exception\OmnipayException $e) {
         $this->createMessage('PurchaseError', $e);
         $this->payment->Status = 'Void';
         $this->payment->write();
         $gatewayresponse->setMessage($e->getMessage());
     }
     $gatewayresponse->setRedirectURL($this->getRedirectURL());
     return $gatewayresponse;
 }
 public function testReturnUrlGeneration()
 {
     $transaction = $this->objFromFixture('GatewayMessage', 'message1');
     $url = PaymentGatewayController::get_return_url($transaction, 'action');
     $this->assertEquals(Director::absoluteURL("paymentendpoint/UNIQUEHASH23q5123tqasdf/action"), $url, "generated url");
 }
 /**
  * Generate a return/notify url for off-site gateways (completePayment).
  * @return string endpoint url
  */
 protected function getEndpointURL($action, $identifier)
 {
     return PaymentGatewayController::get_endpoint_url($action, $identifier);
 }
 public function testReturnUrlGeneration()
 {
     $url = PaymentGatewayController::get_endpoint_url('action', "UniqueHashHere12345");
     $this->assertEquals(Director::absoluteURL("paymentendpoint/UniqueHashHere12345/action"), $url, "generated url");
 }