Web_developer

google 영수증검증 REST v2 class 본문

PHP

google 영수증검증 REST v2 class

에잎이 2022. 7. 8. 20:56
반응형

영수증검증 v2가 나오면서 조금더 명확하게 값을 준다.

기존 서버를 수정

 

<?php

class google_inapp
    {
        private $package_name;
        private $product_id;
        private $purchase_token;
        private $developerPayload;

        private $client;
        private $service;

        public function __construct($package_name,$product_id,$purchase_token,$developerPayload = null)
        {
            if($purchase_token)
            {
                $this->package_name = $package_name;
                $this->product_id = $product_id;
                $this->purchase_token = $purchase_token;
                $this->developerPayload = $developerPayload;

                $this->client = new Google_Client();
                $this->client->setAuthConfig(' Auth json 파일 절대 경로 ');
                $this->client->addScope('https://www.googleapis.com/auth/androidpublisher');
                $this->service = new Google_Service_AndroidPublisher($this->client);
            }
        }

        public function revoke()
        {
            return $this->service->purchases_subscriptions->revoke($this->package_name, $this->product_id, $this->purchase_token);
        }

        public function pay_acknowledge($developerPayload)
        {
            $postBody = new Google_Service_AndroidPublisher_SubscriptionPurchasesAcknowledgeRequest();
            $postBody->setDeveloperPayload($developerPayload);

            $this->service->purchases_subscriptions->acknowledge(
                $this->package_name,
                $this->product_id,
                $this->purchase_token,
                $postBody
            );
        }
        public function certification()
        {
            $get = $this->service->purchases_subscriptionsv2->get($this->package_name, $this->purchase_token);

            if($get->acknowledgementState == "ACKNOWLEDGEMENT_STATE_PENDING")
            {
                $this->pay_acknowledge($this->developerPayload);
            }
            return $get;
        }

    }
?>

1. revoke = 환불

2. pay_ackowledge = 결제승인

3. certification = 영수증 확인

 

사용법은 다음과 같다

<?php

//클라이언트에서 받은 패키지명,상품id,결제토큰을 받은후 셋팅
$google = new google_inapp($package_name,$product_id,$purchase_token,$developerPayload);

// try,catch로 에러 코드 분기처리 
try
{
	//class를 이용하여 영수증 검증 시작
	$google_res = $google->certification();
	if(empty($google_res))
    {
    	throw new Exception();
    }
    else
    {
    	if($google_res->subscriptionState == "SUBSCRIPTION_STATE_ACTIVE" || $google_res->subscriptionState == "SUBSCRIPTION_STATE_CANCELED")
        {
        
        	//영수증 검증후 response 받은 값으로 세팅
            //SUBSCRIPTION_STATE_ACTIVE 결제유효
            //SUBSCRIPTION_STATE_CANCELED 사용자가 취소했지만 만료기간이 남아있음
            
        
        }
    }
        
}
catch (Exception $e)
{
    //실패
    //에러코드 출력
    $msg = $e->getMessage();
    error_log($msg['error']['errors'][0]['reason']);
}

?>

 

시작일,만료일 (Y-m-d H:i:s) 형식으로 출력은 하단에 코드 처럼 변환해서 db에 datetime 형식으로 저장할수있다.

//시작일
date("Y-m-d H:i:s", strtotime($google_res->startTime))

//만료일
date("Y-m-d H:i:s", strtotime($google_res->lineItems[0]->expiryTime))

 

 

 

 

 

 

자세한 내용은 구글api 문서 참조

https://developers.google.com/android-publisher/api-ref/rest/v3/purchases.subscriptionsv2

 

REST Resource: purchases.subscriptionsv2  |  Google Play Developer API  |  Google Developers

REST Resource: purchases.subscriptionsv2 Resource: SubscriptionPurchaseV2 Indicates the status of a user's subscription purchase. JSON representation { "kind": string, "regionCode": string, "latestOrderId": string, "lineItems": [ { object (SubscriptionPurc

developers.google.com

 

Comments