AopAppGatewayTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. namespace Omnipay\Alipay\Tests;
  3. use Omnipay\Alipay\AopAppGateway;
  4. use Omnipay\Alipay\Common\Signer;
  5. use Omnipay\Alipay\Responses\AopCompletePurchaseResponse;
  6. use Omnipay\Alipay\Responses\AopCompleteRefundResponse;
  7. use Omnipay\Alipay\Responses\AopTradeAppPayResponse;
  8. use Omnipay\Common\Exception\InvalidRequestException;
  9. class AopAppGatewayTest extends AbstractGatewayTestCase
  10. {
  11. /**
  12. * @var AopAppGateway $gateway
  13. */
  14. protected $gateway;
  15. protected $options;
  16. public function setUp()
  17. {
  18. parent::setUp();
  19. $this->gateway = new AopAppGateway($this->getHttpClient(), $this->getHttpRequest());
  20. $this->gateway->setAppId($this->appId);
  21. $this->gateway->setPrivateKey(ALIPAY_AOP_PRIVATE_KEY);
  22. //$this->gateway->setAlipayPublicKey(file_get_contents($this->alipayPublicKey));
  23. //$this->gateway->setEncryptKey($this->appEncryptKey);
  24. //$this->gateway->setNotifyUrl('http://www.guoshuzc.com/api/pay/alipay_recharge_notify');
  25. //$this->gateway->setAlipaySdk('alipay_sdk');
  26. }
  27. public function testPurchase()
  28. {
  29. /**
  30. * @var AopTradeAppPayResponse $response
  31. */
  32. $response = $this->gateway->purchase(
  33. [
  34. 'biz_content' => [
  35. 'subject' => 'test',
  36. 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  37. 'total_amount' => '0.01',
  38. 'product_code' => 'QUICK_MSECURITY_PAY',
  39. ]
  40. ]
  41. )->send();
  42. $this->assertTrue($response->isSuccessful());
  43. $this->assertFalse($response->isRedirect());
  44. $this->assertNotEmpty($response->getOrderString());
  45. }
  46. public function testPurchaseUseCert()
  47. {
  48. $this->gateway->setAppId('2016101900723821');
  49. $this->gateway->setSignType('RSA2');
  50. $this->gateway->setPrivateKey(ALIPAY_ASSET_DIR . '/dist/cert/appPrivateKey');
  51. $this->gateway->setAlipayRootCert(ALIPAY_ASSET_DIR . '/dist/cert/alipayRootCert.crt');
  52. $this->gateway->setAlipayPublicCert(ALIPAY_ASSET_DIR . '/dist/cert/alipayCertPublicKey_RSA2.crt');
  53. $this->gateway->setAppCert(ALIPAY_ASSET_DIR . '/dist/cert/appCertPublicKey.crt');
  54. $this->gateway->setCheckAlipayPublicCert(true);
  55. /** @var AopTradeAppPayResponse $response */
  56. $response = $this->gateway->purchase()->setBizContent([
  57. 'subject' => 'test',
  58. 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  59. 'total_amount' => '0.01',
  60. 'product_code' => 'QUICK_MSECURITY_PAY',
  61. ])->send();
  62. $this->assertTrue($response->isSuccessful());
  63. $this->assertFalse($response->isRedirect());
  64. $this->assertNotEmpty($response->getOrderString());
  65. }
  66. public function testPurchaseInline()
  67. {
  68. $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key_inline.pem';
  69. $this->gateway->setPrivateKey($testPrivateKey);
  70. /**
  71. * @var AopTradeAppPayResponse $response
  72. */
  73. $response = $this->gateway->purchase(
  74. [
  75. 'biz_content' => [
  76. 'subject' => 'test',
  77. 'out_trade_no' => date('YmdHis') . mt_rand(1000, 9999),
  78. 'total_amount' => '0.01',
  79. 'product_code' => 'QUICK_MSECURITY_PAY',
  80. ]
  81. ]
  82. )->send();
  83. $this->assertTrue($response->isSuccessful());
  84. $this->assertFalse($response->isRedirect());
  85. $this->assertNotEmpty($response->getOrderString());
  86. }
  87. public function testCompletePurchaseNotify()
  88. {
  89. $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
  90. $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key.pem';
  91. $this->gateway = new AopAppGateway($this->getHttpClient(), $this->getHttpRequest());
  92. $this->gateway->setAppId($this->appId);
  93. $this->gateway->setPrivateKey($this->appPrivateKey);
  94. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  95. $str = '{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"vCAj0n6vUVggDzZUqV4P2IucMeguUMaLBl5Uld7PeLHCo74/d3AcWCNCsGDxtW9Jm7+suyo6Y0jRY7OUi0PKZJre84m2q9Oo30AdgbMFRT91uZFYp9miJGWlQWwHhJDo3cU5iAYf5bnPPYgH8073kTFtmDPmrP9pvEUm3lsroUw=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}';
  96. $str = stripslashes($str);
  97. $data = json_decode($str, true);
  98. $signer = new Signer($data);
  99. $signer->setSort(true);
  100. $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
  101. $data['sign'] = $signer->signWithRSA($testPrivateKey);
  102. $data['sign_type'] = 'RSA';
  103. $this->gateway->setAlipayPublicKey($testPublicKey);
  104. try {
  105. /** @var AopCompletePurchaseResponse $response */
  106. $response = $this->gateway->completePurchase()->setParams($data)->send();
  107. } catch (InvalidRequestException $e) {
  108. $this->assertTrue(false);
  109. }
  110. $this->assertEquals(
  111. '{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"Xa2NyOsxOBjW\/q\/RUFZhii2epa4B3ka+2aGsG8knqkiCD8llXrTDm11QtGkSRVw\/hbfcgFPiTkuaKnaaDu\/UfypsVSHToy28PiH5xkBSSd6zHNZCP\/jvjzOa6GPf4tIpfYNVvjaRMRcbn+TRlOFtHOnMMubjsg7K52P+LCugZIA=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}',
  112. json_encode($response->data())
  113. );
  114. $this->assertEquals('2016092313071234567', $response->data('out_trade_no'));
  115. $this->assertTrue($response->isSuccessful());
  116. $this->assertTrue($response->isPaid());
  117. $this->assertEquals('201609232100100306021234567', $response->getData()['trade_no']);
  118. }
  119. public function testCompletePurchaseNotifyWithInlineKey()
  120. {
  121. $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
  122. $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key_inline.pem';
  123. $this->gateway = new AopAppGateway($this->getHttpClient(), $this->getHttpRequest());
  124. $this->gateway->setAppId($this->appId);
  125. $this->gateway->setPrivateKey($this->appPrivateKey);
  126. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  127. $str = '{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"vCAj0n6vUVggDzZUqV4P2IucMeguUMaLBl5Uld7PeLHCo74/d3AcWCNCsGDxtW9Jm7+suyo6Y0jRY7OUi0PKZJre84m2q9Oo30AdgbMFRT91uZFYp9miJGWlQWwHhJDo3cU5iAYf5bnPPYgH8073kTFtmDPmrP9pvEUm3lsroUw=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}';
  128. $str = stripslashes($str);
  129. $data = json_decode($str, true);
  130. $signer = new Signer($data);
  131. $signer->setSort(true);
  132. $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
  133. $data['sign'] = $signer->signWithRSA($testPrivateKey);
  134. $data['sign_type'] = 'RSA';
  135. $this->gateway->setAlipayPublicKey($testPublicKey);
  136. try {
  137. /** @var AopCompletePurchaseResponse $response */
  138. $response = $this->gateway->completePurchase()->setParams($data)->send();
  139. } catch (InvalidRequestException $e) {
  140. $this->assertTrue(false);
  141. }
  142. $this->assertEquals(
  143. '{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"Xa2NyOsxOBjW\/q\/RUFZhii2epa4B3ka+2aGsG8knqkiCD8llXrTDm11QtGkSRVw\/hbfcgFPiTkuaKnaaDu\/UfypsVSHToy28PiH5xkBSSd6zHNZCP\/jvjzOa6GPf4tIpfYNVvjaRMRcbn+TRlOFtHOnMMubjsg7K52P+LCugZIA=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}',
  144. json_encode($response->data())
  145. );
  146. $this->assertEquals('2016092313071234567', $response->data('out_trade_no'));
  147. $this->assertTrue($response->isSuccessful());
  148. $this->assertTrue($response->isPaid());
  149. $this->assertEquals('201609232100100306021234567', $response->getData()['trade_no']);
  150. }
  151. public function testCompleteRefundNotify()
  152. {
  153. $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
  154. $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key_inline.pem';
  155. $this->gateway = new AopAppGateway($this->getHttpClient(), $this->getHttpRequest());
  156. $this->gateway->setAppId($this->appId);
  157. $this->gateway->setPrivateKey($this->appPrivateKey);
  158. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  159. $str = '{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"vCAj0n6vUVggDzZUqV4P2IucMeguUMaLBl5Uld7PeLHCo74/d3AcWCNCsGDxtW9Jm7+suyo6Y0jRY7OUi0PKZJre84m2q9Oo30AdgbMFRT91uZFYp9miJGWlQWwHhJDo3cU5iAYf5bnPPYgH8073kTFtmDPmrP9pvEUm3lsroUw=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}';
  160. $str = stripslashes($str);
  161. $data = json_decode($str, true);
  162. $signer = new Signer($data);
  163. $signer->setSort(true);
  164. $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
  165. $data['sign'] = $signer->signWithRSA($testPrivateKey);
  166. $data['sign_type'] = 'RSA';
  167. $this->gateway->setAlipayPublicKey($testPublicKey);
  168. try {
  169. /** @var AopCompleteRefundResponse $response */
  170. $response = $this->gateway->completeRefund()->setParams($data)->send();
  171. } catch (InvalidRequestException $e) {
  172. // Params error or sign not match.
  173. $response = -1;
  174. }
  175. if ($response->isSuccessful() && $response->isRefunded()) {
  176. // Refund successful
  177. } else {
  178. // Refund not successful
  179. }
  180. $this->assertTrue($response !== -1);
  181. }
  182. }