首页 > 后端 > 正文

官方给的调用支付示例如下:

$response = $app->getClient()->postJson('v3/pay/transactions/native', [
    'mchid' => (string)$app->getMerchant()->getMerchantId(),
    'out_trade_no' => 'native20210720xxx',
    'appid' => 'wxe2fb06xxxxxxxxxx6',
    'description' => 'Image形象店-深圳腾大-QQ公仔',
    'notify_url' => 'https://weixin.qq.com/',
    'amount' => [
        'total' => 1,
        'currency' => 'CNY',
    ]
]);

print_r($response->toArray(false));

但是如果不小心参数传错了就会返回400错误或其他500错误,看不出来正确的错误信息 那么应该如何处理呢?

只要加上try catch 捕获异常就行,具体代码如下:

 try {
          
           $response = $app->getClient()->postJson('v3/pay/transactions/native', [
           'mchid' => (string)$app->getMerchant()->getMerchantId(),
           'out_trade_no' => 'native20210720xxx',
           'appid' => 'wxe2fb06xxxxxxxxxx6',
           'description' => 'Image形象店-深圳腾大-QQ公仔',
           'notify_url' => 'https://weixin.qq.com/',
           'amount' => [
             'total' => 1,
             'currency' => 'CNY',
           ]
         ]);

        } catch (ServerException $ex) {
            $content = json_decode($ex->getResponse()->getContent(false), true);
            throw new Execption($content['message']);
        } catch (ClientException $ex) {
            $content = json_decode($ex->getResponse()->getContent(false), true);
            throw new Execption($content['message']);
        } catch (Throwable $ex) {
            throw new Execption($ex->getMessage());
        }

RoveCoder版权所有,转载请注明

猜你喜欢
picture loss