63ba8cb8fbb3eb82aef59b55aba9252adab50fbe
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Test\Api;
3
4 use PayPal\Api\Amount;
5 use PayPal\Api\Authorization;
6 use PayPal\Api\Links;
7 use PayPal\Test\Constants;
8 use PayPal\Api\RedirectUrls;
9 use PayPal\Api\Address;
10
11 use PayPal\Api\Capture;
12 use PayPal\Api\CreditCard;
13 use PayPal\Api\Payer;
14 use PayPal\Api\Payment;
15 use PayPal\Api\FundingInstrument;
16 use PayPal\Api\Transaction;
17 use PayPal\Exception\PPConnectionException;
18
19 class AuthorizationTest extends \PHPUnit_Framework_TestCase {
20         private $authorizations = array();
21         public static $create_time = "2013-02-28T00:00:00Z";
22         public static $id = "AUTH-123";
23         public static $state = "Created";
24         public static $parent_payment = "PAY-12345";
25         public static $currency = "USD";
26         public static $total = "1.12";
27         public static $href = "USD";
28         public static $rel = "1.12";
29         public static $method = "1.12";
30         
31         public static function createAuthorization() {                  
32                 $authorization = new Authorization();
33                 $authorization->setCreateTime(self::$create_time);
34                 $authorization->setId(self::$id);
35                 $authorization->setState(self::$state);
36                 
37                 $authorization->setAmount(AmountTest::createAmount());
38                 $authorization->setLinks(array(LinksTest::createLinks()));      
39                 
40                 return $authorization;
41         }
42         
43         public static function authorize()
44         {
45                 $addr = new Address();
46                 $addr->setLine1("3909 Witmer Road");
47                 $addr->setLine2("Niagara Falls");
48                 $addr->setCity("Niagara Falls");
49                 $addr->setState("NY");
50                 $addr->setPostal_code("14305");
51                 $addr->setCountry_code("US");
52                 $addr->setPhone("716-298-1822");
53                 
54                 $card = new CreditCard();
55                 $card->setType("visa");
56                 $card->setNumber("4417119669820331");
57                 $card->setExpire_month("11");
58                 $card->setExpire_year("2019");
59                 $card->setCvv2("012");
60                 $card->setFirst_name("Joe");
61                 $card->setLast_name("Shopper");
62                 $card->setBilling_address($addr);
63                 
64                 $fi = new FundingInstrument();
65                 $fi->setCredit_card($card);
66                 
67                 $payer = new Payer();
68                 $payer->setPayment_method("credit_card");
69                 $payer->setFunding_instruments(array($fi));
70                 
71                 $amount = new Amount();
72                 $amount->setCurrency("USD");
73                 $amount->setTotal("1.00");
74                 
75                 $transaction = new Transaction();
76                 $transaction->setAmount($amount);
77                 $transaction->setDescription("This is the payment description.");
78                 
79                 $payment = new Payment();
80                 $payment->setIntent("authorize");
81                 $payment->setPayer($payer);
82                 $payment->setTransactions(array($transaction));
83                 
84                 $paymnt = $payment->create();
85                 $resArray = $paymnt->toArray();
86                 
87                 return $authId = $resArray['transactions'][0]['related_resources'][0]['authorization']['id'];
88                 
89         }
90         public function setup() {
91                 $authorization = new Authorization();
92                 $authorization->setCreateTime(self::$create_time);
93                 $authorization->setId(self::$id);
94                 $authorization->setState(self::$state);
95                 $authorization->setParentPayment(self::$parent_payment);
96                 $this->authorizations['partial'] = $authorization;
97                 $this->authorizations['full'] = self::createAuthorization();
98                 
99         }
100
101         public function testGetterSetter() {            
102                 $authorization = $this->authorizations['partial'];
103                 $this->assertEquals(self::$create_time, $authorization->getCreateTime());
104                 $this->assertEquals(self::$id, $authorization->getId());
105                 $this->assertEquals(self::$state, $authorization->getState());
106                 $this->assertEquals(self::$parent_payment, $authorization->getParentPayment());
107                 
108                 $authorization = $this->authorizations['full'];
109                 $this->assertEquals(AmountTest::$currency, $authorization->getAmount()->getCurrency());
110                 $this->assertEquals(1, count($authorization->getLinks()));
111         }
112         
113         public function testSerializeDeserialize() {
114                 $a1 = $this->authorizations['partial'];
115                 $a2 = new Authorization();
116                 $a2->fromJson($a1->toJson());
117                 $this->assertEquals($a1, $a2);
118         }
119         public function testOperations() {
120                 $authId = self::authorize();
121                 $auth = Authorization::get($authId);
122                 $this->assertNotNull($auth->getId());
123                 
124                 $amount = new Amount();
125                 $amount->setCurrency("USD");
126                 $amount->setTotal("1.00");
127                 
128                 $captur = new Capture();
129                 $captur->setId($authId);
130                 $captur->setAmount($amount);    
131                 
132                 $capt = $auth->capture($captur);
133                 $this->assertNotNull( $capt->getId());
134                 
135                 $authId = self::authorize();
136                 $auth = Authorization::get($authId);
137                 $void = $auth->void();
138                 $this->assertNotNull($void->getId());
139
140         }
141         
142         public function testReauthorize(){
143                 $authorization = Authorization::get('7GH53639GA425732B');
144         
145                 $amount = new Amount();
146                 $amount->setCurrency("USD");
147                 $amount->setTotal("1.00");
148                 
149                 $authorization->setAmount($amount);
150                 try{
151                         $reauthorization = $authorization->reauthorize();
152                 }catch (PPConnectionException $ex){
153                         $this->assertEquals(strpos($ex->getMessage(),"500"), false);
154                 }
155         }
156 }