Skip to content

Commit 6d5180c

Browse files
committed
'update'
1 parent 11f752d commit 6d5180c

7 files changed

Lines changed: 564 additions & 10 deletions

File tree

app/Components/Trimepay.php

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
class Trimepay {
3+
private $appId;
4+
private $appSecret;
5+
/**
6+
* 签名初始化
7+
* @param merKey 签名密钥
8+
*/
9+
public function __construct($appId, $appSecret) {
10+
$this->appId = $appId;
11+
$this->appSecret = $appSecret;
12+
//国内
13+
// $this->gatewayUri = 'https://api.ecy.es/gateway/pay/go';
14+
// $this->refundUri = 'https://api.ecy.es/gateway/refund/go';
15+
//国外
16+
$this->gatewayUri = 'https://api.trimepay.com/gateway/pay/go';
17+
$this->refundUri = 'https://api.trimepay.com/gateway/refund/go';
18+
}
19+
20+
/**
21+
* @name 准备签名/验签字符串
22+
*/
23+
public function prepareSign($data) {
24+
ksort($data);
25+
return http_build_query($data);
26+
}
27+
/**
28+
* @name 生成签名
29+
* @param sourceData
30+
* @return 签名数据
31+
*/
32+
public function sign($data) {
33+
$signature = strtolower(md5(md5($data).$this->appSecret));
34+
return $signature;
35+
}
36+
/*
37+
* @name 验证签名
38+
* @param signData 签名数据
39+
* @param sourceData 原数据
40+
* @return
41+
*/
42+
public function verify($data, $signature) {
43+
$mySign = $this->sign($data);
44+
if ($mySign === $signature) {
45+
return true;
46+
} else {
47+
return false;
48+
}
49+
}
50+
51+
public function post($data, $url = ''){
52+
if($url == '') {
53+
$url = $this->gatewayUri;
54+
}
55+
$curl = curl_init();
56+
curl_setopt($curl, CURLOPT_URL, $url);
57+
curl_setopt($curl, CURLOPT_HEADER, 0);
58+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
59+
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
60+
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
61+
curl_setopt($curl, CURLOPT_POST, 1);
62+
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
63+
$data = curl_exec($curl);
64+
curl_close($curl);
65+
return json_decode($data, true);
66+
}
67+
68+
public function pay($type, $tradeNo, $totalFee, $notifyUrl = '', $returnUrl = ''){
69+
$payData = [
70+
'appId' => $this->appId,
71+
'merchantTradeNo' => $tradeNo,
72+
'totalFee' => $totalFee * 100,
73+
'notifyUrl' => $notifyUrl,
74+
'returnUrl' => $returnUrl,
75+
'payType' => $type
76+
];
77+
$signData = $this->prepareSign($payData);
78+
$payData['sign'] = $trimepay->sign($signData);
79+
$response = $this->post($payData);
80+
return $response;
81+
}
82+
83+
public function refund($merchantTradeNo) {
84+
$params['merchantTradeNo'] = $merchantTradeNo;
85+
$params['appId'] = $this->appId;
86+
$prepareSign = $this->prepareSign($params);
87+
$params['sign'] = $this->sign($prepareSign);
88+
return $this->post($params, $this->refundUri);
89+
}
90+
public function buildHtml($params, $method = 'post', $target = '_self'){
91+
// var_dump($params);exit;
92+
$html = "<form id='submit' name='submit' action='".$this->gatewayUri."' method='$method' target='$target'>";
93+
foreach ($params as $key => $value) {
94+
$html .= "<input type='hidden' name='$key' value='$value'/>";
95+
}
96+
$html .= "</form><script>document.forms['submit'].submit();</script>";
97+
return $html;
98+
}
99+
}

0 commit comments

Comments
 (0)