Shopify WebHook对接,PHP支付订单验证,PHP后端验证签名,Laravel 框架

8 篇文章 0 订阅

配置 Shopify WebHook

登录商店后台配置 -> 选择 设置
shopify 选择设置
找到通知 选中后下滚动到底部 WebHook 配置位置
通知配置

选择 创建Webhook 根据业务需求选择事件并填写 URL
创建Webhook
这里选择了 订单创建 因为需要记录订单, 订单付款 可以根据这个状态确认用户支付过了, 从而进行发货操作
Webhook

Shopify Webhook接收支付通知处理

	/**
     * shopify webhook接收支付通知
     * @param Request $request
     * @return JsonResponse
     * @throws Exception
     */
    public function shopifyWebhookNotify(Request $request): JsonResponse
    {
        // shopify 关键参数
        $shopifyFields = [
            'HTTP_X_SHOPIFY_WEBHOOK_ID',
            'HTTP_X_SHOPIFY_TRIGGERED_AT',
            'HTTP_X_SHOPIFY_TOPIC',
            'HTTP_X_SHOPIFY_TEST',
            'HTTP_X_SHOPIFY_SHOP_DOMAIN',
            'HTTP_X_SHOPIFY_HMAC_SHA256',
            'HTTP_X_SHOPIFY_API_VERSION',
            'HTTP_CONTENT_TYPE',
            'HTTP_USER_AGENT',
        ];
        // 记录全部请求参数
        $params = $request->input();
        // 使用集合函数过滤不需要的参数
        $server = collect($_SERVER)->only($shopifyFields)->all();
        $this->log('shopify === shopifyWebhookNotify ===');
        $this->log('shopify Server->' . json_encode($server, JSON_UNESCAPED_UNICODE));
        $this->log('shopify Params->' . json_encode($params, JSON_UNESCAPED_UNICODE));
        // 验证请求(shopify 官方文档写法)
        $hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'] ?? '';
        $data = file_get_contents('php://input');
        $verified = $this->shopifyWebhookVerify($data, $hmac_header);
        $result = [
            "code" => 401,
            "message" => 'No Access Rights',
        ];
        if ($verified) {
            $this->log('shopify-> 验证成功');
            // 创建订单
            if ($server['HTTP_X_SHOPIFY_TOPIC'] === 'orders/create') {
                return $this->shopifyOrderCreate($params, $result);
            }
            // 支付订单
            if ($server['HTTP_X_SHOPIFY_TOPIC'] === 'orders/paid') {
                return $this->shopifyOrderPaid($params, $result);
            }
            return response()->json($result);
        } else {
            return response()->json($result, 401);
        }
    }
    
	/**
     * shopify-订单-创建
     * @param $params
     * @param $result
     * @return JsonResponse
     * @throws Exception
     */
    public function shopifyOrderCreate($params, $result): JsonResponse
    {
    	// 订单创建逻辑
		if('判断订单号是否存在'){
		  // 订单已存在
          $result["message"] = 'Order already exists';
          $this->log('shopify->创建失败->订单已存在');
          return response()->json($result);
		}
		
		// 创建订单
		if('创建订单'){
			$result = [
			    "code" => 0,
			    "message" => "success",
			];
			$this->log('shopify->创建成功');
		} else {
		  	$this->log('shopify->创建失败->用户信息不存在');
		  	$result["message"] = 'User Info Does Not Exist'; // 用户信息不存在
		}
      	return response()->json($result);
	}

    /**
     * shopify webhook 验证
     * @param $data
     * @param $hmac_header
     * @return bool
     */
    public function shopifyWebhookVerify($data, $hmac_header): bool
    {
        // 客户秘钥
        $clientSecret = 'xxxxx';
        $calculated_hmac = base64_encode(hash_hmac('sha256', $data, $clientSecret, true));
        return hash_equals($calculated_hmac, $hmac_header);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是使用 Node.js 创建 Shopify Webhook 的步骤: 1. 首先,你需要在 Shopify 后台创建一个 Webhook,具体步骤可以参考上面的教程。 2. 接下来,使用 Node.js 创建一个 HTTP 服务器,用于接收 Shopify Webhook 发送的数据。你可以使用 Express 框架来创建 HTTP 服务器,比如: ```javascript const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.json()); app.post('/webhook', (req, res) => { // 处理 Shopify Webhook 发送的数据 console.log(req.body); res.send('Webhook received!'); }); app.listen(3000, () => { console.log('Server listening on port 3000!'); }); ``` 其中,`/webhook` 路径对应着你在 Shopify 后台创建的 Webhook URL。 3. 在 HTTP 服务器启动后,你需要向 Shopify 注册 Webhook,让 Shopify 在特定事件触发时向你的 HTTP 服务器发送数据。你可以使用 Shopify API 来注册 Webhook,比如: ```javascript const Shopify = require('shopify-api-node'); const shopify = new Shopify({ shopName: 'your-shop-name', apiKey: 'your-api-key', password: 'your-api-password' }); shopify.webhook.create({ topic: 'orders/create', address: 'https://your-server-url/webhook', format: 'json' }) .then((webhook) => { console.log(webhook); }) .catch((err) => { console.log(err); }); ``` 其中,`topic` 表示你要监听的事件类型,`address` 表示你的 HTTP 服务器地址,`format` 表示数据格式。 4. 注册成功后,你可以在 Shopify 后台的 Webhooks 页面中查看你创建的 Webhook。 现在,当 Shopify 上发生了订单创建事件时,你的 HTTP 服务器就会收到 Shopify Webhook 发送的数据。你可以在 HTTP 服务器中处理这些数据,比如存储到数据库中、发送通知等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值