微信小プログラムのwx.requestPaymentが微信決済を開始

59950 ワード

wx.requestPaymentが微信決済を開始
timeStampタイムスタンプ
noncesStrランダム文字列
Package統合単一インタフェースから返されるprepay_idパラメータ値
SignType署名アルゴリズム
paySign支払署名
successインタフェースのコールバックに成功
failインタフェース失敗コールバック
completeインタフェースはコールバックを完了します(成功、失敗はすべて実行します)
1.バックグラウンドインタフェースを先に呼び出し、基本データを生産する
  1. //
  2. Api.BalancePay({
  3. openid: openid,
  4. amount: amount,
  5. bid: bid,
  6. }).then(res => {
  7. if (res.errno) {
  8. wx.showToast({ title: res.errdesc });
  9. return;
  10. }
  11. var data = res. data;
  12. ...
  13. })

id 。

  1. public function unifiedorder($openid,$order_num,$total_fee,$products_name){
  2. $trade_no = $order_num;
  3. $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
  4. $data = [
  5. 'appid' => C( 'APPID'),
  6. 'mch_id' => C( 'MCHID'),
  7. 'nonce_str' => $this->createNonceStr(),
  8. 'sign_type' => 'MD5',
  9. 'body' => $products_name, //
  10. 'attach' => C( 'PAY_ATTACH_NAME'),
  11. 'out_trade_no' => $trade_no, //
  12. 'fee_type' => 'CNY',
  13. 'total_fee' => $total_fee,
  14. 'spbill_create_ip' => $_SERVER[ 'REMOTE_ADDR'],
  15. 'goods_tag' => C( 'PAY_ATTACH_NAME'),
  16. 'notify_url' => 'https://a.squmo.com/'.C( 'PAY_URL_NAME'). '/Recharge/order_notice',
  17. 'trade_type' => 'JSAPI',
  18. 'openid' => $openid
  19. ];
  20. //
  21. $sign = $this->MakeSign($data);
  22. $data[ 'sign'] = $sign;
  23. $xml = $this->ToXml($data);
  24. vendor( 'Func.Http');
  25. //
  26. $result = $this->FromXml(Http::postXmlCurl($url,$xml));
  27. return $result;
  28. }

, 。

  1. $unifiedorder = $this->unifiedorder($openid,$order_num,$total_price,$products_name);
  2. $data = [
  3. 'appId' => C( 'APPID'),
  4. 'timeStamp' => time(),
  5. 'nonceStr' => $this->createNonceStr(),
  6. 'package' => 'prepay_id='.$unifiedorder[ 'prepay_id'],
  7. 'signType' => 'MD5'
  8. ];
  9. //
  10. $sign = $this->MakeSign($data);
  11. $data[ 'sign'] = $sign;
  12. $this->json->setAttr( 'data',$data);
  13. $this->json->Send();

2.

  1. wx.requestPayment({
  2. 'timeStamp': data.timeStamp.toString(),
  3. 'nonceStr': data.nonceStr,
  4. 'package': data.package,
  5. 'signType': 'MD5',
  6. 'paySign': data.sign,
  7. 'success': function (res) {
  8. console.log( ' ');
  9. },
  10. 'fail': function (res) {
  11. console.log( ' ');
  12. return;
  13. },
  14. 'complete': function (res) {
  15. console.log( ' ');
  16. var url = that.data.url;
  17. console.log( 'get url', url)
  18. if (res.errMsg == 'requestPayment:ok') {
  19. wx.showModal({
  20. title: ' ',
  21. content: ' '
  22. });
  23. if (url) {
  24. setTimeout( function () {
  25. wx.redirectTo({
  26. url: '/pages' + url
  27. });
  28. }, 2000)
  29. } else {
  30. setTimeout( () => {
  31. wx.navigateBack()
  32. }, 2000)
  33. }
  34. }
  35. return;
  36. }
  37. });

3. ( )

  1. //
  2. public function order_notice(){
  3. // post
  4. $xml = $GLOBALS[ 'HTTP_RAW_POST_DATA'];
  5. //
  6. $data = $this->FromXml($xml);
  7. // sign
  8. $data_sign = $data[ 'sign'];
  9. // sign
  10. unset($data[ 'sign']);
  11. $sign = $this->makeSign($data);
  12. //
  13. if ( ($sign===$data_sign) && ($data[ 'return_code']== 'SUCCESS') && ($data[ 'result_code']== 'SUCCESS') ) {
  14. $result = $data;
  15. //
  16. $order_num = $data[ 'out_trade_no']; //
  17. $openid = $data[ 'openid']; // openID
  18. $total_fee = $data[ 'total_fee']; //
  19. $transaction_id = $data[ 'transaction_id']; //
  20. $user = M( 'user');
  21. $user_flag = $user->where( array( 'openid'=>$openid))->find();
  22. $save_data = array(
  23. 'total_payed_price' => $total_fee, //
  24. 'transaction_id' => $transaction_id,
  25. 'paytime' => time(),
  26. 'status' => 2 //1 ;2 ;3 ;4 ;5
  27. );
  28. $recharge = M( 'recharge');
  29. $recharge_flag=$recharge->where( array( 'recoder'=>$order_num, 'uid'=>$user_flag[ 'id']))->find();
  30. $recharge_amount=$recharge_flag[ 'amount'];
  31. $recharge_save_flag =$recharge->where( array( 'recoder'=>$order_num, 'uid'=>$user_flag[ 'id']))->save($save_data);
  32. if($recharge_save_flag){
  33. $save_balance[ 'balance']= $user_flag[ 'balance']+$recharge_amount;
  34. $result_balance =$user->where( array( 'openid'=>$openid))->save($save_balance);
  35. }
  36. } else{
  37. $result = false;
  38. }
  39. //
  40. if ($result) {
  41. $str= '';
  42. } else{
  43. $str= '';
  44. }
  45. echo $str;
  46. return $result;
  47. }

  1. public function FromXml($xml)
  2. {
  3. if(!$xml){
  4. throw new WxPayException( "xml !");
  5. }
  6. // XML array
  7. // xml
  8. libxml_disable_entity_loader( true);
  9. $this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
  10. return $this->values;
  11. }
  12. public function ToXml($array){
  13. if(!is_array($array)|| count($array) <= 0){
  14. return ;
  15. }
  16. $xml = '';
  17. foreach ($array as $key=>$val){
  18. if (is_numeric($val)){
  19. $xml.= ".$key. ">".$val. "".$key. ">";
  20. } else{
  21. $xml.= ".$key. ">.$val. "]]>".$key. ">";
  22. }
  23. }
  24. $xml.= "";
  25. return $xml;
  26. }
  27. private function MakeSign($data)
  28. {
  29. // :
  30. ksort($data);
  31. $string = $this->ToUrlParams($data);
  32. // : string KEY
  33. $string = $string . "&key=".C( 'WEIXIN_PAY_KEY');
  34. // :MD5
  35. $string = md5($string);
  36. // :
  37. $result = strtoupper($string);
  38. return $result;
  39. }
  40. private function ToUrlParams($array)
  41. {
  42. $buff = "";
  43. foreach ($array as $k => $v)
  44. {
  45. if($k != "sign" && $v != "" && !is_array($v)){
  46. $buff .= $k . "=" . $v . "&";
  47. }
  48. }
  49. $buff = trim($buff, "&");
  50. return $buff;
  51. }