TestRefill.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. } 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 testRefillPhone()
  70. {
  71. global $config;
  72. $providers = new \refill\bjb\RefillPhone($config['phone_providers']['bjb']);
  73. $resp = $providers->add(18500608333, 5, 50, ['order_sn' => '200229600551218886']);
  74. //{"ack":"success","message":{"order_number":13281474,"charged_amount":"48.750","shipping_status":"5","shipping_status_desc":"未发货"}}
  75. //$_POST='{"order_number":13281474,"shipping_status":1,"shipping_status_desc":"已发货","shipping_status_message":"","sign":"05863f9931ed69a70e456222f057dfdd","voucher":"110103307162012081746340295254","vouchertype":"","voucherurl":"","tradeNo":"200649600557718888"}';
  76. $params['order_number'] = 15566432;
  77. $params['shipping_status'] = 1;
  78. $params['shipping_status_desc'] = '已发货';
  79. $params['shipping_status_message'] = '';
  80. $params['sign'] = '53aaf3dee6b0dd17b081073a0df7019c';
  81. $params['voucher'] = '110103307162012081746340295254';
  82. $params['vouchertype'] = '';
  83. $params['voucherurl'] = '';
  84. $params['tradeNo'] = '200649600557718887';
  85. // $ret = refill\RefillFactory::instance()->notify('beixt',$params);
  86. }
  87. public function testBdtOil()
  88. {
  89. $providers = new \refill\bdt\RefillOil([]);
  90. $resp = $providers->add(1000111100021211884,2,100,['order_sn' => '200229600556618886']);
  91. }
  92. public function testBxtwtCB()
  93. {
  94. $data = '{"order_number":18219726,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"863b4d972f2a4d39a9af7396879116c4","voucher":"","vouchertype":"","voucherurl":"","tradeNo":"100662475851197741"}';
  95. }
  96. public function testSuhcJDVerify()
  97. {
  98. $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';
  99. $url = "http://192.168.1.220/mobile/refill_suhc.php?" . $data;
  100. $resp = http_request($url);
  101. }
  102. public function testSuhcTMVerify()
  103. {
  104. $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';
  105. // $url = "http://192.168.1.220/mobile/refill_suhctm.php?" . $data;
  106. $url = "https://www.xyzshops.cn/mobile/refill_suhctm.php?" . $data;
  107. $resp = http_request($url);
  108. }
  109. public function testBeixtVerify()
  110. {
  111. $data = '{"order_number":18219726,"shipping_status":4,"shipping_status_desc":"发货失败","shipping_status_message":"","sign":"863b4d972f2a4d39a9af7396879116c4","voucher":"","vouchertype":"","voucherurl":"","tradeNo":"100662475851197741"}';
  112. $url = "https://www.xyzshops.cn/mobile/refill_bxtwt.php";
  113. $headers = ['Content-Type: application/json'];
  114. $resp = http_post_data($url, $data, $headers);
  115. }
  116. public function testInput()
  117. {
  118. $input = fopen("php://input", "rw");
  119. file_put_contents($input, 'xxxx');
  120. $content = file_get_contents('php://input');
  121. }
  122. public function testUrl()
  123. {
  124. $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';
  125. $params = preg_split('/&/', $data);
  126. foreach ($params as $pair) {
  127. $kv = explode('=', $pair);
  128. $count = count($kv);
  129. if ($count === 1) {
  130. $key = $kv[0];
  131. $val = "";
  132. } elseif ($count === 2) {
  133. $key = $kv[0];
  134. $val = $kv[1];
  135. } else {
  136. continue;
  137. }
  138. Log::record("{$key}:{$val}", Log::DEBUG);
  139. }
  140. }
  141. public function testAddoil()
  142. {
  143. $url = $this->mReqHost . "/mobile/index.php";
  144. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  145. $params = ['mchid' => 1092,
  146. 'cardno' => '1000111100020445281',
  147. 'amount' => "100",
  148. "act" => "refill",
  149. "op" => "add",
  150. 'order_sn' => $this->make_sn(),
  151. 'notifyurl' => $notifyurl];
  152. $resp = $this->send_md5($url, $params);
  153. Log::record($resp, Log::DEBUG);
  154. }
  155. public function testAddPhoe()
  156. {
  157. $phones = [//13911129867,
  158. 18500608333,
  159. 18510683168,
  160. 18513846008,
  161. 18518237398];
  162. $amount = 10;
  163. for ($i = 0; $i < 3; $i++) {
  164. foreach ($phones as $phone) {
  165. $url = $this->mReqHost . "/mobile/index.php";
  166. $params = ['mchid' => 1092,
  167. 'cardno' => $phone,
  168. 'amount' => $amount,
  169. "act" => "refill",
  170. "op" => "add",
  171. 'order_sn' => $this->make_sn(),
  172. 'notifyurl' => $this->mReqHost . "/mobile/refill_xyz.php"];
  173. $resp = $this->send_md5($url, $params);
  174. Log::record($resp, Log::DEBUG);
  175. }
  176. }
  177. }
  178. public function testGoods()
  179. {
  180. $req_url = $this->mReqHost . '/mobile/index.php';
  181. $params = ['mchid' => $this->mMchid,
  182. "act" => "refill",
  183. "op" => "goods"];
  184. $resp = $this->send_md5($req_url, $params);
  185. }
  186. public function testQueryFactory()
  187. {
  188. refill\RefillFactory::instance()->query(295);
  189. }
  190. public function testQuery()
  191. {
  192. //$req_url = 'https://www.xyzshops.cn/mobile/index.php';
  193. // $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  194. $req_url = BASE_SITE_URL . '/mobile/index.php';
  195. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  196. $params = ['mchid' => 1,
  197. "act" => "refill",
  198. "op" => "query",
  199. 'order_sn' => "13281476"];
  200. $resp = $this->send_md5($req_url, $params);
  201. }
  202. public function testRemoveSession()
  203. {
  204. $resp = http_request(BASE_SITE_URL . "/mobile/signature.php", [], 'POST');
  205. }
  206. public function testSuhcCB()
  207. {
  208. // $notifyurl = BASE_SITE_URL . "/mobile/refill_suhc.php";
  209. $notifyurl = "https://www.xyzshops.cn/mobile/refill_suhc.php";
  210. // $notifyurl = "https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php";
  211. $params = ['onlystr' => '920660917672249741',
  212. 'amt' => 100,
  213. 'jdno' => '',
  214. 'notifyurl' => $notifyurl,
  215. 'cardtype' => 'Sinoepc',
  216. 'batchid' => 18512,
  217. 'cardno' => '1000111100020445281',
  218. 'orgid' => '1590993600',
  219. 'status' => 2];
  220. $sign = $this->md5_sign($params);
  221. $params['sign'] = $sign;
  222. $resp = http_request($notifyurl, $params, 'POST');
  223. Log::record($resp, Log::DEBUG);
  224. }
  225. public function testZFKJ()
  226. {
  227. //带签名参数:mchid=1090&act=refill&op=add&cardno=100112121212212133
  228. //&amount=10&order_sn=PH2012261356569433&
  229. //notifyurl=https%3A%2F%2Fqzcz.edusahoo.com.cn%2Findex%2Findex%2Fcallback
  230. //&sign=4a3ac5f9706e64aa70c6cab0fc5839d3
  231. $params = ['mchid' => 1090,
  232. 'cardno' => '100112121212212133',
  233. 'amount' => '10',
  234. "act" => "refill",
  235. "op" => "add",
  236. 'order_sn' => 'PH2012261356569433',
  237. 'notifyurl' => 'https://qzcz.edusahoo.com.cn/index/index/callback'];
  238. $resp = $this->send_md5(BASE_SITE_URL . '/mobile/index.php', $params);
  239. }
  240. protected function check_empty($value)
  241. {
  242. if (!isset($value))
  243. return true;
  244. if ($value === null)
  245. return true;
  246. if (trim($value) === "")
  247. return true;
  248. return false;
  249. }
  250. private function md5_sign($params)
  251. {
  252. ksort($params);
  253. $body = "";
  254. $i = 0;
  255. foreach ($params as $k => $v) {
  256. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1)) {
  257. if ($i == 0) {
  258. $body .= "{$k}" . "=" . urldecode($v);
  259. } else {
  260. $body .= "&" . "{$k}" . "=" . urldecode($v);
  261. }
  262. $i++;
  263. }
  264. }
  265. $body .= "&key=7yDCLS6S2KzSAJQOUc3vsa";
  266. return md5($body);
  267. }
  268. public function testCallMech()
  269. {
  270. $logic = Logic('queue');
  271. $logic->NotifyMerchantComplete(['order_id' => 289]);
  272. }
  273. private function send($url, $params)
  274. {
  275. $mchid = $params['mchid'];
  276. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  277. $key = file_get_contents($pri_key);
  278. $pri = openssl_get_privatekey($key);
  279. $body = $this->body($params);
  280. openssl_sign($body, $signed, $pri);
  281. $sign = base64_encode($signed);
  282. $params['sign'] = $sign;
  283. $resp = http_request($url, $params, 'POST');
  284. Log::record("resp:{$resp}", Log::DEBUG);
  285. }
  286. private function send_md5($url, $params)
  287. {
  288. $body = $this->body($params);
  289. $body .= "&key={$this->mKey}";
  290. // $body .= "&key=ZhongFKJZhongFKJZhongFKJ";
  291. $params['sign'] = md5($body);
  292. $resp = http_request($url, $params, 'POST');
  293. Log::record("resp:{$resp}", Log::DEBUG);
  294. return $resp;
  295. }
  296. public function testCardType()
  297. {
  298. $cardno = '1000111100021211884';
  299. $ret = preg_match('/^1[0-9]{18}$/', $cardno, $matches);
  300. }
  301. public function testMtrand()
  302. {
  303. }
  304. public function testHttpRefill()
  305. {
  306. $mchid = '000001';
  307. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  308. $key = file_get_contents($pri_key);
  309. $pri = openssl_get_privatekey($key);
  310. $params = ['MCHID' => $mchid, 'cardno' => '1000111100021211884', 'amt' => "100", "act" => "refill", "op" => "addoil"];
  311. $body = $this->body($params);
  312. openssl_sign($body, $signed, $pri);
  313. $sign = base64_encode($signed);
  314. $params['sign'] = $sign;
  315. $resp = http_request(BASE_SITE_URL . "/mobile/index.php", $params, 'POST');
  316. }
  317. private function body($params)
  318. {
  319. ksort($params);
  320. $body = "";
  321. $i = 0;
  322. foreach ($params as $k => $v) {
  323. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  324. if ($i == 0) {
  325. $body .= "{$k}" . "=" . urlencode($v);
  326. } else {
  327. $body .= "&" . "{$k}" . "=" . urlencode($v);
  328. }
  329. $i++;
  330. }
  331. }
  332. return $body;
  333. }
  334. private function checkEmpty($value)
  335. {
  336. if (!isset($value))
  337. return true;
  338. if ($value === null)
  339. return true;
  340. if (trim($value) === "")
  341. return true;
  342. return false;
  343. }
  344. public function testKsort()
  345. {
  346. $age = ["Peter" => "35", "Ben" => "37", "Joe" => "43"];
  347. ksort($age);
  348. foreach ($age as $x => $x_value) {
  349. echo "Key=" . $x . ", Value=" . $x_value;
  350. echo "<br>";
  351. }
  352. }
  353. public function testip()
  354. {
  355. $model_merchant = Model('merchant');
  356. $merchant_info = $model_merchant->getMerchantInfo(['mchid' => 1]);
  357. $ipwhitelist = unserialize($merchant_info['ip_white_list']);
  358. $res = json_encode($ipwhitelist);
  359. }
  360. }