Merge pull request #15630 from eileenmcnaughton/pay_fail
[civicrm-core.git] / tests / phpunit / api / v3 / PaymentProcessorTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class contains api test cases for "civicrm_payment_processor"
30 *
31 * @group headless
32 */
33 class api_v3_PaymentProcessorTest extends CiviUnitTestCase {
34 protected $_paymentProcessorType;
35 protected $_apiversion = 3;
36 protected $_params;
37
38 /**
39 * Set up for class.
40 *
41 * @throws \CRM_Core_Exception
42 */
43 public function setUp() {
44 parent::setUp();
45 $this->useTransaction(TRUE);
46 // Create dummy processor
47 $params = [
48 'name' => 'API_Test_PP_Type',
49 'title' => 'API Test Payment Processor Type',
50 'class_name' => 'CRM_Core_Payment_APITest',
51 'billing_mode' => 'form',
52 'is_recur' => 0,
53 ];
54 $result = $this->callAPISuccess('payment_processor_type', 'create', $params);
55 $this->_paymentProcessorType = $result['id'];
56 $this->_params = [
57 'name' => 'API Test PP',
58 'payment_processor_type_id' => $this->_paymentProcessorType,
59 'class_name' => 'CRM_Core_Payment_APITest',
60 'is_recur' => 0,
61 'domain_id' => 1,
62 ];
63 }
64
65 /**
66 * Check with no name.
67 */
68 public function testPaymentProcessorCreateWithoutName() {
69 $payProcParams = [
70 'is_active' => 1,
71 ];
72 $this->callAPIFailure('payment_processor', 'create', $payProcParams);
73 }
74
75 /**
76 * Create payment processor.
77 *
78 * @throws \Exception
79 */
80 public function testPaymentProcessorCreate() {
81 $params = $this->_params;
82 $result = $this->callAPIAndDocument('payment_processor', 'create', $params, __FUNCTION__, __FILE__);
83 $this->callAPISuccessGetSingle('EntityFinancialAccount', ['entity_table' => 'civicrm_payment_processor', 'entity_id' => $result['id']]);
84
85 // Test that the option values are flushed so ths can be used straight away.
86 $this->callAPISuccess('ContributionRecur', 'create', [
87 'contact_id' => $this->individualCreate(),
88 'amount' => 5,
89 'financial_type_id' => 'Donation',
90 'payment_processor_id' => 'API Test PP',
91 'frequency_interval' => 1,
92 ]);
93 $this->getAndCheck($params, $result['id'], 'PaymentProcessor');
94 }
95
96 /**
97 * Update payment processor.
98 *
99 * @throws \CRM_Core_Exception
100 */
101 public function testPaymentProcessorUpdate() {
102 $params = $this->_params;
103 $result = $this->callAPISuccess('payment_processor', 'create', $params);
104 $this->assertNotNull($result['id']);
105
106 $updateParams = [
107 'id' => $result['id'],
108 'name' => 'Update API Test',
109 ];
110 $this->assertDBState('CRM_Financial_DAO_PaymentProcessor', $result['id'], $params);
111 $this->callAPISuccess('payment_processor', 'create', $updateParams);
112 $result = $this->callAPISuccess('payment_processor', 'get', ['id' => $result['id']]);
113
114 $expectedResult = [
115 'id' => $result['id'],
116 'domain_id' => $params['domain_id'],
117 'name' => $updateParams['name'],
118 'payment_processor_type_id' => $params['payment_processor_type_id'],
119 'is_default' => 0,
120 'is_test' => 0,
121 'class_name' => $params['class_name'],
122 'billing_mode' => 1,
123 'is_recur' => $params['is_recur'],
124 'payment_type' => 1,
125 'payment_instrument_id' => 1,
126 'is_active' => 1,
127 ];
128 $this->checkArrayEquals($expectedResult, $result['values'][$result['id']]);
129 }
130
131 /**
132 * Test using example code.
133 */
134 public function testPaymentProcessorCreateExample() {
135 require_once 'api/v3/examples/PaymentProcessor/Create.ex.php';
136 $result = payment_processor_create_example();
137 $expectedResult = payment_processor_create_expectedresult();
138 $this->assertAPISuccess($result);
139 }
140
141 /**
142 * Check payment processor delete.
143 *
144 * @throws \CRM_Core_Exception
145 */
146 public function testPaymentProcessorDelete() {
147 $result = $this->callAPISuccess('payment_processor', 'create', $this->_params);
148 $params = [
149 'id' => $result['id'],
150 ];
151
152 $this->callAPIAndDocument('payment_processor', 'delete', $params, __FUNCTION__, __FILE__);
153 }
154
155 /**
156 * Check with valid params array.
157 *
158 * @throws \CRM_Core_Exception
159 */
160 public function testPaymentProcessorsGet() {
161 $params = $this->_params;
162 $params['user_name'] = 'test@test.com';
163 $this->callAPISuccess('payment_processor', 'create', $params);
164
165 $params = [
166 'user_name' => 'test@test.com',
167 ];
168 $results = $this->callAPISuccess('payment_processor', 'get', $params);
169
170 $this->assertEquals(1, $results['count']);
171 $this->assertEquals('test@test.com', $results['values'][$results['id']]['user_name']);
172 }
173
174 /**
175 * Test the processor passed to the hook can access the relevant variables.
176 *
177 * @throws \CRM_Core_Exception
178 * @throws \CiviCRM_API3_Exception
179 */
180 public function testPaymentProcessorPay() {
181 $this->hookClass->setHook('civicrm_alterPaymentProcessorParams', [$this, 'hook_civicrm_alterPaymentProcessorParams']);
182 $processor = $this->dummyProcessorCreate();
183 $this->callAPISuccess('PaymentProcessor', 'pay', [
184 'payment_processor_id' => $processor->getID(),
185 'contribution_id' => 10,
186 'invoice_id' => 2,
187 'contribution_recur_id' => 3,
188 'amount' => 6,
189 ]);
190 }
191
192 /**
193 * Implements civicrm_alterPaymentProcessorParams hook.
194 *
195 * @param \CRM_Core_Payment_Dummy $paymentObject
196 */
197 public function hook_civicrm_alterPaymentProcessorParams($paymentObject) {
198 $this->assertEquals(10, $paymentObject->getContributionID());
199 $this->assertEquals(2, $paymentObject->getInvoiceID());
200 $this->assertEquals(3, $paymentObject->getContributionRecurID());
201 }
202
203 }