"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports["default"] = void 0;

var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));

var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));

var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));

var _axios = _interopRequireDefault(require("axios"));

var _crypto = _interopRequireDefault(require("crypto"));

var _moment = _interopRequireDefault(require("moment"));

var _qs = _interopRequireDefault(require("qs"));

var _lodash = _interopRequireDefault(require("lodash"));

var BRI = /*#__PURE__*/function () {
  function BRI(_config) {
    var _this = this;

    (0, _classCallCheck2["default"])(this, BRI);
    (0, _defineProperty2["default"])(this, "accessToken", /*#__PURE__*/function () {
      var _ref = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee() {
        var clientId,
            clientSecret,
            _args = arguments;
        return _regenerator["default"].wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                clientId = _args.length > 0 && _args[0] !== undefined ? _args[0] : _this.config.client_id;
                clientSecret = _args.length > 1 && _args[1] !== undefined ? _args[1] : _this.config.client_secret;
                return _context.abrupt("return", _axios["default"].post("".concat(_this.config.state, "oauth/client_credential/accesstoken"), _qs["default"].stringify({
                  client_id: clientId,
                  client_secret: clientSecret
                }), {
                  headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                  },
                  params: {
                    grant_type: 'client_credentials'
                  }
                }));

              case 3:
              case "end":
                return _context.stop();
            }
          }
        }, _callee);
      }));

      return function () {
        return _ref.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "getAccountInfo", /*#__PURE__*/function () {
      var _ref2 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee5(data) {
        return _regenerator["default"].wrap(function _callee5$(_context5) {
          while (1) {
            switch (_context5.prev = _context5.next) {
              case 0:
                _this.getAccountConfig = _lodash["default"].extend({
                  accountNumber: null,
                  token: null
                }, data);
                return _context5.abrupt("return", new Promise( /*#__PURE__*/function () {
                  var _ref3 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee4(resolve, rejected) {
                    var mTimeStamp, mPayload, mHmac, mHeader;
                    return _regenerator["default"].wrap(function _callee4$(_context4) {
                      while (1) {
                        switch (_context4.prev = _context4.next) {
                          case 0:
                            mTimeStamp = "".concat((0, _moment["default"])(new Date(Date.now())).utc(0).toISOString());
                            mPayload = "path=/v2/inquiry/".concat(_this.getAccountConfig.accountNumber, "&verb=GET&token=Bearer ").concat(_this.getAccountConfig.token, "&timestamp=").concat(mTimeStamp, "&body=");
                            mHmac = _crypto["default"].createHmac('SHA256', _this.config.client_secret).update(mPayload).digest('base64');
                            mHeader = {
                              'BRI-Timestamp': mTimeStamp,
                              'BRI-Signature': mHmac,
                              'Authorization': "Bearer ".concat(_this.getAccountConfig.token)
                            };
                            _context4.next = 6;
                            return _axios["default"].get("".concat(_this.config.state, "v").concat(_this.config.version, "/inquiry/").concat(_this.getAccountConfig.accountNumber), {
                              headers: mHeader
                            }).then( /*#__PURE__*/function () {
                              var _ref4 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee2(res) {
                                return _regenerator["default"].wrap(function _callee2$(_context2) {
                                  while (1) {
                                    switch (_context2.prev = _context2.next) {
                                      case 0:
                                        res.data.responseCode === '0100' ? resolve(res.data) : rejected(res.data);

                                      case 1:
                                      case "end":
                                        return _context2.stop();
                                    }
                                  }
                                }, _callee2);
                              }));

                              return function (_x4) {
                                return _ref4.apply(this, arguments);
                              };
                            }())["catch"]( /*#__PURE__*/function () {
                              var _ref5 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee3(error) {
                                return _regenerator["default"].wrap(function _callee3$(_context3) {
                                  while (1) {
                                    switch (_context3.prev = _context3.next) {
                                      case 0:
                                        rejected(error);
                                        console.log(mHeader);

                                      case 2:
                                      case "end":
                                        return _context3.stop();
                                    }
                                  }
                                }, _callee3);
                              }));

                              return function (_x5) {
                                return _ref5.apply(this, arguments);
                              };
                            }());

                          case 6:
                          case "end":
                            return _context4.stop();
                        }
                      }
                    }, _callee4);
                  }));

                  return function (_x2, _x3) {
                    return _ref3.apply(this, arguments);
                  };
                }()));

              case 2:
              case "end":
                return _context5.stop();
            }
          }
        }, _callee5);
      }));

      return function (_x) {
        return _ref2.apply(this, arguments);
      };
    }());
    (0, _defineProperty2["default"])(this, "briva", {
      Create: function () {
        var _Create = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(config) {
          return _regenerator["default"].wrap(function _callee9$(_context9) {
            while (1) {
              switch (_context9.prev = _context9.next) {
                case 0:
                  _this.brivaConfig = _lodash["default"].extend({
                    url: "".concat(_this.config.state, "v1/briva"),
                    token: null,
                    requestData: {}
                  }, config);
                  return _context9.abrupt("return", new Promise( /*#__PURE__*/function () {
                    var _ref6 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(resolve, rejected) {
                      var mTimeStamp, mPayload, mHmac, mHeader;
                      return _regenerator["default"].wrap(function _callee8$(_context8) {
                        while (1) {
                          switch (_context8.prev = _context8.next) {
                            case 0:
                              mTimeStamp = "".concat((0, _moment["default"])(new Date(Date.now())).utc(0).toISOString());
                              mPayload = "path=/v1/briva    &verb=GET&token=Bearer ".concat(_this.brivaConfig.token, "&timestamp=").concat(mTimeStamp, "&body=");
                              mHmac = _crypto["default"].createHmac('SHA256', _this.config.client_secret).update(mPayload).digest('base64');
                              mHeader = {
                                'BRI-Timestamp': mTimeStamp,
                                'BRI-Signature': mHmac,
                                'Authorization': "Bearer ".concat(_this.brivaConfig.token),
                                'Content-Type': 'application/json'
                              };
                              _context8.next = 6;
                              return _axios["default"].post(_this.brivaConfig.url, _this.brivaConfig.requestData, {
                                headers: mHeader
                              }).then( /*#__PURE__*/function () {
                                var _ref7 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee6(res) {
                                  return _regenerator["default"].wrap(function _callee6$(_context6) {
                                    while (1) {
                                      switch (_context6.prev = _context6.next) {
                                        case 0:
                                          res.data.responseCode === '0100' ? resolve(res.data) : rejected(res.data);

                                        case 1:
                                        case "end":
                                          return _context6.stop();
                                      }
                                    }
                                  }, _callee6);
                                }));

                                return function (_x9) {
                                  return _ref7.apply(this, arguments);
                                };
                              }())["catch"]( /*#__PURE__*/function () {
                                var _ref8 = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee7(error) {
                                  return _regenerator["default"].wrap(function _callee7$(_context7) {
                                    while (1) {
                                      switch (_context7.prev = _context7.next) {
                                        case 0:
                                          rejected(error);
                                          console.log(mHeader);

                                        case 2:
                                        case "end":
                                          return _context7.stop();
                                      }
                                    }
                                  }, _callee7);
                                }));

                                return function (_x10) {
                                  return _ref8.apply(this, arguments);
                                };
                              }());

                            case 6:
                            case "end":
                              return _context8.stop();
                          }
                        }
                      }, _callee8);
                    }));

                    return function (_x7, _x8) {
                      return _ref6.apply(this, arguments);
                    };
                  }()));

                case 2:
                case "end":
                  return _context9.stop();
              }
            }
          }, _callee9);
        }));

        function Create(_x6) {
          return _Create.apply(this, arguments);
        }

        return Create;
      }()
    });
    this.config = _lodash["default"].extend({
      state: BRI.BRI_STATE_DEVELOPMENT,
      version: 2,
      client_id: null,
      client_secret: null
    }, _config);
  }

  (0, _createClass2["default"])(BRI, null, [{
    key: "BRI_STATE_DEVELOPMENT",
    get: function get() {
      return "https://sandbox.partner.api.bri.co.id/";
    }
  }, {
    key: "BRI_STATE_PRODUCTION",
    get: function get() {
      return "https://partner.api.bri.co.id/";
    }
  }]);
  return BRI;
}();

var _default = BRI;
exports["default"] = _default;
//# sourceMappingURL=index.js.map