게이트웨이 템플릿
WooCommerce 게이트웨이 템플릿은 WooCommerce POS를 위한 맞춤형 결제 게이트웨이를 생성하기 위한 시작점입니다. 이 템플릿에는 완전한 기능을 갖춘 결제 게이트웨이를 구축하는 데 필요한 모든 보일러플레이트 코드와 구조가 포함되어 있습니다.
특징
- 완전한 템플릿: 모든 필수 메서드가 있는 즉시 사용 가능한 게이트웨이 구조
- POS 통합: WooCommerce POS 호환성을 위해 미리 구성됨
- 자동 설정: 스크립트 기반 템플릿 사용자 정의
- 모범 사례: WordPress 및 WooCommerce 코딩 표준 준수
- 확장 가능: 특정 결제 제공업체에 맞춤 수정 및 확장이 용이
시작하기
옵션 1: 자동 템플릿 생성
템플릿에는 특정 게이트웨이에 맞게 자동으로 템플릿을 사용자 정의하는 스크립트가 포함되어 있습니다:
-
저장소 복제:
git clone https://github.com/wcpos/woocommerce-gateway-template.git
cd woocommerce-gateway-template -
설치 스크립트 실행:
./create-gateway.sh -
프롬프트를 따르기:
- 게이트웨이 이름 입력 (예: "내 결제 게이트웨이")
- 게이트웨이 슬러그 입력 (예: "my-payment")
- 설명 제공
- 스크립트가 사용자 정의된 플러그인을 생성합니다
옵션 2: 템플릿 수동 사용
수동 사용자 지정을 선호하는 경우:
-
템플릿 다운로드:
- 게이트웨이 템플릿 저장소를 방문하세요
- 최신 릴리스를 다운로드하거나 저장소를 복제하세요
-
템플릿 사용자 정의:
{{GATEWAY_NAME}}을(를) 게이트웨이 표시 이름으로 교체합니다{{GATEWAY_SLUG}}을(를) 게이트웨이 고유 식별자로 교체합니다{{GATEWAY_DESCRIPTION}}을(를) 게이트웨이에 대한 설명으로 교체합니다
-
파일 이름 바꾸기:
wcpos-{{GATEWAY_SLUG}}.php의 이름을 게이트웨이 슬러그에 맞게 변경합니다- 파일 헤더 및 플러그인 정보를 업데이트합니다
템플릿 구조
주요 플러그인 파일
주요 플러그인 파일(wcpos-{{GATEWAY_SLUG}}.php)은 다음을 포함합니다:
- 플러그인 헤더: WordPress 플러그인 정보
- 게이트웨이 클래스: 주요 결제 게이트웨이 클래스
- 초기화: 플러그인 설정 및 훅
- 통합: WooCommerce POS 호환성
주요 구성 요소
게이트웨이 클래스 구조:
class WCPOS_Gateway_{{GATEWAY_CLASS}} extends WC_Payment_Gateway {
// Gateway configuration
public function __construct() { }
// Admin settings form
public function init_form_fields() { }
// Process payment (main logic goes here)
public function process_payment( $order_id ) { }
// POS-specific methods
public function payment_fields() { }
}
사용자 정의 가이드
기본 구성
-
게이트웨이 정보:
$this->id = 'your_gateway_id';
$this->title = 'Your Gateway Name';
$this->description = 'Gateway description for customers';
$this->method_title = 'Admin title';
$this->method_description = 'Admin description'; -
지원되는 기능:
$this->supports = array(
'products',
'refunds',
'subscriptions', // if applicable
);
결제 처리
핵심 결제 논리는 process_payment() 메서드에 위치합니다:
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
// Your payment processing logic here
// Example: API calls, validation, etc.
if ( $payment_successful ) {
$order->payment_complete();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
} else {
wc_add_notice( 'Payment failed', 'error' );
return array(
'result' => 'failure'
);
}
}
관리자 설정
init_form_fields()에서 관리자 설정을 구성합니다:
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/Disable',
'type' => 'checkbox',
'label' => 'Enable Your Gateway',
'default' => 'yes'
),
'api_key' => array(
'title' => 'API Key',
'type' => 'text',
'description' => 'Enter your API key',
'default' => '',
'desc_tip' => true,
),
// Add more settings as needed
);
}
POS 통합
POS 전용 기능을 구현하려면:
public function payment_fields() {
// Custom payment form for POS
if ( is_admin() && isset( $_GET['page'] ) && $_GET['page'] === 'wc-pos' ) {
// POS-specific payment fields
echo '<div class="pos-payment-fields">';
// Your custom POS interface
echo '</div>';
} else {
// Standard web checkout fields
parent::payment_fields();
}
}
개발 모범 사례
코드 표준
- WordPress 코딩 표준: WordPress PHP 코딩 표준 준수
- WooCommerce 가이드라인: WooCommerce 개발 관행 준수
- 보안: 입력 정리, 데이터 유효성 검사, 논스 사용
- 국제화:
__()및_e()를 사용하여 문자열을 번역 가능하게 만듭니다
오류 처리
// Proper error handling
try {
$result = $this->process_api_call( $data );
if ( is_wp_error( $result ) ) {
throw new Exception( $result->get_error_message() );
}
} catch ( Exception $e ) {
$order->add_order_note( 'Payment failed: ' . $e->getMessage() );
wc_add_notice( 'Payment processing error', 'error' );
return array( 'result' => 'failure' );
}
로깅
// Add logging for debugging
if ( $this->debug ) {
$this->log( 'Payment processing started for order ' . $order_id );
}
private function log( $message ) {
if ( empty( $this->logger ) ) {
$this->logger = wc_get_logger();
}
$this->logger->info( $message, array( 'source' => $this->id ) );
}
게이트웨이 테스트
개발 환경
- 테스트 모드: 항상 테스트/샌드박스 모드 구현
- 디버그 로깅: 문제 해결을 위한 포괄적 로깅 포함
- 오류 시나리오: 다양한 실패 조건 테스트
- POS 테스트: 특히 POS 환경에서 테스트
테스트 케이스
- 성공적인 결제: 주문이 올바르게 완료되는지 확인
- 실패한 결제: 올바른 오류 처리 보장
- 환불: 지원되는 경우 환불 기능 테스트
- 모서리 사례: 다양한 주문 금액 및 구성으로 테스트
배포
플러그인 패키징
- 개발 파일 제거: 테스트 파일 및 개발 도구 정리
- 버전 관리: 플러그인 헤더에서 버전 번호 업데이트
- 문서화: 설치 지침을 포함한 README 포함
- Zip 패키지: 설치 가능한 zip 파일 생성
배포
- GitHub 릴리스: 버전 관리를 위한 GitHub 릴리스 사용
- WordPress 플러그인 디렉토리: WordPress.org에 제출 고려
- 개인 배포: 필요에 따라 자체 서버에 호스팅
고급 기능
웹훅
실시간 결제 업데이트를 위해:
public function handle_webhook() {
$payload = file_get_contents( 'php://input' );
$data = json_decode( $payload, true );
// Verify webhook signature
if ( $this->verify_webhook_signature( $payload ) ) {
$this->process_webhook_data( $data );
}
}
구독 지원
정기 결제를 위해:
// Add subscription support
$this->supports[] = 'subscriptions';
$this->supports[] = 'subscription_cancellation';
$this->supports[] = 'subscription_suspension';
다중 통화
국제 결제를 위해:
public function get_supported_currencies() {
return array( 'USD', 'EUR', 'GBP', 'CAD' );
}
자료
문서화
템플릿 저장소
- GitHub: woocommerce-gateway-template
- 문제: 템플릿 문제를 보고하거나 기능 요청
- 기여: 풀 리퀘스트를 통해 개선 사항 제출
도움 요청
개발 지원을 위해:
- 템플릿 특정 문제에 대해서는 GitHub 저장소 방문
- API 질문에 대해서는 WooCommerce 개발자 문서 확인
- 일반적인 안내를 위해 WooCommerce 개발자 커뮤니티에 가입
예시 게이트웨이
구현 예제를 위한 기존의 맞춤형 게이트웨이를 연구하십시오:
- Stripe 터미널: 하드웨어 통합 예제
- SumUp 터미널: API 기반 터미널 통합
- 이메일 인보이스: 간단한 이메일 기반 게이트웨이
- 웹 체크아웃: 웹 통합 게이트웨이