{
  "slug": "Impelient-Bank-Mellat-Payment-Gateway-in-Nestjs-71f67308fa3a",
  "title": "Impelient Bank Mellat Payment Gateway in Nestjs",
  "subtitle": "Simple Code of node js",
  "excerpt": "Simple Code of node js",
  "date": "2025-07-15",
  "tags": [
    "Node.js",
    "Payments"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/impelient-bank-mellat-payment-gateway-in-nestjs-71f67308fa3a",
  "hero": "https://cdn-images-1.medium.com/max/800/1*HdRMG1vmq_pOqPJ1qA8H4g.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "Impelient Bank Mellat Payment Gateway in Nestjs"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Simple Code of node.js"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "مستندات پیاده سازی بانک ملت"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*HdRMG1vmq_pOqPJ1qA8H4g.png",
      "alt": "Impelient Bank Mellat Payment Gateway in Nestjs",
      "caption": "",
      "width": 961,
      "height": 313
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Initial Payment"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*SbRAdjMb03g6B1-8qLn8LA.png",
      "alt": "Impelient Bank Mellat Payment Gateway in Nestjs",
      "caption": "",
      "width": 896,
      "height": 590
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*qwr7KT6xx_LAULjv9K2BQw.png",
      "alt": "Impelient Bank Mellat Payment Gateway in Nestjs",
      "caption": "",
      "width": 725,
      "height": 712
    },
    {
      "type": "heading",
      "level": 3,
      "text": "mellat.type.ts"
    },
    {
      "type": "code",
      "lang": "javascript",
      "code": "export const MellatConfiguration = {\n  GatewayAddress: 'https://bpm.shaparak.ir/pgwchannel/payment.mellat?RefId=', WsdlInitial:\n  'https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl', WsdlConfirm:\n  'https://bpm.shaparak.ir/pgwchannel/services/pgw?wsdl',\n};"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "payment.controller.ts"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "@Post('/mellat/openTransaction') @UseGuards(CustomerGuard) @ApiOkResponse({\n  type: () => CreatePaymentMellatResultDto\n}) @ApiBearerAuth() createMellat(@Body() createPaymentDto: CreatePaymentMellatDto, @Req() req: {\n  user: Partial<User>\n},) {\n  return this.paymentMellatService.openTransaction(createPaymentDto, req.user,);\n}"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "Soap Initial Mellat payment"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "return await new Promise<any>((resolve, reject) => {\n  soap.createClient(\n    MellatConfiguration.WsdlInitial,\n    options,\n    function (err: any, client: any) {\n      if (err) {\n        console.error(\"Error creating SOAP client:\", err);\n        reject(err);\n      }\n      client.bpPayRequest(requestData, (err, result) => {\n        if (err) {\n          console.error(\"Error making SOAP request:\", err);\n          reject(err);\n        }\n        resolve(result);\n      });\n    },\n  );\n});"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "payment.mellat.service.ts"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "import {\n  Injectable, InternalServerErrorException\n}\nfrom '@nestjs/common';\nimport {\n  Payment, PaymentGateway, PaymentStatus,\n}\nfrom './entities/payment.entity';\nimport {\n  Model\n}\nfrom 'mongoose';\nimport {\n  InjectModel\n}\nfrom '@nestjs/mongoose';\nimport {\n  CreatePaymentMellatDto\n}\nfrom './dto/create.mellat.dto';\nimport {\n  User\n}\nfrom 'src/users/schemas/user.schema';\nimport configuration from 'src/config/configuration';\nimport {\n  MellatConfiguration\n}\nfrom './entities/mellat.type';\nconst soap = require('soap');\nconst moment = require('moment');\n@Injectable()\nexport class PaymentMellatService {\n  constructor(@InjectModel(Payment.name) private paymentModel: Model<Payment>,) {\n  }\n  async openTransaction(createPaymentDto: CreatePaymentMellatDto, user: Partial<User>,) {\n    let token = '';\n    const localDate = moment().format('YYYYMMDD');\n    const localTime = moment().format('HHmmss');\n    // Make a SOAP request\n    const requestData = {\n      terminalId: configuration().mellat.terminalId, userName: configuration().mellat.username,\n      userPassword: configuration().mellat.userPassword, orderId: createPaymentDto.uniqueID, amount:\n      createPaymentDto.amount, localDate: localDate, localTime: localTime, additionalData:\n      createPaymentDto.additionalData, callBackUrl: configuration().mellat.callBackUrl, payerId: 0,\n      mobileNo: user.phoneNumber,\n    };\n    try {\n      const resolve = await this.SendRequestInitial(requestData);\n      if (!resolve.hasOwnProperty('return')) {\n        console.log('Return not found');\n        throw new InternalServerErrorException('Return not found');\n      }\n      const str = resolve.return;\n      const mySplit = str.split(',');\n      if (mySplit[0] != '0') {\n        console.log(mySplit[0]);\n        throw new InternalServerErrorException(mySplit[0]);\n      }\n      token = mySplit[1];\n      await this.paymentModel.create({\n        user: user, amount: createPaymentDto.amount, uniqueID: createPaymentDto.uniqueID, status:\n        PaymentStatus.Initial, gateway: PaymentGateway.Mellat, authority_code: token, authority:\n        resolve,\n      });\n    }\n    catch (error) {\n      console.log(error);\n      throw new InternalServerErrorException();\n    }\n    return {\n      paymentLink: MellatConfiguration.GatewayAddress + token, token: token,\n    };\n  }\n  async SendRequestInitial(requestData: any): Promise<any> {\n    moment.locale('en');\n    const options = {\n      overrideRootElement: {\n        namespace: 'ns1',\n      },\n    };\n    return await new Promise<any>((resolve, reject) => {\n      soap.createClient(MellatConfiguration.WsdlInitial, options, function (err: any, client: any) {\n        if (err) {\n          console.error('Error creating SOAP client:', err);\n          reject(err);\n        }\n        client.bpPayRequest(requestData, (err, result) => {\n          if (err) {\n            console.error('Error making SOAP request:', err);\n            reject(err);\n          }\n          resolve(result);\n        });\n      },);\n    });\n  }\n}"
    },
    {
      "type": "heading",
      "level": 3,
      "text": "configuration.ts"
    },
    {
      "type": "code",
      "lang": "typescript",
      "code": "mellat: {    callBackUrl: process.env.MELLAT_CALLBACK_URL,    terminalId:\nprocess.env.MELLAT_TERMINAL_ID,    username: process.env.MELLAT_USERNAME,    userPassword:\nprocess.env.MELLAT_USER_PASSWORD,  },"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Verify Transaction"
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*NmvUmTmOA91S1P7_u5Dw9g.png",
      "alt": "Impelient Bank Mellat Payment Gateway in Nestjs",
      "caption": "",
      "width": 864,
      "height": 660
    },
    {
      "type": "paragraph",
      "html": "After the initial transaction and gateway callback, verifying the transaction is mandatory."
    }
  ]
}
