Skip to content

Commit

Permalink
Add webhook support (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
onurpolattimur authored Apr 29, 2024
1 parent bf2aba4 commit 0b8b5f9
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ public function index()

$data['action'] = $this->url->link('extension/payment/craftgate_payment_gateway', 'user_token=' . $this->session->data['user_token'], 'SSL');
$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'], 'SSL');

$this->load->model('localisation/order_status');
if ($data['payment_craftgate_payment_gateway_order_status_id'] == '') {
$data['payment_craftgate_payment_gateway_order_status_id'] = $this->config->get('config_order_status_id');
}

$data['webhook_url'] = HTTPS_CATALOG . 'index.php?route=extension/payment/craftgate_payment_gateway/webhook';
$data['order_statuses'] = $this->model_localisation_order_status->getOrderStatuses();
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');

$this->response->setOutput($this->load->view('extension/payment/craftgate_payment_gateway', $data));
}

Expand All @@ -61,11 +60,11 @@ protected function validate()
}

foreach ($this->settings_fields as $field) {
if(empty($field['rules'])) continue;
if (empty($field['rules'])) continue;

$field_name = $field['name'];
if($field['rules'] === 'required' && empty($this->request->post[$field_name])){
$field_error= $this->language->get("error_$field_name");
if ($field['rules'] === 'required' && empty($this->request->post[$field_name])) {
$field_error = $this->language->get("error_$field_name");
$error_text = $field_error != "error_$field_name" ? $field_error : $this->language->get("error_required");
$this->error[$field_name] = $error_text;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
$_['text_enabled_payment_methods_help'] = 'Payment methods to be displayed on the Payment Form. All available payment methods are shown when this field is empty.</br>Example: CARD, SODEXO</br>For details: <a target="_blank" href="https://developer.craftgate.io">Craftgate Developer Portal</a>';
$_['text_order_status_help'] = 'Status of order after payment completed ';
$_['text_order_help'] = 'Method order in checkout page.';
$_['text_webhook_url_help'] = 'The URL that payment results will be sent to on the server-side. You should enter this webhook address to Craftgate Merchant Panel to get webhook request. You can see details at <a href="https://developer.craftgate.io/webhook">here</a>.';

// Entry
$_['entry_status'] = 'Status';
Expand All @@ -36,6 +37,7 @@
$_['entry_order_status'] = 'Order Status';
$_['entry_cancel_order_status'] = "Cancel Order Status";
$_['entry_sort_order'] = 'Sort Order';
$_['entry_webhook_url'] = 'Webhook URL';


// Error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
$_['error_required'] = 'Bu alanın doldurulması zorunludur.';
$_['entry_order_status'] = 'Sipariş Durumu';
$_['entry_sort_order'] = 'Sıra No';

$_['entry_webhook_url'] = 'Webhook URL';
$_['text_webhook_url_help'] = 'Ödeme sonucunun iletileceği URL. Webhook isteklerini alabilmek için Craftgate Merchant Panel üzerinden bu adresi girmelisiniz. Detaylara <a href="https://developer.craftgate.io/webhook">buradan</a> ulaşabilirsiniz.';

// Error
$_['error_permission'] = 'Yetkiniz yok';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<style>
.description{
.description {
margin: 5px 0 0 0;
color: #949494;
}
Expand Down Expand Up @@ -39,7 +39,7 @@
<option value="1" {{ payment_craftgate_payment_gateway_status ? 'selected' : '' }}>{{ text_enabled }}</option>
<option value="0" {{ payment_craftgate_payment_gateway_status ? '' : 'selected' }}>{{ text_disabled }}</option>
</select>
<p class="description">{{text_status_help}}</p>
<p class="description">{{ text_status_help }}</p>
</div>
</div>

Expand All @@ -48,11 +48,12 @@
for="payment_craftgate_payment_gateway_live_api_key"> {{ entry_title }}</label>
<div class="col-sm-10">
<input type="text" name="payment_craftgate_payment_gateway_title"
value="{{ payment_craftgate_payment_gateway_title ? payment_craftgate_payment_gateway_title : text_title }}" class="form-control" placeholder="{{ text_title }}"/>
value="{{ payment_craftgate_payment_gateway_title ? payment_craftgate_payment_gateway_title : text_title }}"
class="form-control" placeholder="{{ text_title }}"/>
{% if error_payment_craftgate_payment_gateway_title %}
<span class="text-danger">{{ error_payment_craftgate_payment_gateway_title }}</span>
{% endif %}
<p class="description">{{text_title_help}}</p>
<p class="description">{{ text_title_help }}</p>
</div>
</div>

Expand All @@ -65,7 +66,7 @@
{% if error_payment_craftgate_payment_gateway_live_api_key %}
<span class="text-danger">{{ error_payment_craftgate_payment_gateway_live_api_key }}</span>
{% endif %}
<p class="description">{{text_live_api_key_help}}</p>
<p class="description">{{ text_live_api_key_help }}</p>
</div>
</div>

Expand All @@ -79,7 +80,7 @@
{% if error_payment_craftgate_payment_gateway_live_secret_key %}
<span class="text-danger">{{ error_payment_craftgate_payment_gateway_live_secret_key }}</span>
{% endif %}
<p class="description">{{text_live_secret_key_help}}</p>
<p class="description">{{ text_live_secret_key_help }}</p>
</div>
</div>

Expand All @@ -93,7 +94,7 @@
{% if error_payment_craftgate_payment_gateway_sandbox_api_key %}
<span class="text-danger">{{ error_payment_craftgate_payment_gateway_sandbox_api_key }}</span>
{% endif %}
<p class="description">{{text_sandbox_api_key_help}}</p>
<p class="description">{{ text_sandbox_api_key_help }}</p>
</div>
</div>

Expand All @@ -107,7 +108,7 @@
{% if error_payment_craftgate_payment_gateway_sandbox_secret_key %}
<span class="text-danger">{{ error_payment_craftgate_payment_gateway_sandbox_secret_key }}</span>
{% endif %}
<p class="description">{{text_sandbox_secret_key_help}}</p>
<p class="description">{{ text_sandbox_secret_key_help }}</p>
</div>
</div>

Expand All @@ -134,13 +135,13 @@
{% if error_payment_craftgate_payment_gateway_enabled_payment_methods %}
<span class="text-danger">{{ error_payment_craftgate_payment_gateway_enabled_payment_methods }}</span>
{% endif %}
<p class="description">{{text_enabled_payment_methods_help}}</p>
<p class="description">{{ text_enabled_payment_methods_help }}</p>
</div>
</div>

<div class="form-group">
<label class="col-sm-2 control-label" for="payment_craftgate_payment_gateway_order_status_id">
{{ entry_order_status }}
{{ entry_order_status }}
</label>
<div class="col-sm-10">
<select name="payment_craftgate_payment_gateway_order_status_id" id="input-order-status"
Expand Down Expand Up @@ -170,6 +171,15 @@

</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">{{ entry_webhook_url }}</label>
<div class="col-sm-10" style="position: relative">
<input type="text" disabled value="{{ webhook_url }}" size="1" class="form-control"/>
<button type="button" class="btn btn-primary btn-copy" style="position: absolute; top:0; right: 10px"
data-value="{{ webhook_url }}">{{ button_copy }}</button>
<p class="description">{{ text_webhook_url_help }}</p>
</div>
</div>
</div>
</div>
</form>
Expand All @@ -178,6 +188,29 @@
</div>
</div>

<script>
function copyToClipboard(text) {
var input = document.createElement('input');
input.style.position = 'fixed';
input.style.opacity = 0;
input.value = text;
document.body.appendChild(input);
input.select();
document.execCommand('copy');
document.body.removeChild(input);
}
document.addEventListener('DOMContentLoaded', function () {
var copyButtons = document.querySelectorAll('.btn-copy');
copyButtons.forEach(function (copyButton) {
copyButton.addEventListener('click', function () {
var valueToCopy = this.getAttribute('data-value');
copyToClipboard(valueToCopy);
});
});
});
</script>

{{ footer }}


Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

class ControllerExtensionPaymentCraftgatePaymentGateway extends Controller
{
const ORDER_STATUS_ID_INITIAL = 0;
const ORDER_STATUS_ID_FAILED = 10;

public function index()
{
$this->load->language('extension/payment/craftgate_payment_gateway');
Expand Down Expand Up @@ -33,21 +36,12 @@ public function callback()
try {
$this->validateCallbackParams();
$order_id = $this->request->get['order_id'];
$order_info = $this->getOrder($order_id);

$checkout_form_result = $this->initCraftgateClient()->retrieveCheckoutPayment($this->request->post["token"]);
$this->validateOrder($checkout_form_result);

if (!isset($checkout_form_result->paymentError) && $checkout_form_result->paymentStatus === 'SUCCESS') {
$message = 'Craftgate Payment Id: ' . $checkout_form_result->id . ' <br>';
$message .= 'Craftgate Payment URL: ' . $this->buildCraftgatePaymentUrl($checkout_form_result->id);
$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_craftgate_payment_gateway_order_status_id'), $message, false);

if ($checkout_form_result->installment > 1) {
$this->model_extension_payment_craftgate_payment_gateway->addInstallmentFeeToOrder($order_info['order_id'], $checkout_form_result->paidPrice);
$installmentMessage = $checkout_form_result->cardBrand . ' - ' . $checkout_form_result->installment . ' Installment';
$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_craftgate_payment_gateway_order_status_id'), $installmentMessage);
}
$this->complete_order($checkout_form_result, $order_id);
echo "<script>window.top.location.href = '" . $this->url->link('checkout/success', '', $server_conn_slug) . "';</script>";
} else {
$error = $checkout_form_result->paymentError->errorCode . ' - ' . $checkout_form_result->paymentError->errorDescription . ' - ' . $checkout_form_result->paymentError->errorGroup;
Expand All @@ -61,6 +55,58 @@ public function callback()
}
}

public function webhook()
{
$webhook_data = json_decode(file_get_contents('php://input'), true);
if (!$this->should_process_webhook_request($webhook_data)) {
exit();
}

$checkout_token = $webhook_data['payloadId'];
$checkout_form_result = $this->initCraftgateClient()->retrieveCheckoutPayment($checkout_token);

if ($checkout_form_result->paymentStatus !== 'SUCCESS' || !isset($checkout_form_result->conversationId)) {
exit();
}

$this->complete_order($checkout_form_result, $checkout_form_result->conversationId);
}

private function complete_order($checkout_form_result, $order_id)
{
$order_info = $this->getOrder($order_id);
if (!$this->is_order_eligible_to_complete($order_info)) {
return;
}

$message = 'Craftgate Payment Id: ' . $checkout_form_result->id . ' <br>';
$message .= 'Craftgate Payment URL: ' . $this->buildCraftgatePaymentUrl($checkout_form_result->id);
$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_craftgate_payment_gateway_order_status_id'), $message, false);
if ($checkout_form_result->installment > 1) {
$this->model_extension_payment_craftgate_payment_gateway->addInstallmentFeeToOrder($order_info['order_id'], $checkout_form_result->paidPrice);
$installmentMessage = $checkout_form_result->cardBrand . ' - ' . $checkout_form_result->installment . ' Installment';
$this->model_checkout_order->addOrderHistory($order_id, $this->config->get('payment_craftgate_payment_gateway_order_status_id'), $installmentMessage);
}
}

private function should_process_webhook_request($webhook_data)
{
if (!isset($webhook_data) || $_SERVER['REQUEST_METHOD'] !== 'POST') {
return false;
}

if ($webhook_data['eventType'] !== 'CHECKOUTFORM_AUTH' || $webhook_data['status'] !== "SUCCESS" || !isset($webhook_data['payloadId'])) {
return false;
}
return true;
}

private function is_order_eligible_to_complete($order_info)
{
$order_status_id = $order_info['order_status_id'];
return in_array($order_status_id, array(self::ORDER_STATUS_ID_INITIAL, self::ORDER_STATUS_ID_FAILED));
}

private function initCraftgateClient()
{
$this->load->model('extension/payment/craftgate_payment_gateway');
Expand Down

0 comments on commit 0b8b5f9

Please sign in to comment.