TestRefill.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  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;
  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. } else {
  27. $this->mReqHost = 'https://www.xyzshops.cn';
  28. $this->mMchid = 1092;
  29. $this->mKey = '210fe406954220f56085997d6a4c5b80';
  30. }
  31. }
  32. public static function setUpBeforeClass(): void
  33. {
  34. Base::run_util();
  35. }
  36. public function testFactory()
  37. {
  38. $providers = refill\RefillFactory::instance();
  39. }
  40. private function make_sn()
  41. {
  42. return mt_rand(1000, 9999)
  43. . sprintf('%010d', time())
  44. . sprintf('%06d', (float)microtime() * 1000000);
  45. }
  46. public function testBJBAddPhone()
  47. {
  48. $providers = new refill\bjb\RefillPhone([]);
  49. for ($i = 3; $i > 0; --$i) {
  50. $resp = $providers->add(13911129867, 4, 10, ['order_sn' => $this->make_sn()]);
  51. $resp = $providers->add(18500608333, 4, 10, ['order_sn' => $this->make_sn()]);
  52. $resp = $providers->add(18510683168, 4, 10, ['order_sn' => $this->make_sn()]);
  53. $resp = $providers->add(18513846008, 4, 10, ['order_sn' => $this->make_sn()]);
  54. $resp = $providers->add(18518237398, 4, 10, ['order_sn' => $this->make_sn()]);
  55. }
  56. }
  57. public function testIpWhiteList()
  58. {
  59. $x = serialize(['47.99.57.105']);
  60. $ips = unserialize('a:1:{i:0;s:12:"47.99.57.105";}');
  61. if (!empty($ips)) {
  62. $addr = '47.99.57.105';
  63. Log::record("request ip:{$addr}", Log::DEBUG);
  64. if (!in_array($addr, $ips)) {
  65. throw new Exception("请求地址不在白名单中");
  66. }
  67. }
  68. }
  69. public function testXcPhone(){
  70. $providers = new refill\xc\RefillPhone([]);
  71. $resp = $providers->add(18500608333, 5, 100, ['order_sn' => $this->make_sn()]);
  72. }
  73. public function testYifaPhone(){
  74. $providers = new refill\yifa\RefillPhone([]);
  75. $resp = $providers->add(15120035568, 5, 100, ['order_sn' => $this->make_sn()]);
  76. }
  77. public function testXcQuery(){
  78. $providers = new refill\xc\RefillPhone([]);
  79. $refill_info = ['order_sn' => 950663423236726632 , 'ch_trade_no' => 499 , 'card_no' => 1000111200005252927 ];
  80. $resp = $providers->query($refill_info);
  81. }
  82. public function testSuhcQuery(){
  83. $providers = new refill\suhctm\RefillOil([]);
  84. $refill_info = ['order_sn' => 950663423236726632 , 'ch_trade_no' => 499 , 'card_no' => 1000111200005252927 ];
  85. $resp = $providers->query($refill_info);
  86. }
  87. public function testSaihuOil()
  88. {
  89. $providers = new refill\saihu\RefillOil([]);
  90. $resp = $providers->add(1000111100021211884, 2, 100, ['order_sn' => $this->make_sn()]);
  91. }
  92. public function testBdtOil()
  93. {
  94. $providers = new refill\bdt\RefillOil([]);
  95. $resp = $providers->add(1000111100021211884,2,100,['order_sn' => '200229600556618886']);
  96. }
  97. public function testJiecPhone()
  98. {
  99. $providers = new refill\jiec\RefillPhone([]);
  100. $resp = $providers->add('15120035568',2,100,['order_sn' => $this->make_sn()]);
  101. }
  102. public function testBxtwtCB()
  103. {
  104. $data = '{"order_number":18219726,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"863b4d972f2a4d39a9af7396879116c4","voucher":"","vouchertype":"","voucherurl":"","tradeNo":"100662475851197741"}';
  105. }
  106. public function testSuhcJDVerify()
  107. {
  108. $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';
  109. $url = "http://192.168.1.220/mobile/refill_suhc.php?" . $data;
  110. $resp = http_request($url);
  111. }
  112. public function testSuhcPDD()
  113. {
  114. $data = 'onlystr=820663525320005221&pddno&sign=4b1e8645f7541ced34855779448d9d52&amt=100&notifyurl=https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhcpdd.php&cardtype=Sinoepc&batchid=3&cardno=1000111100020445281&orgid=1590993600&status=2';
  115. $url = "https://www.xyzshops.cn/mobile/refill_suhcpdd.php?" . $data;
  116. $resp = http_request($url);
  117. }
  118. public function testSuhcTMVerify()
  119. {
  120. $data = 'onlystr=180662473867058221&sign=5ff5f95d54a89e78dbb49923f6b20c08&amt=100&notifyurl=https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhctm.php&cardtype=Sinoepc&batchid=3&cardno=1000111100020445281&orgid=1590993600&status=3';
  121. // $url = "http://192.168.1.220/mobile/refill_suhctm.php?" . $data;
  122. $url = "https://www.xyzshops.cn/mobile/refill_suhctm.php?" . $data;
  123. $resp = http_request($url);
  124. }
  125. public function testBeixtVerify()
  126. {
  127. $data = '{"order_number":18219726,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"863b4d972f2a4d39a9af7396879116c4","voucher":"","vouchertype":"","voucherurl":"","tradeNo":"100662475851197741"}';
  128. $url = "https://www.xyzshops.cn/mobile/refill_bxtwt.php";
  129. $headers = ['Content-Type: application/json'];
  130. $resp = http_post_data($url, $data, $headers);
  131. }
  132. public function testInput()
  133. {
  134. $input = fopen("php://input", "rw");
  135. file_put_contents($input, 'xxxx');
  136. $content = file_get_contents('php://input');
  137. }
  138. public function testUrl()
  139. {
  140. $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';
  141. $params = preg_split('/&/', $data);
  142. foreach ($params as $pair) {
  143. $kv = explode('=', $pair);
  144. $count = count($kv);
  145. if ($count === 1) {
  146. $key = $kv[0];
  147. $val = "";
  148. } elseif ($count === 2) {
  149. $key = $kv[0];
  150. $val = $kv[1];
  151. } else {
  152. continue;
  153. }
  154. Log::record("{$key}:{$val}", Log::DEBUG);
  155. }
  156. }
  157. public function testAddoil()
  158. {
  159. $url = $this->mReqHost . "/mobile/index.php";
  160. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  161. $params = ['mchid' => 1092,
  162. 'cardno' => '1000111100020445281',
  163. 'amount' => "100",
  164. "act" => "refill",
  165. "op" => "add",
  166. 'order_sn' => $this->make_sn(),
  167. 'notifyurl' => $notifyurl];
  168. $resp = $this->send_md5($url, $params);
  169. Log::record($resp, Log::DEBUG);
  170. }
  171. public function testBalance()
  172. {
  173. $url = $this->mReqHost . "/mobile/index.php";
  174. $params = ['mchid' => 1092,
  175. "act" => "refill",
  176. "op" => "balance"];
  177. $resp = $this->send_md5($url, $params);
  178. Log::record($resp, Log::DEBUG);
  179. }
  180. public function testAddPhoe()
  181. {
  182. $phones = [//13911129867,
  183. 18500608333,
  184. 18510683168,
  185. 18513846008,
  186. 18518237398];
  187. $amount = 10;
  188. for ($i = 0; $i < 3; $i++) {
  189. foreach ($phones as $phone) {
  190. $url = $this->mReqHost . "/mobile/index.php";
  191. $params = ['mchid' => 1092,
  192. 'cardno' => $phone,
  193. 'amount' => $amount,
  194. "act" => "refill",
  195. "op" => "add",
  196. 'order_sn' => $this->make_sn(),
  197. 'notifyurl' => $this->mReqHost . "/mobile/refill_xyz.php"];
  198. $resp = $this->send_md5($url, $params);
  199. Log::record($resp, Log::DEBUG);
  200. }
  201. }
  202. }
  203. public function testProxyHelper()
  204. {
  205. $phone = '13911129867';
  206. $amount = 100;
  207. $url = $this->mReqHost . "/mobile/index.php";
  208. $params = ['mchid' => 1090,
  209. 'cardno' => $phone,
  210. 'amount' => $amount,
  211. "act"=>"refill",
  212. "op"=>"add",
  213. 'order_sn' => $this->make_sn(),
  214. 'notifyurl'=> "https://qzcz.edusahoo.com.cn/index/index/callback"];
  215. $proxy = new refill_proxy("ZhongFKJZhongFKJZhongFKJ");
  216. $resp = $proxy->send($url,$params);
  217. }
  218. public function testGoods()
  219. {
  220. $req_url = $this->mReqHost . '/mobile/index.php';
  221. $params = ['mchid' => $this->mMchid,
  222. "act" => "refill",
  223. "op" => "goods"];
  224. $resp = $this->send_md5($req_url, $params);
  225. }
  226. public function testQueryFactory()
  227. {
  228. refill\RefillFactory::instance()->query(295);
  229. }
  230. public function testQuery()
  231. {
  232. //$req_url = 'https://www.xyzshops.cn/mobile/index.php';
  233. // $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  234. $req_url = BASE_SITE_URL . '/mobile/index.php';
  235. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  236. $params = ['mchid' => 1,
  237. "act" => "refill",
  238. "op" => "query",
  239. 'order_sn' => "13281476"];
  240. $resp = $this->send_md5($req_url, $params);
  241. }
  242. public function testRemoveSession()
  243. {
  244. $resp = http_request(BASE_SITE_URL . "/mobile/signature.php", [], 'POST');
  245. }
  246. public function testSuhcCB()
  247. {
  248. // $notifyurl = BASE_SITE_URL . "/mobile/refill_suhc.php";
  249. $notifyurl = "https://www.xyzshops.cn/mobile/refill_suhc.php";
  250. // $notifyurl = "https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php";
  251. $params = ['onlystr' => '920660917672249741',
  252. 'amt' => 100,
  253. 'jdno' => '',
  254. 'notifyurl' => $notifyurl,
  255. 'cardtype' => 'Sinoepc',
  256. 'batchid' => 18512,
  257. 'cardno' => '1000111100020445281',
  258. 'orgid' => '1590993600',
  259. 'status' => 2];
  260. $sign = $this->md5_sign($params);
  261. $params['sign'] = $sign;
  262. $resp = http_request($notifyurl, $params, 'POST');
  263. Log::record($resp, Log::DEBUG);
  264. }
  265. public function testBJBCB()
  266. {
  267. $params = [
  268. 'usr' => '13699279618',
  269. 'ord' => '710662733975412771',
  270. 'state' => '2',
  271. 'bz' => '',
  272. 'sgn' => 'AC9AFD254DE682D8440A97CA68B992DA'
  273. ];
  274. $resp = http_request("https://www.xyzshops.cn/mobile/refill_bjb.php", $params, 'POST');
  275. }
  276. public function testJiecCB()
  277. {
  278. $params['result'] = 'fail';
  279. $params['msg'] = '0';
  280. $params['order'] = 'null';
  281. $params['phone_no'] = '18500608333';
  282. $params['amount'] = '30.00';
  283. $params['op_no'] = '800663597981819221';
  284. $params['sign'] = '2510046895e8e12322c8a32547905ee9';
  285. $resp = http_request($this->mReqHost . "/mobile/refill_jiec.php", $params, 'POST');
  286. }
  287. public function testZFKJ()
  288. {
  289. //带签名参数:mchid=1090&act=refill&op=add&cardno=100112121212212133
  290. //&amount=10&order_sn=PH2012261356569433&
  291. //notifyurl=https%3A%2F%2Fqzcz.edusahoo.com.cn%2Findex%2Findex%2Fcallback
  292. //&sign=4a3ac5f9706e64aa70c6cab0fc5839d3
  293. $params = ['mchid' => 1090,
  294. 'cardno' => '100112121212212133',
  295. 'amount' => '10',
  296. "act" => "refill",
  297. "op" => "add",
  298. 'order_sn' => 'PH2012261356569433',
  299. 'notifyurl' => 'https://qzcz.edusahoo.com.cn/index/index/callback'];
  300. $resp = $this->send_md5(BASE_SITE_URL . '/mobile/index.php', $params);
  301. }
  302. protected function check_empty($value)
  303. {
  304. if (!isset($value))
  305. return true;
  306. if ($value === null)
  307. return true;
  308. if (trim($value) === "")
  309. return true;
  310. return false;
  311. }
  312. private function md5_sign($params)
  313. {
  314. ksort($params);
  315. $body = "";
  316. $i = 0;
  317. foreach ($params as $k => $v) {
  318. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  319. if ($i == 0) {
  320. $body .= "{$k}" . "=" . urldecode($v);
  321. } else {
  322. $body .= "&" . "{$k}" . "=" . urldecode($v);
  323. }
  324. $i++;
  325. }
  326. }
  327. $body .= "&key=7yDCLS6S2KzSAJQOUc3vsa";
  328. return md5($body);
  329. }
  330. public function testCallMech()
  331. {
  332. $logic = Logic('queue');
  333. $logic->NotifyMerchantComplete(['order_id' => 289]);
  334. }
  335. private function send($url, $params)
  336. {
  337. $mchid = $params['mchid'];
  338. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  339. $key = file_get_contents($pri_key);
  340. $pri = openssl_get_privatekey($key);
  341. $body = $this->body($params);
  342. openssl_sign($body, $signed, $pri);
  343. $sign = base64_encode($signed);
  344. $params['sign'] = $sign;
  345. $resp = http_request($url, $params, 'POST');
  346. Log::record("resp:{$resp}", Log::DEBUG);
  347. }
  348. private function send_md5($url, $params)
  349. {
  350. $body = $this->body($params);
  351. $body .= "&key={$this->mKey}";
  352. $params['sign'] = md5($body);
  353. $resp = http_request($url, $params, 'POST');
  354. Log::record("resp:{$resp}", Log::DEBUG);
  355. return $resp;
  356. }
  357. public function testCardType()
  358. {
  359. $cardno = '1000111100021211884';
  360. $ret = preg_match('/^1[0-9]{18}$/', $cardno, $matches);
  361. }
  362. public function testMtrand()
  363. {
  364. }
  365. public function testHttpRefill()
  366. {
  367. $mchid = '000001';
  368. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  369. $key = file_get_contents($pri_key);
  370. $pri = openssl_get_privatekey($key);
  371. $params = ['MCHID' => $mchid, 'cardno' => '1000111100021211884', 'amt' => "100", "act" => "refill", "op" => "addoil"];
  372. $body = $this->body($params);
  373. openssl_sign($body, $signed, $pri);
  374. $sign = base64_encode($signed);
  375. $params['sign'] = $sign;
  376. $resp = http_request(BASE_SITE_URL . "/mobile/index.php", $params, 'POST');
  377. }
  378. private function body($params)
  379. {
  380. ksort($params);
  381. $body = "";
  382. $i = 0;
  383. foreach ($params as $k => $v) {
  384. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  385. if ($i == 0) {
  386. $body .= "{$k}" . "=" . urlencode($v);
  387. } else {
  388. $body .= "&" . "{$k}" . "=" . urlencode($v);
  389. }
  390. $i++;
  391. }
  392. }
  393. return $body;
  394. }
  395. private function checkEmpty($value)
  396. {
  397. if (!isset($value))
  398. return true;
  399. if ($value === null)
  400. return true;
  401. if (trim($value) === "")
  402. return true;
  403. return false;
  404. }
  405. public function testKsort()
  406. {
  407. $age = ["Peter" => "35", "Ben" => "37", "Joe" => "43"];
  408. ksort($age);
  409. foreach ($age as $x => $x_value) {
  410. echo "Key=" . $x . ", Value=" . $x_value;
  411. echo "<br>";
  412. }
  413. }
  414. public function testip()
  415. {
  416. $model_merchant = Model('merchant');
  417. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => 1]);
  418. $ipwhitelist = unserialize($merchant_info['ip_white_list']);
  419. $res = json_encode($ipwhitelist);
  420. }
  421. //测试向内部店提交订单,可先屏蔽其它充值通道
  422. public function testAddoilToInnerStore()
  423. {
  424. $url = $this->mReqHost . "/mobile/index.php";
  425. $notifyurl = BASE_SITE_URL . '/mobile/signature.php';
  426. $params = ['mchid' => 1,
  427. 'cardno' => '1000111100020445281',
  428. 'amount' => "1000",
  429. "act" => "refill",
  430. "op" => "add",
  431. 'card_name' => '江海苗',
  432. 'order_sn' => $this->make_sn(),
  433. 'notifyurl' => $notifyurl];
  434. $resp = $this->send_md5($url, $params);
  435. Log::record($resp, Log::DEBUG);
  436. }
  437. public function testRefillInnerCB()
  438. {
  439. $params = ['state_type' => 'order_cancel','order_id' => 311,'channel_name' => 'lx'];
  440. $resp = http_request(BASE_SITE_URL . '/mobile/refill_inner.php',$params,'POST');
  441. }
  442. public function testArgs()
  443. {
  444. Log::record($_SERVER['argv'],Log::DEBUG);
  445. }
  446. public function testCall()
  447. {
  448. $order_id = 14974;
  449. QueueClient::async_push("NotifyMerchantComplete", ['order_id' => $order_id], 10);
  450. }
  451. }
  452. //docker-compose run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefill::testCall)( .*)?$/" --test-suffix TestRefill.php /var/www/html/test
  453. //docker-compose -f ./docker-compose-dev.yml run phpcli php /var/www/html/phpunit-9.2.5.phar --filter "/(TestRefill::testCall)( .*)?$/" --test-suffix TestRefill.php /var/www/html/test