TestRefill.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. use PHPUnit\Framework\TestCase;
  3. define('APP_ID', 'test');
  4. define('BASE_ROOT_PATH', str_replace('/test', '', dirname(__FILE__)));
  5. require_once(BASE_ROOT_PATH . '/global.php');
  6. require_once(BASE_CORE_PATH . '/lrlz.php');
  7. require_once(BASE_ROOT_PATH . '/fooder.php');
  8. require_once(BASE_CORE_PATH . '/framework/function/http.php');
  9. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  10. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  11. const LocalTest = 1;
  12. const NetTest = 2;
  13. const CurrentTest = NetTest;
  14. class TestRefill extends TestCase
  15. {
  16. private $mReqHost = '';
  17. private $mKey='';
  18. private $mMchid = 0;
  19. public function __construct(?string $name = null, array $data = [], $dataName = '')
  20. {
  21. parent::__construct($name, $data, $dataName);
  22. if(CurrentTest == LocalTest) {
  23. $this->mReqHost = BASE_SITE_URL;
  24. $this->mMchid = 1;
  25. $this->mKey = '1ff02223b771c0414468c8892151c602';
  26. }
  27. else {
  28. $this->mReqHost = 'https://www.xyzshops.cn';
  29. $this->mMchid = 1092;
  30. $this->mKey = '210fe406954220f56085997d6a4c5b80';
  31. }
  32. }
  33. public static function setUpBeforeClass(): void
  34. {
  35. Base::run_util();
  36. }
  37. public function testFactory()
  38. {
  39. $providers = refill\RefillFactory::instance();
  40. }
  41. private function make_sn()
  42. {
  43. return mt_rand(1000,9999)
  44. . sprintf('%010d',time())
  45. . sprintf('%06d', (float) microtime() * 1000000);
  46. }
  47. public function testBJBAddPhone()
  48. {
  49. $providers = new refill\bjb\RefillPhone([]);
  50. for ($i = 3; $i > 0; --$i) {
  51. $resp = $providers->add(13911129867,4,10,['order_sn' => $this->make_sn()]);
  52. $resp = $providers->add(18500608333,4,10,['order_sn' => $this->make_sn()]);
  53. $resp = $providers->add(18510683168,4,10,['order_sn' => $this->make_sn()]);
  54. $resp = $providers->add(18513846008,4,10,['order_sn' => $this->make_sn()]);
  55. $resp = $providers->add(18518237398,4,10,['order_sn' => $this->make_sn()]);
  56. }
  57. }
  58. public function testIpWhiteList()
  59. {
  60. $x = serialize(['47.99.57.105']);
  61. $ips = unserialize('a:1:{i:0;s:12:"47.99.57.105";}');
  62. if(!empty($ips)) {
  63. $addr = '47.99.57.105';
  64. Log::record("request ip:{$addr}",Log::DEBUG);
  65. if(!in_array($addr,$ips)) {
  66. throw new Exception("请求地址不在白名单中");
  67. }
  68. }
  69. }
  70. public function testRefillPhone()
  71. {
  72. global $config;
  73. $providers = new \refill\bjb\RefillPhone($config['phone_providers']['bjb']);
  74. $resp = $providers->add(18500608333,5,50,['order_sn' => '200229600551218886']);
  75. //{"ack":"success","message":{"order_number":13281474,"charged_amount":"48.750","shipping_status":"5","shipping_status_desc":"未发货"}}
  76. //$_POST='{"order_number":13281474,"shipping_status":1,"shipping_status_desc":"已发货","shipping_status_message":"","sign":"05863f9931ed69a70e456222f057dfdd","voucher":"110103307162012081746340295254","vouchertype":"","voucherurl":"","tradeNo":"200649600557718888"}';
  77. $params['order_number'] = 15566432;
  78. $params['shipping_status'] = 1;
  79. $params['shipping_status_desc'] = '已发货';
  80. $params['shipping_status_message'] = '';
  81. $params['sign'] = '53aaf3dee6b0dd17b081073a0df7019c';
  82. $params['voucher'] = '110103307162012081746340295254';
  83. $params['vouchertype'] = '';
  84. $params['voucherurl'] = '';
  85. $params['tradeNo'] = '200649600557718887';
  86. // $ret = refill\RefillFactory::instance()->notify('beixt',$params);
  87. }
  88. public function testSuhxVerify()
  89. {
  90. $data = 'onlystr=920661883198026632&sign=533151e3f0dff5f507786aabc37b18f7&amt=100&notifyurl=https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php&jdno&cardtype=Sinoepc&batchid=26106&cardno=1000111100020654226&orgid=1590993600&status=3';
  91. $url = "https://www.xyzshops.cn/mobile/refill_suhc.php?" . $data;
  92. // $url = "http://192.168.1.220/mobile/refill_suhc.php?" . $data;
  93. $resp = http_request($url);
  94. }
  95. public function testBeixtVerify()
  96. {
  97. $data = '{"order_number":17434573,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"3d3b92fc5e30354fc05acf6b7225f699","voucher":"501373971049867026","vouchertype":"","voucherurl":"","tradeNo":"470661991563932741"}';
  98. $url = "http://192.168.1.220/mobile/refill_bxtwt.php?xxx=yyy";
  99. // $url = "http://192.168.1.220/mobile/refill_bxtwt.php";
  100. // $url = "https://www.xyzshops.cn/mobile/refill_bxtwt.php";
  101. // $resp = http_post_data($url,$data,['']);
  102. $headers = ['Content-Type: application/json'];
  103. $resp = http_post_data($url,$data,$headers);
  104. }
  105. public function testInput()
  106. {
  107. $input = fopen("php://input", "rw");
  108. file_put_contents($input,'xxxx');
  109. $content = file_get_contents('php://input');
  110. }
  111. public function testUrl()
  112. {
  113. $data = 'onlystr=190661874318128632&sign=9eb60765c356341fd41c9bec6526f46a&amt=1000&notifyurl=https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php&jdno&cardtype=Sinoepc&batchid=25940&cardno=1000111100012304048&orgid=1590993600&status=2';
  114. $params = preg_split('/&/', $data);
  115. foreach ($params as $pair)
  116. {
  117. $kv = explode('=', $pair);
  118. $count = count($kv);
  119. if($count === 1) {
  120. $key = $kv[0];
  121. $val = "";
  122. }
  123. elseif($count === 2) {
  124. $key = $kv[0];
  125. $val = $kv[1];
  126. }
  127. else {
  128. continue;
  129. }
  130. Log::record("{$key}:{$val}",Log::DEBUG);
  131. }
  132. }
  133. public function testAddoil()
  134. {
  135. $url = $this->mReqHost . "/mobile/index.php";
  136. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  137. $params = ['mchid' => 1,
  138. 'cardno' => '1000111100020445281',
  139. 'amount' => "100",
  140. "act"=>"refill",
  141. "op"=>"add",
  142. 'order_sn' => "13281478",
  143. 'notifyurl'=> $notifyurl];
  144. $resp = $this->send_md5($url,$params);
  145. Log::record($resp,Log::DEBUG);
  146. }
  147. public function testAddPhoe()
  148. {
  149. $phones = [//13911129867,
  150. 18500608333,
  151. 18510683168,
  152. 18513846008,
  153. 18518237398];
  154. $amount = 10;
  155. for ($i = 0; $i < 3; $i++)
  156. {
  157. foreach ($phones as $phone) {
  158. $url = $this->mReqHost . "/mobile/index.php";
  159. $params = ['mchid' => 1092,
  160. 'cardno' => $phone,
  161. 'amount' => $amount,
  162. "act"=>"refill",
  163. "op"=>"add",
  164. 'order_sn' => $this->make_sn(),
  165. 'notifyurl'=> $this->mReqHost . "/mobile/refill_xyz.php"];
  166. $resp = $this->send_md5($url,$params);
  167. Log::record($resp,Log::DEBUG);
  168. }
  169. }
  170. }
  171. public function testGoods()
  172. {
  173. $req_url = $this->mReqHost . '/mobile/index.php';
  174. $params = ['mchid' => $this->mMchid,
  175. "act"=>"refill",
  176. "op"=>"goods"];
  177. $resp = $this->send_md5($req_url,$params);
  178. }
  179. public function testQueryFactory()
  180. {
  181. refill\RefillFactory::instance()->query(295);
  182. }
  183. public function testQuery()
  184. {
  185. //$req_url = 'https://www.xyzshops.cn/mobile/index.php';
  186. // $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  187. $req_url = BASE_SITE_URL . '/mobile/index.php';
  188. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  189. $params = ['mchid' => 1,
  190. "act"=>"refill",
  191. "op"=>"query",
  192. 'order_sn' => "13281476"];
  193. $resp = $this->send_md5($req_url,$params);
  194. }
  195. public function testRemoveSession()
  196. {
  197. $resp = http_request(BASE_SITE_URL . "/mobile/signature.php",[],'POST'); }
  198. public function testSuhcCB()
  199. {
  200. // $notifyurl = BASE_SITE_URL . "/mobile/refill_suhc.php";
  201. $notifyurl = "https://www.xyzshops.cn/mobile/refill_suhc.php";
  202. // $notifyurl = "https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php";
  203. $params = ['onlystr' => '920660917672249741',
  204. 'amt' => 100,
  205. 'jdno'=>'',
  206. 'notifyurl' => $notifyurl,
  207. 'cardtype' => 'Sinoepc',
  208. 'batchid' => 18512,
  209. 'cardno' => '1000111100020445281',
  210. 'orgid' => '1590993600',
  211. 'status' => 2];
  212. $sign = $this->md5_sign($params);
  213. $params['sign'] = $sign;
  214. $resp = http_request($notifyurl,$params,'POST');
  215. Log::record($resp,Log::DEBUG);
  216. }
  217. public function testZFKJ()
  218. {
  219. //带签名参数:mchid=1090&act=refill&op=add&cardno=100112121212212133
  220. //&amount=10&order_sn=PH2012261356569433&
  221. //notifyurl=https%3A%2F%2Fqzcz.edusahoo.com.cn%2Findex%2Findex%2Fcallback
  222. //&sign=4a3ac5f9706e64aa70c6cab0fc5839d3
  223. $params = ['mchid' => 1090,
  224. 'cardno' => '100112121212212133',
  225. 'amount' => '10',
  226. "act"=>"refill",
  227. "op"=>"add",
  228. 'order_sn' => 'PH2012261356569433',
  229. 'notifyurl'=> 'https://qzcz.edusahoo.com.cn/index/index/callback'];
  230. $resp = $this->send_md5(BASE_SITE_URL . '/mobile/index.php',$params);
  231. }
  232. protected function check_empty($value)
  233. {
  234. if (!isset($value))
  235. return true;
  236. if ($value === null)
  237. return true;
  238. if (trim($value) === "")
  239. return true;
  240. return false;
  241. }
  242. private function md5_sign($params)
  243. {
  244. ksort($params);
  245. $body = "";
  246. $i = 0;
  247. foreach ($params as $k => $v)
  248. {
  249. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  250. {
  251. if ($i == 0) {
  252. $body .= "{$k}" . "=" . urldecode($v);
  253. } else {
  254. $body .= "&" . "{$k}" . "=" . urldecode($v);
  255. }
  256. $i++;
  257. }
  258. }
  259. $body .= "&key=7yDCLS6S2KzSAJQOUc3vsa";
  260. return md5($body);
  261. }
  262. public function testCallMech()
  263. {
  264. $logic = Logic('queue');
  265. $logic->NotifyMerchantComplete(['order_id' => 289]);
  266. }
  267. private function send($url,$params)
  268. {
  269. $mchid = $params['mchid'];
  270. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  271. $key = file_get_contents($pri_key);
  272. $pri = openssl_get_privatekey($key);
  273. $body = $this->body($params);
  274. openssl_sign($body, $signed, $pri);
  275. $sign = base64_encode($signed);
  276. $params['sign'] = $sign;
  277. $resp = http_request($url,$params,'POST');
  278. Log::record("resp:{$resp}",Log::DEBUG);
  279. }
  280. private function send_md5($url,$params)
  281. {
  282. $body = $this->body($params);
  283. $body .= "&key={$this->mKey}";
  284. // $body .= "&key=ZhongFKJZhongFKJZhongFKJ";
  285. $params['sign'] = md5($body);
  286. $resp = http_request($url,$params,'POST');
  287. Log::record("resp:{$resp}",Log::DEBUG);
  288. return $resp;
  289. }
  290. public function testCardType()
  291. {
  292. $cardno = '1000111100021211884';
  293. $ret = preg_match( '/^1[0-9]{18}$/',$cardno,$matches);
  294. }
  295. public function testMtrand()
  296. {
  297. }
  298. public function testHttpRefill()
  299. {
  300. $mchid = '000001';
  301. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  302. $key = file_get_contents($pri_key);
  303. $pri = openssl_get_privatekey($key);
  304. $params = ['MCHID' => $mchid,'cardno' => '1000111100021211884','amt' => "100","act"=>"refill","op"=>"addoil"];
  305. $body = $this->body($params);
  306. openssl_sign($body, $signed, $pri);
  307. $sign = base64_encode($signed);
  308. $params['sign'] = $sign;
  309. $resp = http_request(BASE_SITE_URL . "/mobile/index.php",$params,'POST');
  310. }
  311. private function body($params)
  312. {
  313. ksort($params);
  314. $body = "";
  315. $i = 0;
  316. foreach ($params as $k => $v)
  317. {
  318. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1))
  319. {
  320. if ($i == 0) {
  321. $body .= "{$k}" . "=" . urlencode($v);
  322. } else {
  323. $body .= "&" . "{$k}" . "=" . urlencode($v);
  324. }
  325. $i++;
  326. }
  327. }
  328. return $body;
  329. }
  330. private function checkEmpty($value)
  331. {
  332. if (!isset($value))
  333. return true;
  334. if ($value === null)
  335. return true;
  336. if (trim($value) === "")
  337. return true;
  338. return false;
  339. }
  340. public function testKsort()
  341. {
  342. $age= ["Peter"=>"35","Ben"=>"37","Joe"=>"43"];
  343. ksort($age);
  344. foreach($age as $x=>$x_value)
  345. {
  346. echo "Key=" . $x . ", Value=" . $x_value;
  347. echo "<br>";
  348. }
  349. }
  350. public function testip(){
  351. $model_merchant = Model('merchant');
  352. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => 1]);
  353. $ipwhitelist = unserialize($merchant_info['ip_white_list']);
  354. $res = json_encode($ipwhitelist);
  355. }
  356. }