Pay.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace app\extra\dyMini;
  3. use app\extra\dyLife\BasicLife;
  4. use yzh52521\EasyHttp\Http;
  5. class Pay extends BasicLife
  6. {
  7. public function createOrder(array $data = [])
  8. {
  9. $url = "https://developer.toutiao.com/api/apps/ecpay/v1/create_order";
  10. $param = [
  11. "out_order_no" => $data['order_sn'].rand(1000,9999),
  12. "total_amount" => (int) $data['total'],
  13. "subject" => $data['name'],
  14. "body" => $data['name'],
  15. "valid_time" => 1800,
  16. "notify_url" => $data['notify_url'],
  17. "cp_extra" => $data['order_sn']
  18. ];
  19. $param['sign'] = $this->requestSign($param);
  20. $param['app_id'] = $this->config['appid'];
  21. $resp = Http::asJson()->post($url,$param)->array();
  22. if ($resp['err_no'] == 0) {
  23. return $resp['data'];
  24. }
  25. return [];
  26. }
  27. /**
  28. * 生成签名
  29. */
  30. private function requestSign($params): string
  31. {
  32. $rList = [];
  33. foreach($params as $k =>$v) {
  34. if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
  35. continue;
  36. $value = trim(strval($v));
  37. if (is_array($v)) {
  38. $value = arrayToStr($v);
  39. }
  40. $len = strlen($value);
  41. if ($len > 1 && substr($value, 0,1)=="\"" && substr($value, $len-1)=="\"")
  42. $value = substr($value,1, $len-1);
  43. $value = trim($value);
  44. if ($value == "" || $value == "null")
  45. continue;
  46. $rList[] = $value;
  47. }
  48. $rList[] = $this->config['salt'];
  49. sort($rList, SORT_STRING);
  50. return md5(implode('&', $rList));
  51. }
  52. /**
  53. * @param $privateKeyStr
  54. * @param $data
  55. * @param $appId
  56. * @param $nonceStr
  57. * @param $timestamp
  58. * @param $keyVersion
  59. * @return string|null
  60. * @throws \Exception
  61. */
  62. public function getByteAuthorization(string $privateKeyStr, string $data, string $appId, string $nonceStr,string $timestamp, string $keyVersion) {
  63. $byteAuthorization = '';
  64. // 读取私钥
  65. $privateKey = openssl_pkey_get_private($privateKeyStr);
  66. if (!$privateKey) {
  67. throw new \Exception("Invalid private key");
  68. }
  69. // 生成签名
  70. $signature = $this->getSignature("POST", "/requestOrder", $timestamp, $nonceStr, $data, $privateKeyStr);
  71. if ($signature === false) {
  72. return null;
  73. }
  74. // 构造 byteAuthorization
  75. $byteAuthorization = sprintf("SHA256-RSA2048 appid=%s,nonce_str=%s,timestamp=%s,key_version=%s,signature=%s", $appId, $nonceStr, $timestamp, $keyVersion, $signature);
  76. return $byteAuthorization;
  77. }
  78. public function getSignature(string $method, string $url, string $timestamp, string $nonce, $data, $privateKey) {
  79. // printf("method:%s\n url:%s\n timestamp:%s\n nonce:%s\n data:%s", $method, $url, $timestamp, $nonce, $data);
  80. $targetStr = $method. "\n" . $url. "\n" . $timestamp. "\n" . $nonce. "\n" . $data. "\n";
  81. openssl_sign($targetStr, $sign, $privateKey, OPENSSL_ALGO_SHA256);
  82. return base64_encode($sign);
  83. }
  84. }