LegacyExpressGatewayTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Omnipay\Alipay\Tests;
  3. use Omnipay\Alipay\Common\Signer;
  4. use Omnipay\Alipay\LegacyExpressGateway;
  5. use Omnipay\Alipay\Responses\LegacyExpressPurchaseResponse;
  6. use Omnipay\Alipay\Responses\LegacyQueryResponse;
  7. use Omnipay\Alipay\Responses\LegacyRefundResponse;
  8. class LegacyExpressGatewayTest extends AbstractGatewayTestCase
  9. {
  10. /**
  11. * @var LegacyExpressGateway $gateway
  12. */
  13. protected $gateway;
  14. protected $options;
  15. public function setUp()
  16. {
  17. parent::setUp();
  18. $this->gateway = new LegacyExpressGateway($this->getHttpClient(), $this->getHttpRequest());
  19. $this->gateway->setPartner($this->partner);
  20. $this->gateway->setKey($this->key);
  21. $this->gateway->setSellerId($this->sellerId);
  22. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  23. $this->gateway->setReturnUrl('https://www.example.com/return');
  24. $this->options = [
  25. 'out_trade_no' => '2014010122390001',
  26. 'subject' => 'test',
  27. 'total_fee' => '0.01',
  28. ];
  29. }
  30. public function testPurchase()
  31. {
  32. /**
  33. * @var LegacyExpressPurchaseResponse $response
  34. */
  35. $response = $this->gateway->purchase($this->options)->send();
  36. $this->assertTrue($response->isSuccessful());
  37. $this->assertTrue($response->isRedirect());
  38. $this->assertNotEmpty($response->getRedirectUrl());
  39. }
  40. public function testRefund()
  41. {
  42. /**
  43. * @var LegacyRefundResponse $response
  44. */
  45. $response = $this->gateway->refund(
  46. [
  47. 'refund_items' => [
  48. [
  49. 'out_trade_no' => '2016092021001003280286716852',
  50. 'amount' => '1',
  51. 'reason' => 'test',
  52. ]
  53. ]
  54. ]
  55. )->send();
  56. $this->assertTrue($response->isSuccessful());
  57. $this->assertTrue($response->isRedirect());
  58. $this->assertNotEmpty($response->getRedirectUrl());
  59. }
  60. public function testQuery()
  61. {
  62. $this->setMockHttpResponse('LegacyExpress_Query_Failure.txt');
  63. /**
  64. * @var LegacyQueryResponse $response
  65. */
  66. $response = $this->gateway->query(
  67. [
  68. 'out_trade_no' => '2016092021001003280286716850'
  69. ]
  70. )->send();
  71. $this->assertFalse($response->isSuccessful());
  72. }
  73. public function testCompletePurchaseWithMD5()
  74. {
  75. $str = 'buyer_email=aaa%40qq.com&buyer_id=2088202561123456&exterface=create_direct_pay_by_user&is_success=T&notify_id=RqPnCoPT3K9%252Fvwbh3InWes5p8ZRYIdWn4DYfTZV%252FByZc5wcE2q9pffj29yQCHA%252BTNHUY&notify_time=2016-09-24+21%3A03%3A57&notify_type=trade_status_sync&out_trade_no=2016-09-24+15%3A03%3A078138&payment_type=1&seller_email=test%40qq.com&seller_id=20880114664123456&subject=test&total_fee=0.01&trade_no=201609242100100406123456789&trade_status=TRADE_SUCCESS&sign=ea00ba288bf6e1cd4a6e89c5f180df7d&sign_type=MD5';
  76. parse_str($str, $data);
  77. $data['sign'] = (new Signer($data))->signWithMD5($this->key);
  78. $data['sign_type'] = 'MD5';
  79. $this->gateway = new LegacyExpressGateway($this->getHttpClient(), $this->getHttpRequest());
  80. $this->gateway->setPartner($this->partner);
  81. $this->gateway->setKey($this->key);
  82. $this->gateway->setSignType('RSA');
  83. $this->gateway->setPrivateKey(ALIPAY_LEGACY_PRIVATE_KEY);
  84. $this->gateway->setSellerId($this->sellerId);
  85. $this->gateway->setNotifyUrl('https://www.example.com/notify');
  86. $this->gateway->setReturnUrl('https://www.example.com/return');
  87. /**
  88. * @var LegacyExpressPurchaseResponse $response
  89. */
  90. $request = $this->gateway->completePurchase(['params' => $data]);
  91. $request->setVerifyNotifyId(false);
  92. $response = $request->send();
  93. $this->assertTrue($response->isSuccessful());
  94. }
  95. }