| 1 | <?php |
| 2 | /** |
| 3 | * Test Generated example demonstrating the Contribution.create API. |
| 4 | * |
| 5 | * Demonstrates creating contribution with Soft Credit defaults for amount and type. |
| 6 | * |
| 7 | * @return array |
| 8 | * API result array |
| 9 | */ |
| 10 | function contribution_create_example() { |
| 11 | $params = [ |
| 12 | 'contact_id' => 30, |
| 13 | 'receive_date' => '20120511', |
| 14 | 'total_amount' => '100', |
| 15 | 'financial_type_id' => 1, |
| 16 | 'non_deductible_amount' => '10', |
| 17 | 'fee_amount' => '5', |
| 18 | 'net_amount' => '95', |
| 19 | 'source' => 'SSF', |
| 20 | 'contribution_status_id' => 1, |
| 21 | 'soft_credit_to' => 31, |
| 22 | ]; |
| 23 | |
| 24 | try{ |
| 25 | $result = civicrm_api3('Contribution', 'create', $params); |
| 26 | } |
| 27 | catch (CiviCRM_API3_Exception $e) { |
| 28 | // Handle error here. |
| 29 | $errorMessage = $e->getMessage(); |
| 30 | $errorCode = $e->getErrorCode(); |
| 31 | $errorData = $e->getExtraParams(); |
| 32 | return [ |
| 33 | 'is_error' => 1, |
| 34 | 'error_message' => $errorMessage, |
| 35 | 'error_code' => $errorCode, |
| 36 | 'error_data' => $errorData, |
| 37 | ]; |
| 38 | } |
| 39 | |
| 40 | return $result; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Function returns array of result expected from previous function. |
| 45 | * |
| 46 | * @return array |
| 47 | * API result array |
| 48 | */ |
| 49 | function contribution_create_expectedresult() { |
| 50 | |
| 51 | $expectedResult = [ |
| 52 | 'is_error' => 0, |
| 53 | 'version' => 3, |
| 54 | 'count' => 1, |
| 55 | 'id' => 1, |
| 56 | 'values' => [ |
| 57 | '1' => [ |
| 58 | 'id' => '1', |
| 59 | 'contact_id' => '30', |
| 60 | 'financial_type_id' => '1', |
| 61 | 'contribution_page_id' => '', |
| 62 | 'payment_instrument_id' => '4', |
| 63 | 'receive_date' => '20120511000000', |
| 64 | 'non_deductible_amount' => '10', |
| 65 | 'total_amount' => '100', |
| 66 | 'fee_amount' => '5', |
| 67 | 'net_amount' => '95', |
| 68 | 'trxn_id' => '', |
| 69 | 'invoice_id' => '', |
| 70 | 'invoice_number' => '', |
| 71 | 'currency' => 'USD', |
| 72 | 'cancel_date' => '', |
| 73 | 'cancel_reason' => '', |
| 74 | 'receipt_date' => '', |
| 75 | 'thankyou_date' => '', |
| 76 | 'source' => 'SSF', |
| 77 | 'amount_level' => '', |
| 78 | 'contribution_recur_id' => '', |
| 79 | 'is_test' => '', |
| 80 | 'is_pay_later' => '', |
| 81 | 'contribution_status_id' => '1', |
| 82 | 'address_id' => '', |
| 83 | 'check_number' => '', |
| 84 | 'campaign_id' => '', |
| 85 | 'creditnote_id' => '', |
| 86 | 'tax_amount' => '', |
| 87 | 'revenue_recognition_date' => '', |
| 88 | 'is_template' => '', |
| 89 | 'contribution_type_id' => '1', |
| 90 | ], |
| 91 | ], |
| 92 | ]; |
| 93 | |
| 94 | return $expectedResult; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * This example has been generated from the API test suite. |
| 99 | * The test that created it is called "testCreateContributionWithSoftCreditDefaults" |
| 100 | * and can be found at: |
| 101 | * https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/api/v3/ContributionTest.php |
| 102 | * |
| 103 | * You can see the outcome of the API tests at |
| 104 | * https://test.civicrm.org/job/CiviCRM-Core-Matrix/ |
| 105 | * |
| 106 | * To Learn about the API read |
| 107 | * https://docs.civicrm.org/dev/en/latest/api/ |
| 108 | * |
| 109 | * Browse the API on your own site with the API Explorer. It is in the main |
| 110 | * CiviCRM menu, under: Support > Development > API Explorer. |
| 111 | * |
| 112 | * Read more about testing here |
| 113 | * https://docs.civicrm.org/dev/en/latest/testing/ |
| 114 | * |
| 115 | * API Standards documentation: |
| 116 | * https://docs.civicrm.org/dev/en/latest/framework/api-architecture/ |
| 117 | */ |