TestRefill.php 10 KB

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