* API result array.
*
* @throws \API_Exception
+ * @throws \CiviCRM_API3_Exception
*/
function civicrm_api3_payment_processor_pay($params) {
+ /* @var CRM_Core_Payment $processor */
$processor = Civi\Payment\System::singleton()->getById($params['payment_processor_id']);
$processor->setPaymentProcessor(civicrm_api3('PaymentProcessor', 'getsingle', ['id' => $params['payment_processor_id']]));
try {
+ $processor->setContributionID($params['contribution_id']);
+ $processor->setInvoiceID($params['invoice_id'] ?? '');
+ if (!empty($params['contact_id'])) {
+ $processor->setContactID((int) $params['contact_id']);
+ }
+ if (!empty($params['contribution_recur_id'])) {
+ $processor->setContributionRecurID((int) $params['contribution_recur_id']);
+ }
+
$result = $processor->doPayment($params);
}
catch (\Civi\Payment\Exception\PaymentProcessorException $e) {
}
throw new API_Exception('Payment failed', $code, $errorData, $e);
}
- return civicrm_api3_create_success(array($result), $params);
+ return civicrm_api3_create_success([$result], $params);
}
/**
*/
function _civicrm_api3_payment_processor_pay_spec(&$params) {
$params['payment_processor_id'] = [
- 'api.required' => 1,
+ 'api.required' => TRUE,
'title' => ts('Payment processor'),
'type' => CRM_Utils_Type::T_INT,
];
'title' => ts('Amount to pay'),
'type' => CRM_Utils_Type::T_MONEY,
];
+ $params['contribution_id'] = [
+ 'api.required' => TRUE,
+ 'title' => ts('Contribution ID'),
+ 'type' => CRM_Utils_Type::T_INT,
+ ];
+ $params['contact_id'] = [
+ 'title' => ts('Contact ID'),
+ 'type' => CRM_Utils_Type::T_INT,
+ ];
+ $params['contribution_recur_id'] = [
+ 'title' => ts('Contribution Recur ID'),
+ 'type' => CRM_Utils_Type::T_INT,
+ ];
+ $params['invoice_id'] = [
+ 'title' => ts('Invoice ID'),
+ 'type' => CRM_Utils_Type::T_STRING,
+ ];
}
/**
protected $_apiversion = 3;
protected $_params;
+ /**
+ * Set up for class.
+ *
+ * @throws \CRM_Core_Exception
+ */
public function setUp() {
parent::setUp();
$this->useTransaction(TRUE);
/**
* Create payment processor.
+ *
+ * @throws \Exception
*/
public function testPaymentProcessorCreate() {
$params = $this->_params;
/**
* Update payment processor.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testPaymentProcessorUpdate() {
$params = $this->_params;
/**
* Check payment processor delete.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testPaymentProcessorDelete() {
$result = $this->callAPISuccess('payment_processor', 'create', $this->_params);
/**
* Check with valid params array.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testPaymentProcessorsGet() {
$params = $this->_params;
$this->assertEquals('test@test.com', $results['values'][$results['id']]['user_name']);
}
+ /**
+ * Test the processor passed to the hook can access the relevant variables.
+ *
+ * @throws \CRM_Core_Exception
+ * @throws \CiviCRM_API3_Exception
+ */
+ public function testPaymentProcessorPay() {
+ $this->hookClass->setHook('civicrm_alterPaymentProcessorParams', [$this, 'hook_civicrm_alterPaymentProcessorParams']);
+ $processor = $this->dummyProcessorCreate();
+ $this->callAPISuccess('PaymentProcessor', 'pay', [
+ 'payment_processor_id' => $processor->getID(),
+ 'contribution_id' => 10,
+ 'invoice_id' => 2,
+ 'contribution_recur_id' => 3,
+ 'amount' => 6,
+ ]);
+ }
+
+ /**
+ * Implements civicrm_alterPaymentProcessorParams hook.
+ *
+ * @param \CRM_Core_Payment_Dummy $paymentObject
+ */
+ public function hook_civicrm_alterPaymentProcessorParams($paymentObject) {
+ $this->assertEquals(10, $paymentObject->getContributionID());
+ $this->assertEquals(2, $paymentObject->getInvoiceID());
+ $this->assertEquals(3, $paymentObject->getContributionRecurID());
+ }
+
}