收藏
回答

调用【创建代金券批次API】,返回NO_AUTH

商户号:1518669531

wechatpay-guzzle-middleware调用创建代金券api接口(https://api.mch.weixin.qq.com/v3/marketing/favor/coupon-stocks),时返回报错:

Client error: `POST https://api.mch.weixin.qq.com/v3/marketing/favor/coupon-stocks` resulted in a `403 Forbidden` response: {"code":"NO_AUTH","message":"你配置的信息需要开通特殊权限"} 403 Forbidden {"code":"NO_AUTH","message":"你配置的信息需要开通特殊权限"}

请求参数:

array(9) {

  ["stock_name"] => string(6) "测试"

  ["belong_merchant"] => string(10) "1518669531"

  ["available_begin_time"] => string(25) "2021-03-16T16:43:16+08:00"

  ["available_end_time"] => string(25) "2021-04-08T16:43:16+08:00"

  ["stock_use_rule"] => array(5) {

    ["max_amount"] => int(10)

    ["max_coupons"] => int(10)

    ["max_coupons_per_user"] => int(2)

    ["natural_person_limit"] => bool(true)

    ["prevent_api_abuse"] => bool(true)

  }

  ["coupon_use_rule"] => array(2) {

    ["available_merchants"] => array(1) {

      [0] => string(10) "1518669531"

    }

    ["fixed_normal_coupon"] => array(2) {

      ["coupon_amount"] => int(1)

      ["transaction_minimum"] => int(100)

    }

  }

  ["no_cash"] => bool(true)

  ["stock_type"] => string(6) "NORMAL"

  ["out_request_no"] => string(24) "1518669531_20210315_9465"

}

请求代码:


public function test()

{


    $gzh_id = config('database.gzh_id');

    $xcx_id = config('database.xcx_id');


    // 商户相关配置

    $merchantId = config('database.mch_id');

    $merchantSerialNumber = config('database.mch_ser_num');

    $merchantPrivateKey = PemUtil::loadPrivateKey(ADDON_PATH.DS.'wxcard'.DS.'public'.DS.'cert1'.DS.'apiclient_key.pem'); // 商户私钥

    // 微信支付平台配置

    $wechatpayCertificate = PemUtil::loadCertificate(ADDON_PATH.DS.'wxcard'.DS.'public'.DS.'cert1'.DS.'apiclient_cert.pem'); // 微信支付平台证书


    // 构造一个WechatPayMiddleware

    $wechatpayMiddleware = WechatPayMiddleware::builder()

        ->withMerchant($merchantId, $merchantSerialNumber, $merchantPrivateKey) // 传入商户相关配置

        ->withWechatPay([ $wechatpayCertificate ]) // 可传入多个微信支付平台证书,参数类型为array

        ->build();


    // 将WechatPayMiddleware添加到Guzzle的HandlerStack中

    $stack = GuzzleHttp\HandlerStack::create();

    $stack->push($wechatpayMiddleware, 'wechatpay');


    // 创建Guzzle HTTP Client时,将HandlerStack传入

    $client = new GuzzleHttp\Client(['handler' => $stack]);


    $json = [

        'stock_name' => '测试',

        'belong_merchant' => $merchantId,

        'available_begin_time' => date(DATE_RFC3339, time()+(24*3600)),

        'available_end_time' => date(DATE_RFC3339, time()+(24*24*3600)),

        'stock_use_rule' => [

            'max_amount' => 10,

            'max_coupons' => 10,

            'max_coupons_per_user' => 2,

            'natural_person_limit' => true,

            'prevent_api_abuse' => true,

        ],

        'coupon_use_rule' => [

            'available_merchants' => [

                $merchantId,

            ],

            'fixed_normal_coupon' => [

                'coupon_amount' => 1,

                'transaction_minimum' => 100,

            ],

        ],

        'no_cash' => true,

        'stock_type' => 'NORMAL',

        'out_request_no' => $merchantId.'_'.date('Ymd').'_'.Random::numeric(4),

    ];


    dump($json);



    // 接下来,正常使用Guzzle发起API请求,WechatPayMiddleware会自动地处理签名和验签

    try {

        $resp = $client->request('POST', 'https://api.mch.weixin.qq.com/v3/marketing/favor/coupon-stocks', [ // 注意替换为实际URL

            // JSON请求体

            'json' => $json,

            'headers' => [

                'Accept' => 'application/json',

                'Content-Type' => 'application/json',

            ],

        ]);


        echo $resp->getStatusCode().' '.$resp->getReasonPhrase()."\n";

        echo $resp->getBody()."\n";

        // return $resp->getBody();

    } catch (RequestException $e) {

        // // 进行错误处理

        echo $e->getMessage()."\n";

        if ($e->hasResponse()) {

            echo $e->getResponse()->getStatusCode().' '.$e->getResponse()->getReasonPhrase()."\n";

            echo $e->getResponse()->getBody();

        }

        return;

        // return $e->getResponse()->getBody();

    }


}


回答关注问题邀请回答
收藏

1 个回答

登录 后发表内容
问题标签