(function(root, factory) {

if (typeof module === 'object' && module.exports) {
    // CommonJS-like environments that support module.exports, like Node.
    module.exports = factory(require('logtown').getLogger('/src/handlers/CreateRefundHandler'), require('../ApiClient'), require('../api/RefundApi'), require('../OrbipayApiError'), require('../CommonUtil'));
  } 
}(this, function(logger, ApiClient, RefundApi, OrbipayApiError, CommonUtil) {
  'use strict';

  /**
   * @module ApiClient
   */

  /**
   * <h3 style="color:red"> This class subject to change without prior notice, Please dont use this class directly. </h3>

   * @module handlers/CreateRefundHandler
   * @class
   */

  var exports = function() {
  };

  exports.prototype.process = function (refund, callback) {
        
        var secret = refund.invocation_context.secret;
		var host = refund.api_end_point;
        var api_key = refund.invocation_context.api_key;
        var auth_scheme = refund.invocation_context.auth_scheme;
        var _this=this;
		var response = {};
        var exception;
        try {
            var api_client = new ApiClient(secret, host,api_key,auth_scheme);
			var api_instance = new RefundApi(api_client);
			var channel = refund.invocation_context['channel'];
			var client_key = refund.invocation_context['client_key'];
			var product = refund.invocation_context['product'];
			var timestamp;
			if (refund.invocation_context['timestamp'])
			{
				timestamp = refund.invocation_context['timestamp'];
			}		
			else
			{
				timestamp = CommonUtil.getTimeStamp();
			}
			var idempotent_request_key = refund.invocation_context['idempotent_request_key'];
			var requestor_type = refund.invocation_context['requestor_type'];
			var id_payment = refund['ID_PAYMENT'];
			var id_customer = refund['ID_CUSTOMER'];
			var create_refund_request = _this.populate_body(refund);
            var opts = _this.getOptions(refund);
			var result = api_instance.createRefund(channel, client_key, product, timestamp, idempotent_request_key, requestor_type, id_payment, id_customer, create_refund_request,  opts, callback);
			response['request'] = result;
			return response;
        } catch (e) {
            exception = OrbipayApiError.getDefaultException(e);
            if (callback) {
                callback(null, null, null, exception);
            }
            response['exception'] = exception;
            return response;
        }
    };

    exports.prototype.getOptions = function (refund) {
        return {
		    'requestor': refund['invocation_context']['requestor'], 
		    'x_opay_headers': refund['invocation_context']['X-OPAY-Headers']
        };
    };  

    exports.prototype.populate_body = function(refund){
		var CreateRefundRequest = require('../models/CreateRefundRequest');

		return  CreateRefundRequest.constructFromObject(refund);
	};


  return exports;
}));