TestRefill.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. 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. public function testRefillPhone()
  36. {
  37. // global $config;
  38. // $providers = new \refill\beixts\RefillPhone($config['phone_providers']['beixts']);
  39. // $resp = $providers->add(13699279618,4,50,['order_sn' => '200649600557718886']);
  40. //{"ack":"success","message":{"order_number":13281474,"charged_amount":"48.750","shipping_status":"5","shipping_status_desc":"未发货"}}
  41. //$_POST='{"order_number":13281474,"shipping_status":1,"shipping_status_desc":"已发货","shipping_status_message":"","sign":"05863f9931ed69a70e456222f057dfdd","voucher":"110103307162012081746340295254","vouchertype":"","voucherurl":"","tradeNo":"200649600557718888"}';
  42. $params['order_number'] = 15566432;
  43. $params['shipping_status'] = 1;
  44. $params['shipping_status_desc'] = '已发货';
  45. $params['shipping_status_message'] = '';
  46. $params['sign'] = '53aaf3dee6b0dd17b081073a0df7019c';
  47. $params['voucher'] = '110103307162012081746340295254';
  48. $params['vouchertype'] = '';
  49. $params['voucherurl'] = '';
  50. $params['tradeNo'] = '200649600557718887';
  51. $ret = refill\RefillFactory::instance()->notify('beixt',$params);
  52. }
  53. public function testSuhxVerify()
  54. {
  55. $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';
  56. $url = "https://www.xyzshops.cn/mobile/refill_suhc.php?" . $data;
  57. // $url = "http://192.168.1.220/mobile/refill_suhc.php?" . $data;
  58. $resp = http_request($url);
  59. }
  60. public function testBeixtVerify()
  61. {
  62. $data = '{"order_number":17179442,"shipping_status":1,"shipping_status_desc":"已发货","shipping_status_message":"","sign":"4d0d9e22410407a3ef374f70fdbc0425","voucher":"501324954073664276","vouchertype":"","voucherurl":"","tradeNo":"780661942259568741"}';
  63. // $url = "http://192.168.1.220/mobile/refill_beixt.php";
  64. $url = "https://www.xyzshops.cn/mobile/refill_bxtwt.php";
  65. $resp = http_post_data($url,$data);
  66. }
  67. public function testUrl()
  68. {
  69. $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';
  70. $params = preg_split('/&/', $data);
  71. foreach ($params as $pair)
  72. {
  73. $kv = explode('=', $pair);
  74. $count = count($kv);
  75. if($count === 1) {
  76. $key = $kv[0];
  77. $val = "";
  78. }
  79. elseif($count === 2) {
  80. $key = $kv[0];
  81. $val = $kv[1];
  82. }
  83. else {
  84. continue;
  85. }
  86. Log::record("{$key}:{$val}",Log::DEBUG);
  87. }
  88. }
  89. public function testAddoil()
  90. {
  91. $url = $this->mReqHost . "/mobile/index.php";
  92. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  93. $params = ['mchid' => 1,
  94. 'cardno' => '1000111100020445281',
  95. 'amount' => "100",
  96. "act"=>"refill",
  97. "op"=>"add",
  98. 'order_sn' => "13281478",
  99. 'notifyurl'=> $notifyurl];
  100. $resp = $this->send_md5($url,$params);
  101. Log::record($resp,Log::DEBUG);
  102. }
  103. public function testAddPhoe()
  104. {
  105. $url = $this->mReqHost . "/mobile/index.php";
  106. $params = ['mchid' => 1,
  107. 'cardno' => '15120035568',
  108. 'amount' => "50",
  109. "act"=>"refill",
  110. "op"=>"add",
  111. 'order_sn' => "13281475",
  112. 'notifyurl'=> $this->mReqHost . "/mobile/beixt_notify.php"];
  113. $resp = $this->send_md5($url,$params);
  114. Log::record($resp,Log::DEBUG);
  115. }
  116. public function testGoods()
  117. {
  118. // $req_url = 'https://www.xyzshops.cn/mobile/index.php';
  119. // $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  120. $req_url = BASE_SITE_URL . '/mobile/index.php';
  121. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  122. $params = ['mchid' => 1,
  123. "act"=>"refill",
  124. "op"=>"goods"];
  125. $resp = $this->send_md5($req_url,$params);
  126. }
  127. public function testQueryFactory()
  128. {
  129. refill\RefillFactory::instance()->query(295);
  130. }
  131. public function testQuery()
  132. {
  133. //$req_url = 'https://www.xyzshops.cn/mobile/index.php';
  134. // $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  135. $req_url = BASE_SITE_URL . '/mobile/index.php';
  136. $notifyurl = 'https://www.xyzshops.cn/mobile/signature.php';
  137. $params = ['mchid' => 1,
  138. "act"=>"refill",
  139. "op"=>"query",
  140. 'order_sn' => "13281476"];
  141. $resp = $this->send_md5($req_url,$params);
  142. }
  143. public function testRemoveSession()
  144. {
  145. $resp = http_request(BASE_SITE_URL . "/mobile/signature.php",[],'POST'); }
  146. public function testSuhcCB()
  147. {
  148. // $notifyurl = BASE_SITE_URL . "/mobile/refill_suhc.php";
  149. $notifyurl = "https://www.xyzshops.cn/mobile/refill_suhc.php";
  150. // $notifyurl = "https%3A%2F%2Fwww.xyzshops.cn%2Fmobile%2Frefill_suhc.php";
  151. $params = ['onlystr' => '920660917672249741',
  152. 'amt' => 100,
  153. 'jdno'=>'',
  154. 'notifyurl' => $notifyurl,
  155. 'cardtype' => 'Sinoepc',
  156. 'batchid' => 18512,
  157. 'cardno' => '1000111100020445281',
  158. 'orgid' => '1590993600',
  159. 'status' => 2];
  160. $sign = $this->md5_sign($params);
  161. $params['sign'] = $sign;
  162. $resp = http_request($notifyurl,$params,'POST');
  163. Log::record($resp,Log::DEBUG);
  164. }
  165. protected function check_empty($value)
  166. {
  167. if (!isset($value))
  168. return true;
  169. if ($value === null)
  170. return true;
  171. if (trim($value) === "")
  172. return true;
  173. return false;
  174. }
  175. private function md5_sign($params)
  176. {
  177. ksort($params);
  178. $body = "";
  179. $i = 0;
  180. foreach ($params as $k => $v)
  181. {
  182. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  183. {
  184. if ($i == 0) {
  185. $body .= "{$k}" . "=" . urldecode($v);
  186. } else {
  187. $body .= "&" . "{$k}" . "=" . urldecode($v);
  188. }
  189. $i++;
  190. }
  191. }
  192. $body .= "&key=7yDCLS6S2KzSAJQOUc3vsa";
  193. return md5($body);
  194. }
  195. public function testCallMech()
  196. {
  197. $logic = Logic('queue');
  198. $logic->NotifyMerchantComplete(['order_id' => 289]);
  199. }
  200. private function send($url,$params)
  201. {
  202. $mchid = $params['mchid'];
  203. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  204. $key = file_get_contents($pri_key);
  205. $pri = openssl_get_privatekey($key);
  206. $body = $this->body($params);
  207. openssl_sign($body, $signed, $pri);
  208. $sign = base64_encode($signed);
  209. $params['sign'] = $sign;
  210. $resp = http_request($url,$params,'POST');
  211. Log::record("resp:{$resp}",Log::DEBUG);
  212. }
  213. private function send_md5($url,$params)
  214. {
  215. $body = $this->body($params);
  216. $body .= "&key=1ff02223b771c0414468c8892151c602";
  217. $params['sign'] = md5($body);
  218. $resp = http_request($url,$params,'POST');
  219. Log::record("resp:{$resp}",Log::DEBUG);
  220. return $resp;
  221. }
  222. public function testCardType()
  223. {
  224. $cardno = '1000111100021211884';
  225. $ret = preg_match( '/^1[0-9]{18}$/',$cardno,$matches);
  226. }
  227. public function testMtrand()
  228. {
  229. }
  230. public function testHttpRefill()
  231. {
  232. $mchid = '000001';
  233. $pri_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pri.pem";
  234. $key = file_get_contents($pri_key);
  235. $pri = openssl_get_privatekey($key);
  236. $params = ['MCHID' => $mchid,'cardno' => '1000111100021211884','amt' => "100","act"=>"refill","op"=>"addoil"];
  237. $body = $this->body($params);
  238. openssl_sign($body, $signed, $pri);
  239. $sign = base64_encode($signed);
  240. $params['sign'] = $sign;
  241. $resp = http_request(BASE_SITE_URL . "/mobile/index.php",$params,'POST');
  242. }
  243. private function body($params)
  244. {
  245. ksort($params);
  246. $stringToBeSigned = "";
  247. $i = 0;
  248. foreach ($params as $k => $v)
  249. {
  250. if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1))
  251. {
  252. if ($i == 0) {
  253. $stringToBeSigned .= "{$k}" . "=" . urlencode($v);
  254. } else {
  255. $stringToBeSigned .= "&" . "{$k}" . "=" . urlencode($v);
  256. }
  257. $i++;
  258. }
  259. }
  260. return $stringToBeSigned;
  261. }
  262. private function checkEmpty($value)
  263. {
  264. if (!isset($value))
  265. return true;
  266. if ($value === null)
  267. return true;
  268. if (trim($value) === "")
  269. return true;
  270. return false;
  271. }
  272. public function testKsort()
  273. {
  274. $age= ["Peter"=>"35","Ben"=>"37","Joe"=>"43"];
  275. ksort($age);
  276. foreach($age as $x=>$x_value)
  277. {
  278. echo "Key=" . $x . ", Value=" . $x_value;
  279. echo "<br>";
  280. }
  281. }
  282. public function testip(){
  283. $model_merchant = Model('merchant');
  284. $merchant_info = $model_merchant->getMerchantInfo(array('mchid' => 1));
  285. $ipwhitelist = unserialize($merchant_info['ip_white_list']);
  286. $res = json_encode($ipwhitelist);
  287. }
  288. }