---
# System prepended metadata

title: Create Payment API

---

# Create Payment API
```javascript=
router.post('/', verifyAuthToken, async(req, res, next) => {
    try {
        var uid = req.uid
            // var uid = req.body.uid

        var transactionId = 'TR-' + nanoid();

        var transactionItem = {
            transactionId: transactionId,
            uid: uid,
            donationId: req.body.donationId,
            nominal: req.body.nominal,
            description: req.body.description
        }

        var customer = await getUserProfileByUid(uid)

        let parameter = {
            "transaction_details": {
                "order_id": transactionItem.transactionId,
                "gross_amount": transactionItem.nominal
            },
            "credit_card": {
                "secure": true
            },
            "customer_details": {
                "first_name": customer.first_name,
                "last_name": customer.last_name,
                "email": customer.email,
                "phone": customer.phone_number
            },
            "enabled_payments": ["credit_card", "cimb_clicks",
                "bca_klikbca", "bca_klikpay", "bri_epay", "echannel", "permata_va",
                "bca_va", "bni_va", "bri_va", "other_va", "gopay", "indomaret",
                "danamon_online", "akulaku", "shopeepay"
            ],
        };

        var paymentInfo = await snap.createTransaction(parameter)

        var transaction = await createTransaction(transactionItem)

        transaction.payment_data = paymentInfo

        res.status(200).send(transaction)

    } catch (error) {
        res.status(400).send(error)
    }
})

// Only for local testing not for production
router.post('/update', (req, res, next) => {
    updateTransactionStatus(req.query.id, 'Success')
        .then((data) => res.status(200).send(data))
        .catch((error) => res.status(400).send(error))
})

router.post('/notification', async(req, res, next) => {
    try {
        var receivedJson = req.body

        var transactionStatusObject = await core.transaction.notification(receivedJson)

        let transactionId = transactionStatusObject.order_id;
        let transactionStatus = transactionStatusObject.transaction_status;
        let fraudStatus = transactionStatusObject.fraud_status;

        let summary = `Transaction notification received. Order ID: ${transactionId}. Transaction status: ${transactionStatus}. Fraud status: ${fraudStatus}.<br>Raw notification object:<pre>${JSON.stringify(transactionStatusObject, null, 2)}</pre>`;

        if (transactionStatus == 'capture') {
            if (fraudStatus == 'challenge') {
                var transaction = await updateTransactionStatus(transactionId, 'Challenge')

            } else if (fraudStatus == 'accept') {
                // TODO set transaction status on your databaase to 'success'
                var transaction = await updateTransactionStatus(transactionId, 'Success')

            }
        } else if (transactionStatus == 'settlement') {
            // TODO set transaction status on your databaase to 'success'
            var transaction = await updateTransactionStatus(transactionId, 'Success')

        } else if (transactionStatus == 'cancel' ||
            transactionStatus == 'deny' ||
            transactionStatus == 'expire') {
            // TODO set transaction status on your databaase to 'failure'
            var transaction = await updateTransactionStatus(transactionId, 'Failed')

        } else if (transactionStatus == 'pending') {
            // TODO set transaction status on your databaase to 'pending' / waiting payment
            var transaction = await updateTransactionStatus(transactionId, 'Pending')

        } else if (transactionStatus == 'refund') {
            // TODO set transaction status on your databaase to 'refund'
            var transaction = await updateTransactionStatus(transactionId, 'Refund')

        }
        console.log(summary)
        res.status(200).send(summary)
    } catch (error) {
        res.send(error)
    }
})




export const getTransactionsByDonationId = (donationId) => {
    return new Promise((resolve, reject) => {
        db.manyOrNone(getTransactionsByDonationIdSql, [donationId])
            .then((data) => resolve(data))
            .catch((error) => reject(error))
    })
}

export const getTransactionsByUserUid = (uid) => {
    return new Promise((resolve, reject) => {
        db.manyOrNone(getTransactionsByUserUidSql, [uid])
            .then((data) => resolve(data))
            .catch((error) => reject(error))
    })
}

export const createTransaction = (transaction) => {
    return new Promise((resolve, reject) => {

        // console.log(transaction.transactionId)
        // resolve(transaction)
        db.oneOrNone(createTransactionSql, [
                transaction.transactionId,
                transaction.uid,
                transaction.donationId,
                transaction.nominal,
                transaction.description
            ])
            .then((data) => resolve(data))
            .catch((error) => reject(error))
    })
}

export const updateTransactionStatus = (transactionId, status) => {
    return new Promise((resolve, reject) => {
        db.oneOrNone(updateTransactionStatusSql, [status, transactionId])
            .then((data) => resolve(data))
            .catch((error) => reject(error))
    })
}
```