Skip to content

Commit

Permalink
[GXM-91] feat: 네이버페이 주문형 배송지 변경 대응
Browse files Browse the repository at this point in the history
  • Loading branch information
kdino committed May 31, 2023
1 parent efa188e commit 88d1912
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 4 deletions.
20 changes: 17 additions & 3 deletions IamportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: 우커머스용 아임포트 플러그인(국내 모든 PG를 한 번에)
* Plugin URI: http://www.iamport.kr
* Description: 우커머스용 한국PG 연동 플러그인 ( 신용카드 / 실시간계좌이체 / 가상계좌 / 휴대폰소액결제 - 에스크로포함 )
* Version: 2.2.35
* Version: 2.2.36
* Author: PortOne
* Author URI: https://portone.io/
*
Expand Down Expand Up @@ -100,6 +100,7 @@ function init_iamport_plugin() {

$label_refund = IamportHelper::display_label(IamportHelper::STATUS_REFUND);
$label_exchange = IamportHelper::display_label(IamportHelper::STATUS_EXCHANGE);
$label_address_changed = IamportHelper::DEFAULT_STATUS_ADDRESS_CHANGED;

register_post_status( 'wc-refund-request', array(
'label' => __( "{$label_refund}", 'iamport-for-woocommerce' ),
Expand All @@ -119,6 +120,15 @@ function init_iamport_plugin() {
'label_count' => _n_noop( "{$label_exchange} <span class=\"count\">(%s)</span>", "{$label_exchange} <span class=\"count\">(%s)</span>" )
) );

register_post_status( 'wc-address-changed', array(
'label' => __( "{$label_address_changed}", 'iamport-for-woocommerce' ),
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( "{$label_address_changed} <span class=\"count\">(%s)</span>", "{$label_address_changed} <span class=\"count\">(%s)</span>" )
) );


//상품 제공기간
add_action( 'woocommerce_product_options_advanced', 'iamport_advanced_meta' );
Expand All @@ -145,9 +155,11 @@ function add_cancel_actions_to_order_statuses( $order_statuses ) {
if ( 'wc-cancelled' === $key ) {
$label_refund = IamportHelper::display_label(IamportHelper::STATUS_REFUND);
$label_exchange = IamportHelper::display_label(IamportHelper::STATUS_EXCHANGE);
$label_address_changed = IamportHelper::DEFAULT_STATUS_ADDRESS_CHANGED;

$new_order_statuses['wc-refund-request'] = __( "{$label_refund}", 'iamport-for-woocommerce' );
$new_order_statuses['wc-exchange-request'] = __( "{$label_exchange}", 'iamport-for-woocommerce' );
$new_order_statuses['wc-address-changed'] = __( "{$label_address_changed}", 'iamport-for-woocommerce' );
}
}

Expand Down Expand Up @@ -873,6 +885,7 @@ public function __construct() {

abstract protected function get_gateway_id();
abstract public function iamport_order_detail( $order_id );
public function update_shipping_info($order, $payment_data) {}

public function init_form_fields() {
//iamport기본 플러그인에 해당 정보가 세팅되어있는지 먼저 확인
Expand Down Expand Up @@ -986,6 +999,9 @@ public function check_payment_response() {
$this->_iamport_post_meta($order_id, '_iamport_paymethod', $payment_data->pay_method);
$this->_iamport_post_meta($order_id, '_iamport_pg_tid', $payment_data->pg_tid);
$this->_iamport_post_meta($order_id, '_iamport_receipt_url', $payment_data->receipt_url);

$order = new WC_Order( $order_id );
$gateway->update_shipping_info($order, $payment_data);

if ( $payment_data->status === 'paid' ) {
$loggers[] = "B:paid";
Expand All @@ -995,8 +1011,6 @@ public function check_payment_response() {
//lock the row
$synced_row = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}posts WHERE ID = {$order_id} FOR UPDATE");

$order = new WC_Order( $order_id ); //lock잡은 후 호출(2017-01-16 : 의미없음. [1.6.8] synced_row의 값을 활용해서 status체크해야 함)

if ( $gateway->is_paid_confirmed($order, $payment_data) ) {
$loggers[] = "C:confirm";

Expand Down
60 changes: 60 additions & 0 deletions iamport-naverpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,66 @@ public function is_paid_confirmed($order, $payment_data) {
return $payment_data->status === 'paid'; //이미 paid인 건에 대해서만 is_paid_confirmed가 호출되기는 하지만 한 번 더 체크
}

public function update_shipping_info($order, $payment_data) {
try {
// 네이버페이 상품주문정보 조회
$impUid = $payment_data->imp_uid;

$iamport = new WooIamport($this->imp_rest_key, $this->imp_rest_secret);
$result = $iamport->getNaverProductOrders($impUid);

if ( $result->success ) {
$productOrders = $result->data;

$shippingNotes = array();
foreach ($productOrders as $idx=>$po) {
//product line item
$productLineItem = IamportHelper::findProductItem($order, $po->product_id, self::getVariationIdFromQuery($po->product_option_id), self::getAttributesFromQuery($po->product_option_id));
if ($productLineItem) {
$productLineItem->add_meta_data('naver_product_order_id', $po->product_order_id);
$productLineItem->add_meta_data('naver_product_order_status', $po->product_order_status);
$productLineItem->add_meta_data('product_amount', $po->product_amount);
$productLineItem->add_meta_data('delivery_amount', $po->delivery_amount);
$productLineItem->add_meta_data('shipping_memo', $po->shipping_memo ? $po->shipping_memo : '없음');
$productLineItem->add_meta_data('shipping_due', $po->shipping_due ? date('Y-m-d H:i:s', $po->shipping_due + get_option('gmt_offset')*HOUR_IN_SECONDS) : '없음');
$productLineItem->save_meta_data();
}

$shippingNotes[] = sprintf( __( '[상품명 : %s] 배송요청사항 : %s (배송기한 : %s)', 'iamport-for-woocommerce' ),
$po->product_name,
$po->shipping_memo ? $po->shipping_memo : "없음",
$po->shipping_due ? date('Y-m-d H:i:s', $po->shipping_due + get_option('gmt_offset')*HOUR_IN_SECONDS) : "없음");

if ( $idx == 0 ) { //첫번째 상품정보에서 orderer / shipping 정보 추출
if(!empty($order->get_shipping_address_1()) && ($order->get_shipping_address_1() != $po->shipping_address->base)) {
$order->add_order_note("네이버페이 배송지가 변경되었습니다.");
$order->update_status('address-changed');
}
$order->set_billing_first_name( $po->orderer->name );
$order->set_billing_email( $po->orderer->id . "@naver.com" );
$order->set_billing_phone( $po->orderer->tel );

$order->set_shipping_first_name( $po->shipping_address->name );
$order->add_meta_data( "_shipping_phone1", $po->shipping_address->tel1, true ); //구리지만 어쩔 수 없음
$order->add_meta_data( "_shipping_phone2", $po->shipping_address->tel2, true ); //구리지만 어쩔 수 없음
$order->set_shipping_address_1( $po->shipping_address->base );
$po->shipping_address->detail ? $order->set_shipping_address_2( $po->shipping_address->detail ) : $order->set_shipping_address_2("");
$order->set_shipping_postcode( $po->shipping_address->postcode );
}
}

$orderComment = implode(",\n", $shippingNotes);
$order->set_customer_note( $orderComment );
} else {
$order->add_order_note( '[네이버페이-상세조회실패] ' . $result->error['message'] );
error_log('[네이버페이-상세조회실패] ' . $result->error['message']);
}
} catch (Exception $e) {
$order->add_order_note( '[네이버페이-상세조회실패] ' . $e->getMessage() );
error_log($e);
}
}

public function update_shipping_amount($order, $payment_data) {
$shipping_amount = $payment_data->amount - $order->get_total();

Expand Down
1 change: 1 addition & 0 deletions lib/IamportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class IamportHelper
const DEFAULT_STATUS_REFUND = '반품요청';
const DEFAULT_STATUS_EXCHANGE = '교환요청';
const DEFAULT_STATUS_AWAITING_VBANK = '가상계좌 입금대기 중';
const DEFAULT_STATUS_ADDRESS_CHANGED = '배송지 변경';

public static function get_customer_uid($order)
{
Expand Down
5 changes: 4 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: http://www.iamport.kr
Tags: 네이버페이, 카카오페이, 페이코, 이니시스, KCP, naverpay, naver, payco, kakao, kakaopay, kpay, inicis, 유플러스, lguplus, uplus, 나이스, 나이스페이, nice, nicepay, 제이티넷, 티페이, jtnet, tpay, 다날, danal, 모빌리언스, mobilians, 정기결제, subscription, 해외카드, visa, master, jcb, shopping, mall, iamport
Requires at least: 3.5
Tested up to: 5.5.1
Stable tag: 2.2.35
Stable tag: 2.2.36
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html

Expand Down Expand Up @@ -60,6 +60,9 @@ https://www.facebook.com/iamportservice


== Changelog ==
= 2.2.36 =
* 네이버페이 주문형 결제 후 배송지 변경 시, 주문 건에 반영되도록 기능 추가

= 2.2.35 =
* 엑심베이에 몰페이 결제수단 추가

Expand Down

0 comments on commit 88d1912

Please sign in to comment.