ホームページ  >  記事  >  PHPフレームワーク  >  ThinkPhp5.1 は WeChat 支払いと支払い後のいくつかのステータス説明を行います

ThinkPhp5.1 は WeChat 支払いと支払い後のいくつかのステータス説明を行います

XuPing.Yang
XuPing.Yangオリジナル
2022-02-10 15:06:462142ブラウズ

多くの場合、モールの制作、オンライン支払い、保険支払いなどのプロジェクト開発中に WeChat 支払いインターフェイス呼び出しを使用する必要があります。編集者は最近、いくつかの水道料金請求システム、保証状発行システム、オンライン サービスを構築しました。登録. システムはすべて WeChat 支払いを使用しています. WeChat 支払いの生成と支払い後のいくつかのステータスの説明を以下に示します. 皆様のお役に立てれば幸いです.

1 WeChat Pay

【QRコードをスキャンして支払い】

public function wxPayImg($body,$out_trade_no,$fee,$product_id,$logo,$path,$attach,$pro_id){
    require_once Env::get('app_path')."api/wxpay/lib/WxPay.Api.php";
    //实例化配置信息
    $config = new WxPayConfig();
    $input = new \WxPayUnifiedOrder();
    //设置商品描述
    $input->SetBody($body);
    //设置订单号
    $input->SetOut_trade_no($out_trade_no);
    //设置商品金额(单位:分)
    $input->SetTotal_fee($fee);
    //设置异步通知地址
    $notify = $config->GetNotifyUrl();
    $input->SetNotify_url($notify);
    //设置交易类型
    $input->SetTrade_Type('NATIVE');
    //设置商品ID
    $input->SetProduct_id($product_id);
    $input->SetDevice_info($pro_id);
    $input->SetAttach($attach);
    //调用统一下单API
    $result = \WxPayApi::unifiedOrder($config,$input);
    //dump($result);
    $qr_url = $result['code_url'];
    $img = $this->createCode($qr_url,$logo,$path);
    //生成数组
    $array = array('img'=>$img,'out_trade_no'=>$out_trade_no);
    return $array;
}
//生成二维码
public function createCode($code_url,$logo,$path){
    //创建基本的QR码
    $qrCode = new QrCode($code_url);
    $qrCode->setSize(300);
    //设置高级选项
    $qrCode->setWriterByName('png');
    $qrCode->setEncoding('UTF-8');
    $qrCode->setErrorCorrectionLevel(ErrorCorrectionLevel::HIGH());
    $qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]);
    $qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]);
    //$qrCode->setLabel('', 16, App::getAppPath().'/../public/static/user/font/msyhl.ttc', LabelAlignment::CENTER());
    $qrCode->setLogoPath($logo);
    $qrCode->setLogoWidth(50);
    $qrCode->setValidateResult(false);
    //边距设置
    $qrCode->setMargin(10);
    //直接输出二维码
    header('Content-Type: '.$qrCode->getContentType());
    //echo $qrCode->writeString();
    //将二维码保存到指定目录
    $qrCode->writeFile($path);
    //生成数据URI以内联图像数据(即在<img>标记内)
    $dataUri = $qrCode->writeDataUri();
    return $dataUri;
}

【JSAPIインターフェース決済】

public function jsApiPay($openId,$Body,$out_trade_no,$fee,$product_id,$attach){
    require_once Env::get(&#39;app_path&#39;)."api/wxpay/lib/WxPay.Api.php";
    $tools = new JsApiPay();
    $input = new \WxPayUnifiedOrder();
    //设置商品描述
    $input->SetBody($Body);
    //设置订单号
    $input->SetOut_trade_no($out_trade_no);
    //设置商品金额(单位:分)
    $input->SetTotal_fee($fee);
    //设置异步通知地址
    $config = new WxPayConfig();
    $notify = $config->GetNotifyUrl();
    $input->SetNotify_url($notify);
    //设置交易类型
    $input->SetTrade_Type(&#39;JSAPI&#39;);
    //设置商品ID
    $input->SetProduct_id($product_id);
    //用户openID
    $input->SetOpenid($openId);
    $input->SetAttach($attach);
    //调用统一下单API
    $result = \WxPayApi::unifiedOrder($config,$input);
    $jsApiParameters = $tools->GetJsApiParameters($result);
    return $jsApiParameters;
}

【注文問い合わせ】

public function QueryOrder($number,$transaction_id=NULL){
    require_once App::getAppPath().&#39;api/wxpay/lib/WxPay.Api.php&#39;;
    require_once App::getAppPath().&#39;api/wxpay/lib/WxPay.Notify.php&#39;;
    $input = new \WxPayOrderQuery();
    $input->SetOut_trade_no($number);
    $input->SetTransaction_id($transaction_id);
    $config = new WxPayConfig();
    $result = \WxPayApi::orderQuery($config, $input);
    if($result[&#39;result_code&#39;]==&#39;SUCCESS&#39;){
        if($result[&#39;trade_state&#39;]==&#39;SUCCESS&#39;){
            $arr = json_decode($result[&#39;attach&#39;],true);
            $pay_time = $this->getPayTime($result[&#39;time_end&#39;]);
            return [&#39;code&#39;=>100,&#39;result_code&#39;=>$result[&#39;result_code&#39;],&#39;attach&#39;=>$arr,&#39;pay_time&#39;=>$pay_time,&#39;result&#39;=>$result,&#39;msg&#39;=>"支付成功"];
        }else{
            return [&#39;code&#39;=>101,&#39;result_code&#39;=>$result[&#39;result_code&#39;],&#39;err_code&#39;=>$result[&#39;err_code&#39;],&#39;result&#39;=>$result,&#39;msg&#39;=>"订单未支付"];
        }
    }else{
        return [&#39;code&#39;=>103,&#39;result_code&#39;=>$result[&#39;result_code&#39;],&#39;result&#39;=>$result,&#39;msg&#39;=>"订单不存在"];
    }
    //dump($result);
    //return $result;
}

2 WeChat 決済時の添付パラメータの設定

attach、公式説明: 追加データはクエリ API と支払い通知でそのまま返され、カスタムとして使用できます実際の状況 このフィールドは、支払いが完了した場合にのみ返されます。開発中に注文をクエリするときに、メンバー ID、支払い注文 ID、アプリケーション ID、メンバー名などの特定の値を返す必要がある場合があります。現時点では、attach を使用してパラメーターを渡すことができますが、attach は渡される値は配列であることが多いため、変換には json_encode を使用するだけで済みます (例:

$remarks = array(
    &#39;cid&#39;=>1,
    &#39;member_id&#39;=>2,
    &#39;apply_id&#39;=>28,
);
$attach = json_encode($remarks);

) 注文をクエリした後、変換には json_decode を使用します (例:
)

$arr = json_decode($result[&#39;attach&#39;],true);

ここで取得した $arr は配列であり、配列の呼び出し規則に従って直接呼び出すことができます。

3 WeChat 支払いのいくつかのステータス

照会した注文情報が存在しない場合の戻り値は次のとおりです。

array(9) {
  ["appid"] => string(18) "公众号APPId"
  ["err_code"] => string(13) "ORDERNOTEXIST"
  ["err_code_des"] => string(15) "订单不存在"
  ["mch_id"] => string(10) "商户ID"
  ["nonce_str"] => string(16) "H0K6KPemexExM5DV"
  ["result_code"] => string(4) "FAIL"
  ["return_code"] => string(7) "SUCCESS"
  ["return_msg"] => string(2) "OK"
  ["sign"] => string(64) "8779CA8C7F4931B4296C19FFFB176A3111F74B7244123A0C0EB7AD8DB2B1BDA49DA"
}

照会した注文情報の決済が失敗した場合の戻り値は次のとおりです。

array(11) {
  ["appid"] => string(18) "公众号APPId"
  ["attach"] => string(13) "你的传参值"
  ["mch_id"] => string(10) "商户ID"
  ["nonce_str"] => string(16) "BR3zfazCdI3vZj5e"
  ["out_trade_no"] => string(13) "1636555204840"
  ["result_code"] => string(7) "SUCCESS"
  ["return_code"] => string(7) "SUCCESS"
  ["return_msg"] => string(2) "OK"
  ["sign"] => string(64) "7927EC724A7FDBFF034621B1EC492DB4D130AC13A43E4C66C7B6AD7889736CD5"
  ["trade_state"] => string(6) "NOTPAY"
  ["trade_state_desc"] => string(15) "订单未支付"
}

照会した注文情報が存在しない場合の戻り値は次のとおりです。情報が存在する場合、戻り値は次のとおりです。

array(21) {
  ["appid"] => string(18) "公众号APPId"
  ["attach"] => string(13) "你的传参值"
  ["bank_type"] => string(10) "LZB_CREDIT"
  ["cash_fee"] => string(4) "2000"
  ["cash_fee_type"] => string(3) "CNY"
  ["fee_type"] => string(3) "CNY"
  ["is_subscribe"] => string(1) "Y"
  ["mch_id"] => string(10) "商户ID"
  ["nonce_str"] => string(16) "Y8iYqXqHfs22hexX"
  ["openid"] => string(28) "支付者微信openid"
  ["out_trade_no"] => string(13) "1636600772812"
  ["result_code"] => string(7) "SUCCESS"
  ["return_code"] => string(7) "SUCCESS"
  ["return_msg"] => string(2) "OK"
  ["sign"] => string(64) "7AC36F5EA6C4EE5D33584F0F1CDB54F804F0B196B49B61A4FFB6C045D521DA3C"
  ["time_end"] => string(14) "20211111111938"
  ["total_fee"] => string(4) "2000"
  ["trade_state"] => string(7) "SUCCESS"
  ["trade_state_desc"] => string(12) "支付成功"
  ["trade_type"] => string(5) "JSAPI"
  ["transaction_id"] => string(28) "4200001198202111115284536175"
}

ネイティブ支払いが成功しました

array(22) {
  ["appid"] => string(18) "公众号APPId"
  ["attach"] => string(13) "你的传参值"
  ["bank_type"] => string(6) "OTHERS"
  ["cash_fee"] => string(1) "1"
  ["cash_fee_type"] => string(3) "CNY"
  ["device_info"] => string(1) "1"
  ["fee_type"] => string(3) "CNY"
  ["is_subscribe"] => string(1) "Y"
  ["mch_id"] => string(10) "商户ID"
  ["nonce_str"] => string(16) "Y8iYqXqHfs22hexX"
  ["openid"] => string(28) "支付者微信openid"
  ["out_trade_no"] => string(13) "1642682408295"
  ["result_code"] => string(7) "SUCCESS"
  ["return_code"] => string(7) "SUCCESS"
  ["return_msg"] => string(2) "OK"
  ["sign"] => string(64) "2F25084A568BBDA805DA8EE3FEB846448C9778DCBC2B745E8210D950E0742E38"
  ["time_end"] => string(14) "20220120204024"
  ["total_fee"] => string(1) "1"
  ["trade_state"] => string(7) "SUCCESS"
  ["trade_state_desc"] => string(12) "支付成功"
  ["trade_type"] => string(6) "NATIVE"
  ["transaction_id"] => string(28) "4200001358202201201811257221"
}

関連する推奨事項:「thinkphp ビデオ チュートリアル #」 ##"

上記は、WeChat 支払いに関する関連知識を編集者がまとめたものです。すべての開発者にとって役立つことを願っています。


以上がThinkPhp5.1 は WeChat 支払いと支払い後のいくつかのステータス説明を行いますの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。