Iamport is useful payment module in korea. We can save lots of time by using this. I write this for foreigners who want to use the Iamport. You should do a setting for the Iamport before the below process. I will use nodejs, Javascript to explain the process. iamport site : https://www.iamport.kr/ . I recommend to use google chrome translation if you can’t read Korean.
There are two way to get billing key , First one is do a payment by restful API, second one is do a payment by plugin of payment company (PG).
First, i wlll explain how to get a billing key by restful API. you should get a cardnumber, expire date, birthday, two pw by client.
// "/subscription/issue-billing" app post request example (nodejs)
app.post("/subscriptions/issue-billing", async (req, res) => {
try {
const {
card_number, // card number
expiry, // card expiry
birth, // birthday
pwd_2digit, // password
customer_uid, // the unique id that matched with billing key, you can make this like by `cuid_${new Date().getTime()}` } = req.body; // get a token
const getToken = await axios({
url: "https://api.iamport.kr/users/getToken",
method: "post", // POST method
headers: { "Content-Type": "application/json" }, // "Content-Type": "application/json"
data: {
imp_key: "imp_apikey", // REST API key
imp_secret: "ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f" // REST API Secret
}
});
const { access_token } = getToken.data.response;
// get a billing key
const issueBilling = await axios({
url: \`https://api.iamport.kr/subscribe/customers/\${customer_uid}\`,
method: "post",
headers: { "Authorization": access_token },
data: {
card_number, // card number
expiry, // card expiry
birth, // birthday
pwd_2digit, // 2 card passward
}
});
...
const { code, message } = issueBilling;
if (code === 0) { // success
res.send({ status: "success", message: "Billing has successfully issued" });
} else { // fail
res.send({ status: "failed", message });
}
} catch (e) {
res.status(400).send(e);
}
});
customer_uid is required for regular payment. merchant_uid is needed but this one doesn’t important. it can be just any string. For recognizing the payment well. i recommend you to use unique id.
For the regular payment. You should save the customer_uid in your own DB
Second, i wlll explain how to get a billing key by PG plugin.
IMP.request_pay({ // param
pg: "kakaopay",
pay_method: "card", // "card"
merchant_uid: "issue_billingkey_monthly_0001", // can be any string, recommended to be unique
customer_uid: "gildong_0001_1234",
name: "first paymeny",
amount: 0, // the cost that you want to pay.
buyer_email: "gildong@gmail.com",
buyer_name: "name",
buyer_tel: "010-4242-4242",
buyer_addr: "gangnamgu",
buyer_postcode: "01181"
}, function (rsp) { // callback
if (rsp.success) {
// success
} else {
// fail
}
});
if you use React Native, you can do like this. (if you use other client, look at this page, https://www.iamport.kr/getstarted )
import React from ‘react’;
import IMP from ‘iamport-react-native’;
export default function Payment() {
const data ={
pg: ‘kakaopay’,
pay_method: ‘card’,
name: ‘the Name’,
merchant_uid: `mid_${new Date().getTime()}`,
amount: ‘39000’,
buyer_name: ‘buyer name’,
buyer_tel: ‘01012345678’,
buyer_email: ‘example@naver.com’,
buyer_addr: ‘seould gangnamgu’,
buyer_postcode: ‘06018’,
app_scheme: ‘example’,
customer_uid : “customer_” + new Date().getTime()
}
return (
<IMP.Payment
userCode={getUserCode(pg)} // this one you can get in the iamport console.
data={data}
callback={response => navigation.replace(‘PaymentResult’, { response })}
/>);}
the customer_uid should be saved in the own sevice server. That one is used in server-side payment
Next step is getting a accessToken
const getToken = await axios({
url: "https://api.iamport.kr/users/getToken",
method: "post", // POST method
headers: { "Content-Type": "application/json" }, // "Content-Type": "application/json"
data: {
imp_key: "imp_apikey", // REST API key
imp_secret: "ekKoeW8RyKuT0zgaZsUtXXTLQ4AhPFW3ZGseDA6bkA5lamv9OqDMnxyeB9wqOsuO9W3Mx9YSJ4dTqJ3f" // REST API Secret
}
});
const { access_token } = getToken.data.response;
The imp_key and imp_secret are come from the iamport console. Just sign in the iamport site and you can get the key and secret.
Now you can do payment in your server, without the client confirming process.
const paymentResult = await axios({
url: \`https://api.iamport.kr/subscribe/payments/again\`,
method: "post",
headers: { "Authorization": access_token },
data: {
customer_uid,
merchant_uid: "order_monthly_0001", // new merchant id
amount: 8900, //cost
name: "payment"
}
});
If you want to make a regular payment. Use these code.
axios({
url: \`https://api.iamport.kr/subscribe/payments/schedule\`,
method: "post",
headers: { "Authorization": access_token }, // accesstoken
data: {
customer_uid: "gildong_0001_1234", // the customer_uid
schedules: [
{
merchant_uid: "order_monthly_0001", // order_number
schedule_at: 1519862400, // the unix time. that you want to do payment
amount: 8900,
name: "regular payment",
buyer_name: "name",
buyer_tel: "01012345678",
buyer_email: "gildong@gmail.com"
}
]
}
});
These steps are for payment by kakao. if you have a question, let me know. email: songjihyeok@grey-zero.com
Sadly the ‘iamport’ site now only provides korean language. Next time I hope i can explain the site and how to use it. I hope the chrome translation do work well for foreigners.
Reference : https://docs.iamport.kr/implementation/subscription#issue-billing-b-2