TestRefill.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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 = LocalTest;
  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 testXcQuery(){
  74. $providers = new refill\xc\RefillPhone([]);
  75. $refill_info = ['order_sn' => 950663423236726632 , 'ch_trade_no' => 499 , 'card_no' => 1000111200005252927 ];
  76. $resp = $providers->query($refill_info);
  77. }
  78. public function testSuhcQuery(){
  79. $providers = new refill\suhctm\RefillOil([]);
  80. $refill_info = ['order_sn' => 950663423236726632 , 'ch_trade_no' => 499 , 'card_no' => 1000111200005252927 ];
  81. $resp = $providers->query($refill_info);
  82. }
  83. public function testYifaPhone()
  84. {
  85. $providers = new refill\yifa\RefillPhone([]);
  86. $resp = $providers->add(1000111100021211884, 4, 100, ['order_sn' => $this->make_sn()]);
  87. }
  88. public function testSaihuOil()
  89. {
  90. $providers = new refill\saihu\RefillOil([]);
  91. $resp = $providers->add(1000111100021211884, 2, 100, ['order_sn' => $this->make_sn()]);
  92. }
  93. public function testBdtOil()
  94. {
  95. $providers = new \refill\bdt\RefillOil([]);
  96. $resp = $providers->add(1000111100021211884,2,100,['order_sn' => '200229600556618886']);
  97. }
  98. public function testBxtwtCB()
  99. {
  100. $data = '{"order_number":18219726,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"863b4d972f2a4d39a9af7396879116c4","voucher":"","vouchertype":"","voucherurl":"","tradeNo":"100662475851197741"}';
  101. }
  102. public function testSuhcJDVerify()
  103. {
  104. $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';
  105. $url = "http://192.168.1.220/mobile/refill_suhc.php?" . $data;
  106. $resp = http_request($url);
  107. }
  108. public function testSuhcTMVerify()
  109. {
  110. $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';
  111. // $url = "http://192.168.1.220/mobile/refill_suhctm.php?" . $data;
  112. $url = "https://www.xyzshops.cn/mobile/refill_suhctm.php?" . $data;
  113. $resp = http_request($url);
  114. }
  115. public function testBeixtVerify()
  116. {
  117. $data = '{"order_number":18219726,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"863b4d972f2a4d39a9af7396879116c4","voucher":"","vouchertype":"","voucherurl":"","tradeNo":"100662475851197741"}';
  118. $url = "https://www.xyzshops.cn/mobile/refill_bxtwt.php";
  119. $headers = ['Content-Type: application/json'];
  120. $resp = http_post_data($url, $data, $headers);
  121. }
  122. public function testInput()
  123. {
  124. $input = fopen("php://input", "rw");
  125. file_put_contents($input, 'xxxx');
  126. $content = file_get_contents('php://input');
  127. }
  128. public function testUrl()
  129. {
  130. $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';
  131. $params = preg_split('/&/', $data);
  132. foreach ($params as $pair) {
  133. $kv = explode('=', $pair);
  134. $count = count($kv);
  135. if ($count === 1) {
  136. $key = $kv[0];
  137. $val = "";
  138. } elseif ($count === 2) {
  139. $key = $kv[0];
  140. $val = $kv[1];
  141. } else {
  142. continue;
  143. }
  144. Log::record("{$key}:{$val}", Log::DEBUG);
  145. }
  146. }
  147. public function testAddoil()
  148. {
  149. $url = $this->mReqHost . "/mobile/index.php";
  150. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  151. $params = ['mchid' => 1092,
  152. 'cardno' => '1000111100020445281',
  153. 'amount' => "100",
  154. "act" => "refill",
  155. "op" => "add",
  156. 'order_sn' => $this->make_sn(),
  157. 'notifyurl' => $notifyurl];
  158. $resp = $this->send_md5($url, $params);
  159. Log::record($resp, Log::DEBUG);
  160. }
  161. public function testBalance()
  162. {
  163. $url = $this->mReqHost . "/mobile/index.php";
  164. $params = ['mchid' => 1092,
  165. "act" => "refill",
  166. "op" => "balance"];
  167. $resp = $this->send_md5($url, $params);
  168. Log::record($resp, Log::DEBUG);
  169. }
  170. public function testAddPhoe()
  171. {
  172. $phones = [//13911129867,
  173. 18500608333,
  174. 18510683168,
  175. 18513846008,
  176. 18518237398];
  177. $amount = 10;
  178. for ($i = 0; $i < 3; $i++) {
  179. foreach ($phones as $phone) {
  180. $url = $this->mReqHost . "/mobile/index.php";
  181. $params = ['mchid' => 1092,
  182. 'cardno' => $phone,
  183. 'amount' => $amount,
  184. "act" => "refill",
  185. "op" => "add",
  186. 'order_sn' => $this->make_sn(),
  187. 'notifyurl' => $this->mReqHost . "/mobile/refill_xyz.php"];
  188. $resp = $this->send_md5($url, $params);
  189. Log::record($resp, Log::DEBUG);
  190. }
  191. }
  192. }
  193. public function testProxyHelper()
  194. {
  195. $phone = '13911129867';
  196. $amount = 100;
  197. $url = $this->mReqHost . "/mobile/index.php";
  198. $params = ['mchid' => 1090,
  199. 'cardno' => $phone,
  200. 'amount' => $amount,
  201. "act"=>"refill",
  202. "op"=>"add",
  203. 'order_sn' => $this->make_sn(),
  204. 'notifyurl'=> "https://qzcz.edusahoo.com.cn/index/index/callback"];
  205. $proxy = new refill_proxy("ZhongFKJZhongFKJZhongFKJ");
  206. $resp = $proxy->send($url,$params);
  207. }
  208. public function testGoods()
  209. {
  210. $req_url = $this->mReqHost . '/mobile/index.php';
  211. $params = ['mchid' => $this->mMchid,
  212. "act" => "refill",
  213. "op" => "goods"];
  214. $resp = $this->send_md5($req_url, $params);
  215. }
  216. public function testQueryFactory()
  217. {
  218. refill\RefillFactory::instance()->query(295);
  219. }
  220. public function testQuery()
  221. {
  222. //$req_url = 'https://www.xyzshops.cn/mobile/index.php';
  223. // $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  224. $req_url = BASE_SITE_URL . '/mobile/index.php';
  225. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  226. $params = ['mchid' => 1,
  227. "act" => "refill",
  228. "op" => "query",
  229. 'order_sn' => "13281476"];
  230. $resp = $this->send_md5($req_url, $params);
  231. }
  232. public function testRemoveSession()
  233. {
  234. $resp = http_request(BASE_SITE_URL . "/mobile/signature.php", [], 'POST');
  235. }
  236. public function testSuhcCB()
  237. {
  238. // $notifyurl = BASE_SITE_URL . "/mobile/refill_suhc.php";
  239. $notifyurl = "https://www.xyzshops.cn/mobile/refill_suhc.php";
  240. // $notifyurl = "https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php";
  241. $params = ['onlystr' => '920660917672249741',
  242. 'amt' => 100,
  243. 'jdno' => '',
  244. 'notifyurl' => $notifyurl,
  245. 'cardtype' => 'Sinoepc',
  246. 'batchid' => 18512,
  247. 'cardno' => '1000111100020445281',
  248. 'orgid' => '1590993600',
  249. 'status' => 2];
  250. $sign = $this->md5_sign($params);
  251. $params['sign'] = $sign;
  252. $resp = http_request($notifyurl, $params, 'POST');
  253. Log::record($resp, Log::DEBUG);
  254. }
  255. public function testBJBCB()
  256. {
  257. $params = [
  258. 'usr' => '13699279618',
  259. 'ord' => '710662733975412771',
  260. 'state' => '2',
  261. 'bz' => '',
  262. 'sgn' => 'AC9AFD254DE682D8440A97CA68B992DA'
  263. ];
  264. $resp = http_request("https://www.xyzshops.cn/mobile/refill_bjb.php", $params, 'POST');
  265. }
  266. public function testZFKJ()
  267. {
  268. //带签名参数:mchid=1090&act=refill&op=add&cardno=100112121212212133
  269. //&amount=10&order_sn=PH2012261356569433&
  270. //notifyurl=https%3A%2F%2Fqzcz.edusahoo.com.cn%2Findex%2Findex%2Fcallback
  271. //&sign=4a3ac5f9706e64aa70c6cab0fc5839d3
  272. $params = ['mchid' => 1090,
  273. 'cardno' => '100112121212212133',
  274. 'amount' => '10',
  275. "act" => "refill",
  276. "op" => "add",
  277. 'order_sn' => 'PH2012261356569433',
  278. 'notifyurl' => 'https://qzcz.edusahoo.com.cn/index/index/callback'];
  279. $resp = $this->send_md5(BASE_SITE_URL . '/mobile/index.php', $params);
  280. }
  281. protected function check_empty($value)
  282. {
  283. if (!isset($value))
  284. return true;
  285. if ($value === null)
  286. return true;
  287. if (trim($value) === "")
  288. return true;
  289. return false;
  290. }
  291. private function md5_sign($params)
  292. {
  293. ksort($params);
  294. $body = "";
  295. $i = 0;
  296. foreach ($params as $k => $v) {
  297. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  298. if ($i == 0) {
  299. $body .= "{$k}" . "=" . urldecode($v);
  300. } else {
  301. $body .= "&" . "{$k}" . "=" . urldecode($v);
  302. }
  303. $i++;
  304. }
  305. }
  306. $body .= "&key=7yDCLS6S2KzSAJQOUc3vsa";
  307. return md5($body);
  308. }
  309. public function testCallMech()
  310. {
  311. $logic = Logic('queue');
  312. $logic->NotifyMerchantComplete(['order_id' => 289]);
  313. }
  314. private function send($url, $params)
  315. {
  316. $mchid = $params['mchid'];
  317. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  318. $key = file_get_contents($pri_key);
  319. $pri = openssl_get_privatekey($key);
  320. $body = $this->body($params);
  321. openssl_sign($body, $signed, $pri);
  322. $sign = base64_encode($signed);
  323. $params['sign'] = $sign;
  324. $resp = http_request($url, $params, 'POST');
  325. Log::record("resp:{$resp}", Log::DEBUG);
  326. }
  327. private function send_md5($url, $params)
  328. {
  329. $body = $this->body($params);
  330. $body .= "&key={$this->mKey}";
  331. $params['sign'] = md5($body);
  332. $resp = http_request($url, $params, 'POST');
  333. Log::record("resp:{$resp}", Log::DEBUG);
  334. return $resp;
  335. }
  336. public function testCardType()
  337. {
  338. $cardno = '1000111100021211884';
  339. $ret = preg_match('/^1[0-9]{18}$/', $cardno, $matches);
  340. }
  341. public function testMtrand()
  342. {
  343. }
  344. public function testHttpRefill()
  345. {
  346. $mchid = '000001';
  347. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  348. $key = file_get_contents($pri_key);
  349. $pri = openssl_get_privatekey($key);
  350. $params = ['MCHID' => $mchid, 'cardno' => '1000111100021211884', 'amt' => "100", "act" => "refill", "op" => "addoil"];
  351. $body = $this->body($params);
  352. openssl_sign($body, $signed, $pri);
  353. $sign = base64_encode($signed);
  354. $params['sign'] = $sign;
  355. $resp = http_request(BASE_SITE_URL . "/mobile/index.php", $params, 'POST');
  356. }
  357. private function body($params)
  358. {
  359. ksort($params);
  360. $body = "";
  361. $i = 0;
  362. foreach ($params as $k => $v) {
  363. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  364. if ($i == 0) {
  365. $body .= "{$k}" . "=" . urlencode($v);
  366. } else {
  367. $body .= "&" . "{$k}" . "=" . urlencode($v);
  368. }
  369. $i++;
  370. }
  371. }
  372. return $body;
  373. }
  374. private function checkEmpty($value)
  375. {
  376. if (!isset($value))
  377. return true;
  378. if ($value === null)
  379. return true;
  380. if (trim($value) === "")
  381. return true;
  382. return false;
  383. }
  384. public function testKsort()
  385. {
  386. $age = ["Peter" => "35", "Ben" => "37", "Joe" => "43"];
  387. ksort($age);
  388. foreach ($age as $x => $x_value) {
  389. echo "Key=" . $x . ", Value=" . $x_value;
  390. echo "<br>";
  391. }
  392. }
  393. public function testip()
  394. {
  395. $model_merchant = Model('merchant');
  396. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => 1]);
  397. $ipwhitelist = unserialize($merchant_info['ip_white_list']);
  398. $res = json_encode($ipwhitelist);
  399. }
  400. //测试向内部店提交订单,可先屏蔽其它充值通道
  401. public function testAddoilToInnerStore()
  402. {
  403. $url = $this->mReqHost . "/mobile/index.php";
  404. $notifyurl = BASE_SITE_URL . '/mobile/signature.php';
  405. $params = ['mchid' => 1,
  406. 'cardno' => '1000111100020445281',
  407. 'amount' => "1000",
  408. "act" => "refill",
  409. "op" => "add",
  410. 'card_name' => '江海苗',
  411. 'order_sn' => $this->make_sn(),
  412. 'notifyurl' => $notifyurl];
  413. $resp = $this->send_md5($url, $params);
  414. Log::record($resp, Log::DEBUG);
  415. }
  416. public function testRefillInnerCB()
  417. {
  418. $params = ['state_type' => 'order_cancel','order_id' => 311,'channel_name' => 'lx'];
  419. $resp = http_request(BASE_SITE_URL . '/mobile/refill_inner.php',$params,'POST');
  420. }
  421. }