1. Go to this page and download the library: Download iamport/rest-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
iamport / rest-client example snippets
// 성공 예시
Result {
data : Payment {
imp_uid: "imp_1234567"
merchant_uid : "merchant_1234"
// .. 생략
}
error : null
}
// 에러 예시
Result {
data : null
error : {
"code": -1
"message": "존재하지 않는 결제정보입니다."
"previous": IamportException {
request : Request {}
response: Response {}
handlerContext: {}
message: "존재하지 않는 결제 정보입니다."
code: 404,
// .. 생략
}
}
}
$iamport = new Iamport('YOUR_IMP_REST_API_KEY', 'YOUR_IMP_REST_API_SECRET');
$payment = Payment::getImpUid('imps_410064014595');
$result = $iamport->callApi($payment);
if ($result->isSuccess()) {
// 조회한 결제 정보
$payment_data = $result->getData();
// __get을 통해 API의 Response Model의 값들을 모두 property처럼 접근할 수 있습니다.
// https://api.iamport.kr/#!/payments/getPaymentByImpUid 의 Response Model.
$imp_uid = $payment_data->imp_uid;
// Response 객체에서 편의를 위해 자체적으로 변환해주는 값들의 경우 ( ex: Unix timestamp -> Y-m-d H:is )
// 변환값이 아닌 원본 property 접근은 getAttributes()를 사용합니다.
$paid_at = $payment_data->paid_at;
$original_paid_at = $payment->getAttributes('paid_at');
if ( 결제검증 로직 ) {
// 결제 성공 처리
} else {
// 결제 실패 처리
}
} else {
error_log($result->getError());
}