TestRefill.php 12 KB

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