일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- ssmtp
- no key alg
- openfire
- 중독 게임
- 리플리케이션 오류
- mongodb
- 워드프레스한글팩
- node.js
- MySQL
- set foreign_key
- xcache
- 날짜계산
- 훼인
- security.limit_extensions
- 태권브이
- rsync
- php-fpm
- 뒤로 가기
- node 이미지 저장
- 앞으로 가기
- 태권v
- 특정 패키지 업데이트 중지 / 해제
- simplexml_load_filesimplexml
- php
- 외래키
- strtotime
- 이미지주소추출
- quota
- 쁘띠프랑스
- 재귀쿼리
- Today
- Total
일상 기록 창고
카카오 페이 연동을 위해 만든 클래스 본문
워낙에 다음카카오API가 잘 되어 있어서 후딱 만들어서 사용.. 개발부터 테스트까지 오전시간에 완료할 정도로 쉬웠음.
CURL 을 사용하기 때문에 CURL은 필수
<?php class KakaoPay{ private $cAdminKey = ''; // 카카오 어드민 키 private $cTestStoreKey = 'TC0ONETIME'; // 상점 아이디 ( 테스트키 : TC0ONETIME ) private $cStoreKey = ''; // 상점 아이디 private $cServiceUrl = 'kapi.kakao.com'; // 서비스 URL private $cMonthStoreKey = ''; // 상점 아이디 ( 정기결제 ) private $cTestMonthStoreKey = 'TCSUBSCRIP'; // 상점 아이디 ( 정기결제 테스트키 : TCSUBSCRIP ) private $aPayPostUrl = Array(); private $cApprovalUrl = ''; // 결제 성공 URL private $cFailUrl = ''; // 결제 실패 URL private $cCancelUrl = ''; // 결제 취소 URL private $cHeaderInfo = Array(); public function __construct( $type='' ){ $this->aPayPostUrl = Array( 'ready' => 'https://kapi.kakao.com/v1/payment/ready', // 결제 준비 요청 'approval' => 'https://kapi.kakao.com/v1/payment/approve', // 결제 승인 요청 ); } // 결제 코드 변경 하기 public function setStoreKey( $cType ) { if( $cType != '' ) { $cType = strtoupper( $cType ); if( $cType == 'S' ) { $this->cStoreKey = $this->cStoreKey; } elseif( $cType == 'T' ) { $this->cStoreKey = $this->cMonthStoreKey; } elseif( $cType == 'TM' ) { $this->cStoreKey = $this->cTestMonthStoreKey; } else { $this->cStoreKey = $this->cTestStoreKey; } } } public function initCurl( $aData ) { $aPostData = ''; foreach( $aData['sData'] As $sKey => $val ) { $aPostData .= $aPostData == '' ? $sKey.'='.urlencode($val) : '&'.$sKey.'='.urlencode($val); } $curl = null; $curl = curl_init(); curl_setopt( $curl, CURLOPT_URL, $aData['sUrl'] ); curl_setopt( $curl, CURLOPT_BINARYTRANSFER, 1); curl_setopt( $curl, CURLOPT_HTTPHEADER, $this->cHeaderInfo); curl_setopt( $curl, CURLOPT_HEADER, 0); curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt( $curl, CURLOPT_POST, 1 ); curl_setopt( $curl, CURLOPT_POSTFIELDS, $aPostData ); $gData = curl_exec( $curl ); curl_close($curl); return $gData; } /** * 결제 준비 */ function kakaoPayReady( $aInfo ) { $aResult = ''; if( COUNT($aInfo) > 0 && is_array( $aInfo ) ) { $this->cHeaderInfo = Array( 'POST /v1/payment/ready HTTP/1.1', 'Host: '.$this->cServiceUrl, 'Authorization: KakaoAK '.$this->cAdminKey, 'Content-type: application/x-www-form-urlencoded;charset=utf-8' ); $aPostData = Array( 'cid' => $this->cStoreKey, // 가맹점 코드. 10자. O String 'partner_order_id' => $aInfo['nOrderNo'], // 가맹점 주문번호. 최대 100자 O String 'partner_user_id' => $aInfo['nUserNo'], // 가맹점 회원 id. 최대 100자 O String 'item_name' => $aInfo['aProdName'], // 상품명. 최대 100자 O String 'quantity' => 1, // 상품 수량 O Integer 'total_amount' => $aInfo['nTotalPrice'], // 상품 총액 O Integer 'tax_free_amount' => 0, // 상품 비과세 금액 O Integer 'vat_amount' => $aInfo['nVat'], // 상품 부가세 금액(안보낼 경우 (상품총액 - 상품 비과세 금액)/11 : 소숫점이하 반올림) X Integer 'approval_url' => $this->cApprovalUrl, // 결제 성공시 redirect url. 최대 255자 O String 'cancel_url' => $this->cCancelUrl, // 결제 취소시 redirect url. 최대 255자 O String 'fail_url' => $this->cFailUrl, // 결제 실패시 redirect url. 최대 255자 O String ); $aSendData = Array( 'sUrl' => $this->aPayPostUrl['ready'], 'sData' => $aPostData ); $aResult = $this->initCurl( $aSendData ); $aResult = json_decode( $aResult, true ); } return $aResult; } /** * 결제 승인 요청 */ public function approveRequest( $aInfo ) { $aResult = ''; if( COUNT($aInfo) > 0 && is_array( $aInfo ) ) { $this->cHeaderInfo = Array( 'POST /v1/payment/approve HTTP/1.1', 'Host: '.$this->cServiceUrl, 'Authorization: KakaoAK '.$this->cAdminKey, 'Content-type: application/x-www-form-urlencoded;charset=utf-8' ); $aPostData = Array( 'cid' => $this->cStoreKey, // 가맹점 코드. 10자. O String 'tid' => $aInfo['tid'], // 결제 고유번호 'partner_order_id' => $aInfo['nOrderNo'], // 가맹점 주문번호. 최대 100자 O String 'partner_user_id' => $aInfo['nUserNo'], // 가맹점 회원 id. 최대 100자 O String 'pg_token' => $aInfo['cPgToken'], // 결제 승인 요청 시 가져오는 값 ); $aSendData = Array( 'sUrl' => $this->aPayPostUrl['approval'], 'sData' => $aPostData ); $aResult = $this->initCurl( $aSendData ); $aResult = json_decode( $aResult, true ); } return $aResult; } } ?> |
기본적으로 결제 진행에 앞서 처리 부분은 제외하고 필요한 부분만 처리
'프로그래밍 > PHP' 카테고리의 다른 글
스핑크스 검색엔진 마리아 디비 searchd 실행 시 오류 처리 (0) | 2018.08.22 |
---|---|
파이썬으로 ssh 접속하여 cpu,메모리,os,아이피 정보 가져오기 (0) | 2018.04.12 |
초를 일,시, 분 추출하기... (0) | 2017.08.11 |
Laravel 프레임워크 설치 오류.. (0) | 2015.03.31 |
외환은행 환율 정보 파싱 (0) | 2014.10.27 |