收藏
回答

调用【创建代金券批次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();

    }


}


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

3 个回答

  • 支付社区运营
    支付社区运营
    2021-03-15

    {

        "stock_name": "GD测批次4",

        "comment": "验证活动",

        "belong_merchant": "xxxxxx",

        "available_begin_time": "2020-02-13T18:00:00.120+08:00",

        "available_end_time": "2020-02-20T23:59:59.120+08:00",

        "stock_use_rule": {

            "max_coupons": 10,

            "max_amount": 100,

            "max_coupons_per_user": 10,

            "natural_person_limit": false,

            "prevent_api_abuse": false

        },

        "coupon_use_rule": {

            "fixed_normal_coupon": {

                "coupon_amount": 10,

                "transaction_minimum": 10

            },

            "available_merchants": [

                "209784532",

                "221003827"

            ]

        },

        "no_cash": false,

        "stock_type": "NORMAL",

        "out_request_no": "207662xxxxxx"

    }


    按照这个示列传值,并注意:

    1.stock_name:最多可填写9个字

    2.max_coupons_per_user:单天发放个数上限不能为0

    3. coupon_amount:10<=coupon_amount<=100000

    4.available_time_after_receive:可用时间:相对时间,按分钟设置,是否1min<=分钟范围<=1440min

    5.transaction_minimum校验规则:

    a、使用门槛-券面额>=0.01(门槛要大于面额)

    b、0.1元<=门槛<=100000

    6.stock_type:目前只支持NORMAL

    7.out_request_no:校验规则:不可以重复

    8.开始时间结束时间控制在90天内

    请按照示列跟注意事项进行参考一下

    2021-03-15
    有用 1
    回复
  • Memory
    Memory
    2021-03-15

    belong_merchant 该字段暂未开放

    2021-03-15
    有用
    回复
  • peng
    peng
    2021-03-15

    券金额小于1元(coupon_amount)要申请吧

    2021-03-15
    有用
    回复 3
    • Memory
      Memory
      2021-03-15
      他是传参了未开放字段
      2021-03-15
      回复
    • 阿白
      阿白
      2021-03-15回复Memory
      哪个字段是未开放字段
      2021-03-15
      回复
    • 阿白
      阿白
      2021-03-15
      刚才看了一下券金额小于10分(coupon_amount)需要申请
      2021-03-15
      回复
登录 后发表内容
问题标签