當前位置:首頁 » 編程語言 » php生成訂單號

php生成訂單號

發布時間: 2022-05-31 07:42:34

php 後台生成微信預支付訂單 為什麼查詢不到

<?php
namespace common\services\WechatPay;
class WechatAppPay extends WechatPayBase
{
//package參數
public $package = [];
//非同步通知參數
public $notify = [];
//推送預支付訂單參數
protected $config = [];
//存儲access token和獲取時間的文件
protected $file;
//access token
protected $accessToken;
//取access token的url
const ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
//生成預支付訂單提交地址
const POST_ORDER_URL = 'https://api.weixin.qq.com/pay/genprepay?access_token=%s';
public function __construct()
{
$this->file = __DIR__ . '/payAccessToken.txt';
}
/**
* 創建APP支付最終返回參數
* @throws \Exception
* @return multitype:string NULL
*/
public function createAppPayData()
{
$this->generateConfig();
$prepayid = $this->getPrepayid();
try{
$array = [
'appid' => $this->appid,
'appkey' => $this->paySignkey,
'noncestr' => $this->getRandomStr(),
'package' => 'Sign=WXPay',
'partnerid' => $this->partnerId,
'prepayid' => $prepayid,
'timestamp' => (string)time(),
];
$array['sign'] = $this->sha1Sign($array);
unset($array['appkey']);
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
return $array;
}
/**
* 驗證支付成功後的通知參數
*
* @throws \Exception
* @return boolean
*/
public function verifyNotify()
{
try{
$staySignStr = $this->notify;
unset($staySignStr['sign']);
$sign = $this->signData($staySignStr);
return $this->notify['sign'] === $sign;
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* 魔術方法,給添加支付參數進來
*
* @param string $name 參數名
* @param string $value 參數值
*/
public function __set($name, $value)
{
$this->$name = $value;
}
/**
* 設置access token
* @param string $token
* @throws \Exception
* @return boolean
*/
public function setAccessToken()
{
try{
if(!file_exists($this->file) || !is_file($this->file)) {
$f = fopen($this->file, 'a');
fclose($f);
}
$content = file_get_contents($this->file);
if(!empty($content)) {
$info = json_decode($content, true);
if( time() - $info['getTime'] < 7150 ) {
$this->accessToken = $info['accessToken'];
return true;
}
}
//文件內容為空或access token已失效,重新獲取
$this->outputAccessTokenToFile();
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
return true;
}
/**
* 寫入access token 到文件
* @throws \Exception
* @return boolean
*/
protected function outputAccessTokenToFile()
{
try{
$f = fopen($this->file, 'wb');
$token = [
'accessToken' => $this->getAccessToken(),
'getTime' => time(),
];
flock($f, LOCK_EX);
fwrite($f, json_encode($token));
flock($f, LOCK_UN);
fclose($f);
$this->accessToken = $token['accessToken'];
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
return true;
}
/**
* 取access token
*
* @throws \Exception
* @return string
*/
protected function getAccessToken()
{
$url = sprintf(self::ACCESS_TOKEN_URL, $this->appid, $this->appSecret);
$result = json_decode( $this->getUrl($url), true );
if(isset($result['errcode'])) {
throw new \Exception("get access token failed:{$result['errmsg']}");
}
return $result['access_token'];
}
/**
* 取預支付會話標識
*
* @throws \Exception
* @return string
*/
protected function getPrepayid()
{
$data = json_encode($this->config);
$url = sprintf(self::POST_ORDER_URL, $this->accessToken);
$result = json_decode( $this->postUrl($url, $data), true );
if( isset($result['errcode']) && $result['errcode'] != 0 ) {
throw new \Exception($result['errmsg']);
}
if( !isset($result['prepayid']) ) {
throw new \Exception('get prepayid failed, url request error.');
}
return $result['prepayid'];
}
/**
* 組裝預支付參數
*
* @throws \Exception
*/
protected function generateConfig()
{
try{
$this->config = [
'appid' => $this->appid,
'traceid' => $this->traceid,
'noncestr' => $this->getRandomStr(),
'timestamp' => time(),
'package' => $this->generatePackage(),
'sign_method' => $this->sign_method,
];
$this->config['app_signature'] = $this->generateSign();
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* 生成package欄位
*
* 生成規則:
* 1、生成sign的值signValue
* 2、對package參數再次拼接成查詢字元串,值需要進行urlencode
* 3、將sign=signValue拼接到2生成的字元串後面得到最終的package字元串
*
* 第2步urlencode空格需要編碼成%20而不是+
*
* RFC 1738會把 空格編碼成+
* RFC 3986會把空格編碼成%20
*
* @return string
*/
protected function generatePackage()
{
$this->package['sign'] = $this->signData($this->package);
return http_build_query($this->package, '', '&', PHP_QUERY_RFC3986);
}
/**
* 生成簽名
*
* @return string
*/
protected function generateSign()
{
$signArray = [
'appid' => $this->appid,
'appkey' => $this->paySignkey,
'noncestr' => $this->config['noncestr'],
'package' => $this->config['package'],
'timestamp' => $this->config['timestamp'],
'traceid' => $this->traceid,
];
return $this->sha1Sign($signArray);
}
/**
* 簽名數據
*
* 生成規則:
* 1、字典排序,拼接成查詢字元串格式,不需要urlencode
* 2、上一步得到的字元串最後拼接上key=paternerKey
* 3、MD5哈希字元串並轉換成大寫得到sign的值signValue
*
* @param array $data 待簽名數據
* @return string 最終簽名結果
*/
protected function signData($data)
{
ksort($data);
$str = $this->arrayToString($data);
$str .= "&key={$this->partnerKey}";
return strtoupper( $this->signMd5($str) );
}
/**
* sha1簽名
* 簽名規則
* 1、字典排序
* 2、拼接查詢字元串
* 3、sha1運算
*
* @param array $arr
* @return string
*/
protected function sha1Sign($arr)
{
ksort($arr);
return sha1( $this->arrayToString($arr) );
}
}

Ⅱ php商品支付成功後在另一個數據表生成了訂單號,如何把這個單號和商品數據表中的關聯數據一起讀出

首先你的saixin表中是否有商品ID欄位,如果沒有就不能關聯
saixin表中如果有商品ID欄位,那這個欄位記錄的是單一商品ID,還是商品ID的集合(一個訂單包含多商品)
<?php?>標簽不能嵌套

Ⅲ thinkphp訂單號怎麼生成

  • PHP 生成訂單號,GUID 方法

  • 生成訂單號

function build_order_no() {

return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);

}

  • 生成GUID

function guid() {

if (function_exists('com_create_guid')) {

return com_create_guid();

} else {

mt_srand((double)microtime()*10000);

$charid = strtoupper(md5(uniqid(rand(), true)));

$hyphen = chr(45);

$uuid = chr(123)

.substr($charid, 0, 8).$hyphen

.substr($charid, 8, 4).$hyphen

.substr($charid,12, 4).$hyphen

.substr($charid,16, 4).$hyphen

.substr($charid,20,12)

.chr(125);

return $uuid;

}

}

Ⅳ 在php中點擊生成訂單時,表格上面有兩行是空白行,怎樣去掉這兩行

判斷 if($bookname){
執行代碼

}

Ⅳ php 獲取淘寶訂單號

到淘寶的開放平台,然後下載PHP的SDK,然後創建賣家APP,然後獲取key和密鑰!然後根據API文檔調取對應的訂單API!

Ⅵ php如何生成訂單號

echo date('ymdHis').rand(1000,2000);這樣也有重復的幾率,但是很小。如果想更低就用微秒同一微秒然後隨機1000-2000的幾率就更小了

Ⅶ 織夢中的訂單編號{dede:var.carts.orders_id/}如何用PHP語言表達

<?php

echo$carts['orders_id'];

?>


就可以了

Ⅷ 在php中點擊生成訂單時,表格裡面自動生成前面兩行是空白行,怎樣去掉這兩行

table的列數和你要插入的信息的條數是否一致

Ⅸ php生成唯一訂單號 時間戳可以嗎

可以,但是最好是毫秒級的時間戳,,防止並發的情況

Ⅹ php商城中訂單號的生成,並且調用支付包介面,把交易訂單保存資料庫。這個過程是怎麼樣的

介面對接么!這些都在支付寶介面技術文檔中有。你仔細查看支付寶的技術文檔。

熱點內容
python做腳本 發布:2025-02-11 17:05:42 瀏覽:548
風神瞳腳本 發布:2025-02-11 17:02:18 瀏覽:690
物理化學壓縮 發布:2025-02-11 17:02:03 瀏覽:295
蔚來配置哪些值得加 發布:2025-02-11 16:58:28 瀏覽:325
索引型資料庫 發布:2025-02-11 16:58:26 瀏覽:916
hbasephp 發布:2025-02-11 16:44:41 瀏覽:761
微軟不給源碼 發布:2025-02-11 16:13:37 瀏覽:38
php的get方法 發布:2025-02-11 16:12:30 瀏覽:967
源碼網嘉 發布:2025-02-11 16:07:06 瀏覽:192
免費ftp服務軟體 發布:2025-02-11 15:58:06 瀏覽:866