AopF2FGatewayTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <?php
  2. namespace Omnipay\Alipay\Tests;
  3. use Omnipay\Alipay\AopF2FGateway;
  4. use Omnipay\Alipay\Common\Signer;
  5. use Omnipay\Alipay\Responses\AopCompletePurchaseResponse;
  6. use Omnipay\Alipay\Responses\AopCompleteRefundResponse;
  7. use Omnipay\Alipay\Responses\AopTradePayResponse;
  8. use Omnipay\Alipay\Responses\AopTradePreCreateResponse;
  9. use Omnipay\Alipay\Responses\AopTradeQueryResponse;
  10. use Omnipay\Alipay\Responses\AopTradeRefundQueryResponse;
  11. use Omnipay\Alipay\Responses\AopTradeRefundResponse;
  12. use Omnipay\Alipay\Responses\DataServiceBillDownloadUrlQueryResponse;
  13. class AopF2FGatewayTest extends AbstractGatewayTestCase
  14. {
  15. /**
  16. * @var AopF2FGateway $gateway
  17. */
  18. protected $gateway;
  19. protected $options;
  20. public function setUp()
  21. {
  22. parent::setUp();
  23. $this->gateway = new AopF2FGateway($this->getHttpClient(), $this->getHttpRequest());
  24. $this->gateway->setAppId($this->appId);
  25. $this->gateway->setPrivateKey($this->appPrivateKey);
  26. $this->gateway->setEncryptKey($this->appEncryptKey);
  27. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  28. $this->gateway->sandbox();
  29. }
  30. public function testCapture()
  31. {
  32. $this->setMockHttpResponse('AopF2F_Capture_Failure.txt');
  33. /**
  34. * @var AopTradePayResponse $response
  35. */
  36. $response = $this->gateway->capture(
  37. [
  38. 'biz_content' => [
  39. 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  40. 'scene' => 'bar_code',
  41. 'auth_code' => '288412621343841260',
  42. 'subject' => 'test',
  43. 'total_amount' => '0.01',
  44. ]
  45. ]
  46. )->setPollingAttempts(1)->send();
  47. $this->assertArrayHasKey('alipay_trade_pay_response', $response->getData());
  48. $this->assertFalse($response->isSuccessful());
  49. }
  50. public function testPurchase()
  51. {
  52. $this->setMockHttpResponse('AopF2F_Purchase_Failure.txt');
  53. /**
  54. * @var AopTradePreCreateResponse $response
  55. */
  56. $response = $this->gateway->purchase(
  57. [
  58. 'biz_content' => [
  59. 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  60. 'subject' => 'test',
  61. 'total_amount' => '0.01',
  62. ]
  63. ]
  64. )->send();
  65. $this->assertArrayHasKey('alipay_trade_precreate_response', $response->getData());
  66. $this->assertFalse($response->isSuccessful());
  67. $this->assertFalse($response->isRedirect());
  68. }
  69. public function testQuery()
  70. {
  71. $this->setMockHttpResponse('AopF2F_Query_Failure.txt');
  72. /**
  73. * @var AopTradeQueryResponse $response
  74. */
  75. $response = $this->gateway->query(
  76. [
  77. 'biz_content' => [
  78. 'out_trade_no' => '201609220542532413'
  79. ]
  80. ]
  81. )->send();
  82. $this->assertArrayHasKey('alipay_trade_query_response', $response->getData());
  83. $this->assertFalse($response->isSuccessful());
  84. }
  85. public function testRefund()
  86. {
  87. $this->setMockHttpResponse('AopF2F_Refund_Failure.txt');
  88. /**
  89. * @var AopTradeRefundResponse $response
  90. */
  91. $response = $this->gateway->refund(
  92. [
  93. 'biz_content' => [
  94. 'refund_amount' => '10.01',
  95. 'out_trade_no' => '201609220542532413'
  96. ]
  97. ]
  98. )->send();
  99. $this->assertArrayHasKey('alipay_trade_refund_response', $response->getData());
  100. $this->assertFalse($response->isSuccessful());
  101. }
  102. public function testQueryRefund()
  103. {
  104. $this->setMockHttpResponse('AopF2F_QueryRefund_Failure.txt');
  105. /**
  106. * @var AopTradeRefundQueryResponse $response
  107. */
  108. $response = $this->gateway->refundQuery(
  109. [
  110. 'biz_content' => [
  111. 'refund_amount' => '10.01',
  112. 'out_trade_no' => '201609220542532412',
  113. 'out_request_no' => '201609220542532412'
  114. ]
  115. ]
  116. )->send();
  117. $this->assertArrayHasKey('alipay_trade_fastpay_refund_query_response', $response->getData());
  118. $this->assertFalse($response->isSuccessful());
  119. }
  120. public function testSettle()
  121. {
  122. $this->setMockHttpResponse('AopF2F_Settle_Failure.txt');
  123. /**
  124. * @var AopTradeRefundQueryResponse $response
  125. */
  126. $response = $this->gateway->settle(
  127. [
  128. 'biz_content' => [
  129. 'out_request_no' => '201609220542532412',
  130. 'trade_no' => '2014030411001007850000672009',
  131. 'royalty_parameters' => [
  132. [
  133. 'trans_out' => '111111',
  134. 'trans_in' => '222222',
  135. 'amount' => '0.01',
  136. ],
  137. [
  138. 'trans_out' => '111111',
  139. 'trans_in' => '333333',
  140. 'amount' => '0.02',
  141. ]
  142. ]
  143. ]
  144. ]
  145. )->send();
  146. $this->assertArrayHasKey('alipay_trade_order_settle_response', $response->getData());
  147. $this->assertFalse($response->isSuccessful());
  148. }
  149. public function testQueryBillDownloadUrl()
  150. {
  151. $this->setMockHttpResponse('AopF2F_QueryBillDownloadUrl_Failure.txt');
  152. /**
  153. * @var DataServiceBillDownloadUrlQueryResponse $response
  154. */
  155. $response = $this->gateway->queryBillDownloadUrl(
  156. [
  157. 'biz_content' => [
  158. 'bill_type' => 'trade',
  159. 'bill_date' => '1999-04-05',
  160. ]
  161. ]
  162. )->send();
  163. $this->assertArrayHasKey('alipay_data_dataservice_bill_downloadurl_query_response', $response->getData());
  164. $this->assertFalse($response->isSuccessful());
  165. }
  166. public function testCompletePurchase()
  167. {
  168. $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
  169. $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key.pem';
  170. $this->gateway = new AopF2FGateway($this->getHttpClient(), $this->getHttpRequest());
  171. $this->gateway->setAppId($this->appId);
  172. $this->gateway->setPrivateKey($this->appPrivateKey);
  173. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  174. $str = 'gmt_payment=2015-06-11 22:33:59&notify_id=42af7baacd1d3746cf7b56752b91edcj34&seller_email=testyufabu07@alipay.com&notify_type=trade_status_sync&sign=kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=&trade_no=2015061121001004400068549373&out_trade_no=21repl2ac2eOutTradeNo322&gmt_create=2015-06-11 22:33:46&seller_id=2088211521646673&notify_time=2015-06-11 22:34:03&subject=FACE_TO_FACE_PAYMENT_PRECREATE中文&trade_status=TRADE_SUCCESS&sign_type=RSA';
  175. parse_str($str, $data);
  176. $signer = new Signer($data);
  177. $signer->setSort(true);
  178. $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
  179. $data['sign'] = $signer->signWithRSA($testPrivateKey);
  180. $data['sign_type'] = 'RSA';
  181. $this->gateway->setAlipayPublicKey($testPublicKey);
  182. /**
  183. * @var AopCompletePurchaseResponse $response
  184. */
  185. $response = $this->gateway->completePurchase(['params' => $data])->send();
  186. $this->assertEquals(
  187. '{"gmt_payment":"2015-06-11 22:33:59","notify_id":"42af7baacd1d3746cf7b56752b91edcj34","seller_email":"testyufabu07@alipay.com","notify_type":"trade_status_sync","sign":"T4JCUXoO5sK\/7UjupKEfsSQnjDnw\/1aSJnC6s53SYJyqdjFl+1Lt8dWdNuuXl5yX39leQsYzmk2CDwZx6F\/YIQWCo1LHZME3DYMqH\/F5wT5uiSUk2KYsYbLluW9pi7YHtBXRWKB6jtnn73DWWbC2sN3tDky9KySPizL5jQ1Cd0I=","trade_no":"2015061121001004400068549373","out_trade_no":"21repl2ac2eOutTradeNo322","gmt_create":"2015-06-11 22:33:46","seller_id":"2088211521646673","notify_time":"2015-06-11 22:34:03","subject":"FACE_TO_FACE_PAYMENT_PRECREATE\u4e2d\u6587","trade_status":"TRADE_SUCCESS","sign_type":"RSA"}',
  188. json_encode($response->data())
  189. );
  190. $this->assertEquals('21repl2ac2eOutTradeNo322', $response->data('out_trade_no'));
  191. $this->assertTrue($response->isSuccessful());
  192. $this->assertTrue($response->isPaid());
  193. $this->assertEquals('2015061121001004400068549373', $response->getData()['trade_no']);
  194. }
  195. public function testCompleteRefund()
  196. {
  197. $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
  198. $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key.pem';
  199. $this->gateway = new AopF2FGateway($this->getHttpClient(), $this->getHttpRequest());
  200. $this->gateway->setAppId($this->appId);
  201. $this->gateway->setPrivateKey($this->appPrivateKey);
  202. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  203. $str = 'gmt_payment=2015-06-11 22:33:59&notify_id=42af7baacd1d3746cf7b56752b91edcj34&seller_email=testyufabu07@alipay.com&notify_type=trade_status_sync&sign=kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=&trade_no=2015061121001004400068549373&out_trade_no=21repl2ac2eOutTradeNo322&gmt_create=2015-06-11 22:33:46&seller_id=2088211521646673&notify_time=2015-06-11 22:34:03&subject=FACE_TO_FACE_PAYMENT_PRECREATE中文&trade_status=TRADE_SUCCESS&sign_type=RSA';
  204. parse_str($str, $data);
  205. $signer = new Signer($data);
  206. $signer->setSort(true);
  207. $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
  208. $data['sign'] = $signer->signWithRSA($testPrivateKey);
  209. $data['sign_type'] = 'RSA';
  210. $this->gateway->setAlipayPublicKey($testPublicKey);
  211. /**
  212. * @var AopCompleteRefundResponse $response
  213. */
  214. $response = $this->gateway->completeRefund(['params' => $data])->send();
  215. $this->assertEquals(
  216. '{"gmt_payment":"2015-06-11 22:33:59","notify_id":"42af7baacd1d3746cf7b56752b91edcj34","seller_email":"testyufabu07@alipay.com","notify_type":"trade_status_sync","sign":"T4JCUXoO5sK\/7UjupKEfsSQnjDnw\/1aSJnC6s53SYJyqdjFl+1Lt8dWdNuuXl5yX39leQsYzmk2CDwZx6F\/YIQWCo1LHZME3DYMqH\/F5wT5uiSUk2KYsYbLluW9pi7YHtBXRWKB6jtnn73DWWbC2sN3tDky9KySPizL5jQ1Cd0I=","trade_no":"2015061121001004400068549373","out_trade_no":"21repl2ac2eOutTradeNo322","gmt_create":"2015-06-11 22:33:46","seller_id":"2088211521646673","notify_time":"2015-06-11 22:34:03","subject":"FACE_TO_FACE_PAYMENT_PRECREATE\u4e2d\u6587","trade_status":"TRADE_SUCCESS","sign_type":"RSA"}',
  217. json_encode($response->data())
  218. );
  219. $this->assertEquals('21repl2ac2eOutTradeNo322', $response->data('out_trade_no'));
  220. $this->assertTrue($response->isSuccessful());
  221. $this->assertTrue($response->isRefunded());
  222. $this->assertEquals('2015061121001004400068549373', $response->getData()['trade_no']);
  223. }
  224. }