From 8bcf290026413a949a1ae6aa63fe8f14dcb18471 Mon Sep 17 00:00:00 2001 From: Giorgos Kontopoulos Date: Thu, 11 Apr 2019 11:35:22 +0300 Subject: [PATCH] Add offsite redirect WinbankPaymentRedirectForm --- .../WinbankPaymentRedirectForm.php | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 src/PluginForm/OffsiteRedirect/WinbankPaymentRedirectForm.php diff --git a/src/PluginForm/OffsiteRedirect/WinbankPaymentRedirectForm.php b/src/PluginForm/OffsiteRedirect/WinbankPaymentRedirectForm.php new file mode 100644 index 0000000..3c12349 --- /dev/null +++ b/src/PluginForm/OffsiteRedirect/WinbankPaymentRedirectForm.php @@ -0,0 +1,116 @@ +entity; + $payment_gateway_plugin = $payment->getPaymentGateway()->getPlugin(); + $config = $payment_gateway_plugin->getConfiguration(); + $order = $payment->getOrder(); + // Format amount + $amount = sprintf('%0.2f', $order->getTotalPrice()->getNumber()); + + if ($config['mode'] == 'test') { + $config["tickets_url"] = $config["tickets_url_test"]; + $config["paycenter_url"] = $config["paycenter_url_test"]; + } + + // Validate merchant_id, other validations ? + if (empty($config['merchant_id'])) { + throw new PaymentGatewayException('Merchant ID not provided.'); + } + + // Get new ticket + try { + $result = $this->getNewTicket($amount, $order->id(), $config); + + if ($result->IssueNewTicketResult->ResultCode == 0) { + + // save response data for validating response later + $order->setData("IssueNewTicketResult",[ + "ResultCode" => $result->IssueNewTicketResult->ResultCode, + "TranTicket" => $result->IssueNewTicketResult->TranTicket, + "Timestamp" => $result->IssueNewTicketResult->Timestamp, + "MinutesToExpiration" => $result->IssueNewTicketResult->MinutesToExpiration, + ]); + + } else { + $message = 'There was a problem connecting ot the ticket issuer: '. $result->IssueNewTicketResult->ResultDescription; + throw new InvalidResponseException($message); + } + } catch (Exception $e) { + throw new InvalidResponseException('Could not connect to bank ticket issuer'); + } + + // Prepare redirect form + $data = [ + 'AcquirerId' => $config['acquirer_id'], + 'MerchantId' => $config['merchant_id'], + 'PosId' => $config['pos_id'], + 'User' => $config['user'], + 'MerchantReference' => $order->id(), + 'LanguageCode' => $config['language_code'], + 'ParamBackLink' => '', + ]; + + $form = $this->buildRedirectForm($form, $form_state, $config['paycenter_url'], $data); + //temporary + unset($form['#attached']['library']); + return $form; + } + + + /** + * Soap request to get NewTicket for transaction + * + * @param int $amount + * The order amount. + * @param int $orderid + * The order id. + * @param int $config + * The configuration object. + * + * @return array + * Object containing the soap service response. + * + */ + public function getNewTicket($amount, $orderid, $config){ + //libxml_disable_entity_loader(false); + $options= array( + 'location' => $config['tickets_url'], + 'uri' => $config['tickets_url'] + ); + $soap = new \SoapClient(NULL, $options); + $ticketRequest = [ + 'Username' => $config['username'], + 'Password' => hash('md5', $config['password']), + 'MerchantId' => $config['merchant_id'], + 'PosId' => $config['pos_id'], + 'AcquirerId' => $config['acquirer_id'], + 'MerchantReference' => $orderid, + 'RequestType' => $config['request_type'], + 'ExpirePreauth' => 0, + 'Amount' => $amount, + 'CurrencyCode' => $config['currency_code'], + 'Installments' => 0, + 'Bnpl' => '0', + 'Parameters' => '' + ]; + $xml = array( + 'Request' => $ticketRequest + ); + return $soap->IssueNewTicket($xml); + } +} -- GitLab