/** * Create a payment model, and provide link to redirect to external gateway, * or redirect to order link. * * @return string - url for redirection after payment has been made */ public function makePayment($gateway, $gatewaydata = array()) { //create payment $payment = $this->createPayment($gateway); if (!$payment) { //errors have been stored. return false; } // make payment with authorize service instead of direct purchase $service = AuthorizeCaptureService::create($payment)->setReturnUrl($this->getReturnUrl()); // authorize the money, create the order $response = $service->authorize($this->getGatewayData($gatewaydata)); if (GatewayInfo::is_manual($gateway) || $response->isSuccessful()) { //don't complete the order at this stage, if payment is manual $this->placeOrder(); } // Create a purchase service, and set the user-facing success URL for redirects /* $service = PurchaseService::create($payment) ->setReturnUrl($this->getReturnUrl()); // Process payment, get the result back $response = $service->purchase($this->getGatewayData($gatewaydata)); if (GatewayInfo::is_manual($gateway)) { //don't complete the payment at this stage, if payment is manual $this->placeOrder(); } elseif ($response->isSuccessful()) { $this->completePayment(); } */ return $response; }
/** * 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(); } //update success/fail urls $this->update($data); //set the client IP address, if not already set if (!isset($data['clientIp'])) { $data['clientIp'] = Controller::curr()->getRequest()->getIP(); } $gatewaydata = array_merge($data, array('card' => $this->getCreditCard($data), 'amount' => (double) $this->payment->MoneyAmount, 'currency' => $this->payment->MoneyCurrency, 'returnUrl' => $this->getEndpointURL("complete", $this->payment->Identifier), 'cancelUrl' => $this->getEndpointURL("cancel", $this->payment->Identifier), 'notifyUrl' => $this->getEndpointURL("notify", $this->payment->Identifier))); if (!isset($gatewaydata['transactionId'])) { $gatewaydata['transactionId'] = $this->payment->Identifier; } $request = $this->oGateway()->purchase($gatewaydata); $message = $this->createMessage('PurchaseRequest', $request); $message->SuccessURL = $this->returnurl; $message->FailureURL = $this->cancelurl; $message->write(); $gatewayresponse = $this->createGatewayResponse(); try { $response = $this->response = $request->send(); $gatewayresponse->setOmnipayResponse($response); //update payment model if ($this->manualpurchasestatus == "Authorized" && GatewayInfo::is_manual($this->payment->Gateway)) { //initiate 'authorized' 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); $gatewayresponse->setMessage("Error (" . $response->getCode() . "): " . $response->getMessage()); } } catch (Omnipay\Common\Exception\OmnipayException $e) { $this->createMessage('PurchaseError', $e); $gatewayresponse->setMessage($e->getMessage()); } $gatewayresponse->setRedirectURL($this->getRedirectURL()); return $gatewayresponse; }
/** * Make payment for a place order, where payment had previously failed. * * @param array $data * @param Form $form * * @return boolean */ public function dopayment($data, $form) { if (self::config()->allow_paying && $this->order && $this->order->canPay()) { // Save payment data from form and process payment $data = $form->getData(); $gateway = !empty($data['PaymentMethod']) ? $data['PaymentMethod'] : null; if (!GatewayInfo::is_manual($gateway)) { $processor = OrderProcessor::create($this->order); $data['cancelUrl'] = $processor->getReturnUrl(); $response = $processor->makePayment($gateway, $data); if ($response) { if ($response->isRedirect() || $response->isSuccessful()) { return $response->redirect(); } $form->sessionMessage($response->getMessage(), 'bad'); } else { $form->sessionMessage($processor->getError(), 'bad'); } } else { $form->sessionMessage(_t('OrderActionsForm.MANUAL_NOT_ALLOWED', "Manual payment not allowed"), 'bad'); } return $this->controller->redirectBack(); } $form->sessionMessage(_t('OrderForm.COULDNOTPROCESSPAYMENT', 'Payment could not be processed.'), 'bad'); $this->controller->redirectBack(); }
/** * 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 checkoutSubmit($data, $form) { // form validation has passed by this point, so we can save data $this->config->setData($form->getData()); $order = $this->config->getOrder(); $gateway = Checkout::get($order)->getSelectedPaymentMethod(false); if (GatewayInfo::is_offsite($gateway) || GatewayInfo::is_manual($gateway) || $this->config->getComponentByType('OnsitePaymentCheckoutComponent')) { return $this->submitpayment($data, $form); } return $this->controller->redirect($this->controller->Link('payment')); }