wordpress 如何对接沃尔玛卖家api流程

进入沃尔玛卖家的后台,进入开发者中心,在底部有个人的key

这是沃尔玛的api文档

https://developer.walmart.com/api/us/mp/auth#operation/tokenAPI

打开WP后台,到外观-》主题文件编辑器-》打开function.php文件-》拉到最底部,写入代码

1.设置API

/**
 * 获取沃尔玛的API的密钥
 * ***/
function getWalmartDetail()
{
$data = [];
$data['client_id'] = "沃尔玛的client_id";
$data['client_secret'] = "沃尔玛的client_secret";
return $data;
}

2.获取密钥的tonken

/**
 * 获取沃尔玛密钥的token
 * ***/
function get_walmart_access_token() {
	$data = getWalmartDetail();
	$client_id = $data['client_id'];
	$client_secret = $data['client_secret'];
	$url = "https://marketplace.walmartapis.com/v3/token";
	$uniqid = uniqid();
	$authorization_key = base64_encode($client_id.":".$client_secret);
	$code = "";

	$ch = curl_init();
	$options = array(
		CURLOPT_URL => $url,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_TIMEOUT => 60,
		CURLOPT_HEADER => false,
		CURLOPT_POST => 1,
		CURLOPT_POSTFIELDS => "grant_type=client_credentials",//个人对接默认类型
		CURLOPT_HTTPHEADER => array(
			"WM_SVC.NAME: Walmart Marketplace",//个人对接默认名称
			"WM_QOS.CORRELATION_ID: $uniqid",
			"Authorization: Basic $authorization_key",
			"Accept: application/json",
			"Content-Type: application/x-www-form-urlencoded",
		),
	);
	curl_setopt_array($ch,$options);
	$response = curl_exec($ch);
	$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
	curl_close($ch);

	if($code == 201 || $code == 200)
	{
		$token = json_decode($response,true);
		return $token['access_token'];
	}

}

3.请求API,返回订单信息

/**
 * 请求沃尔玛的店铺获取订单信息
 * $endpoint 订单号
 * $method 请求方式
 * $body null
 * 
 * ***/
function walmart_api_request($endpoint, $method = 'GET', $body = null) {
    $access_token = get_walmart_access_token();

    $headers = array(
        'WM_SEC.ACCESS_TOKEN' => $access_token,
        'WM_SVC.NAME' => 'Walmart Marketplace',
        'WM_QOS.CORRELATION_ID' => uniqid(),
        'Content-Type' => 'application/x-www-form-urlencoded',
        'Accept' => 'application/json',
        'WM_CONSUMER.CHANNEL.TYPE' => '',  // 没有就为空
    );

    $url = 'https://marketplace.walmartapis.com/v3/orders/' . $endpoint;//订单url

    $args = array(
        'method' => $method,
        'headers' => $headers,
        'body' => $body ? json_encode($body) : null,
        'timeout' => 60,
    );

    $response = wp_remote_request($url, $args);

    if (is_wp_error($response)) {
        return $response->get_error_message();
    }

    $data = json_decode(wp_remote_retrieve_body($response));
	if($data->order->purchaseOrderId){
		return $data->order->customerEmailId;
	}else{
		return false;
	}
}

其实沃尔玛的对接到这里就已经结束了,下面我个人使用这个API的方式,大家可以参考

4.生成前端访问的路由和处理表单提交和返回APi结果到前端展示。

/**
 * 生成前端请求沃尔玛的api路由
 * 
 * ***/
function warranty_custom_api() {
    register_rest_route('warranty/v1', '/check_order/', array(
        'methods' => 'POST',
        'callback' => 'get_walmart_orders',
    ));
}
add_action('rest_api_init', 'warranty_custom_api');

/**
 * 请求沃尔玛的表单提交
 * 
 * ***/
function get_walmart_orders(WP_REST_Request $request) {
    $order_id = sanitize_text_field($request->get_param('order_id'));

    // 调用你的订单检查逻辑
    $result = walmart_api_request($order_id);

    if ($result) {
        return new WP_REST_Response(['message' => 'Order verification passed', 'data' => $result], 200);
    } else {
        return new WP_REST_Response(['message' => 'The order number does not exist on Walmart'], 404);
    }
}

5.前端调用API和表单提交

<style>
/* 初始禁用提交按钮的样式 */
#gform_submit_button_2 {
    pointer-events: none; /* 禁用点击事件 */
    opacity: 0.5; /* 调整按钮透明度以显示为禁用状态 */
}
</style>
<script>
jQuery(document).ready(function($) {
    $('#input_2_5').on('blur', function() {
        var order_id = $(this).val();
        if (order_id === '') {
            return; // Stop further execution
        }
        $.ajax({
            url: '/wp-json/warranty/v1/check_order/',
            type: 'POST',
            data: {
                order_id: order_id
            },
            success: function(response) {
                if(response.message === "Order verification passed") {
                    // Enable submit button
                    $('#gform_submit_button_2').css('pointer-events', 'auto').css('opacity', '1');
                } else {
                    // Keep submit button disabled and alert the user
                    alert(response.message);
                }
            },
            error: function(response) {
                alert(response.responseJSON.message);
            }
        });
    });
});
</script>

OK,沃尔玛对接到此结束!!!

相关文章

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论