DonationFormRepository.php
4 years ago
DonationRepository.php
4 years ago
DonorRepository.php
4 years ago
RevenueRepository.php
4 years ago
DonationRepository.php
93 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\TestData\Repositories; |
| 4 | |
| 5 | use Give\TestData\Factories\DonationFactory; |
| 6 | use Give\TestData\Framework\MetaRepository; |
| 7 | use Give\ValueObjects\Money; |
| 8 | use Give_Donor; |
| 9 | |
| 10 | class DonationRepository |
| 11 | { |
| 12 | /** |
| 13 | * @var DonationFactory |
| 14 | */ |
| 15 | private $donationFactory; |
| 16 | /** |
| 17 | * @var RevenueRepository |
| 18 | */ |
| 19 | private $revenueRepository; |
| 20 | |
| 21 | /** |
| 22 | * @param DonationFactory $donationFactory |
| 23 | * @param RevenueRepository $revenueRepository |
| 24 | */ |
| 25 | public function __construct(DonationFactory $donationFactory, RevenueRepository $revenueRepository) |
| 26 | { |
| 27 | $this->donationFactory = $donationFactory; |
| 28 | $this->revenueRepository = $revenueRepository; |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * @param array $donation |
| 33 | * @param array $params |
| 34 | */ |
| 35 | public function insertDonation($donation, $params) |
| 36 | { |
| 37 | global $wpdb; |
| 38 | |
| 39 | $donation = wp_parse_args( |
| 40 | apply_filters('give-test-data-donation-definition', $donation, $params), |
| 41 | $this->donationFactory->definition() |
| 42 | ); |
| 43 | |
| 44 | // Insert donation |
| 45 | $wpdb->insert( |
| 46 | "{$wpdb->prefix}posts", |
| 47 | [ |
| 48 | 'post_type' => 'give_payment', |
| 49 | 'post_date' => $donation['completed_date'], |
| 50 | 'post_status' => $donation['payment_status'], |
| 51 | ] |
| 52 | ); |
| 53 | |
| 54 | $donationID = $wpdb->insert_id; |
| 55 | |
| 56 | // Insert donation in revenue table |
| 57 | $this->revenueRepository->insertRevenue( |
| 58 | [ |
| 59 | 'donation_id' => $donationID, |
| 60 | 'form_id' => $donation['payment_form_id'], |
| 61 | 'amount' => Money::of($donation['payment_total'], $donation['payment_currency'])->getMinorAmount(), |
| 62 | ] |
| 63 | ); |
| 64 | |
| 65 | $metaRepository = new MetaRepository('give_donationmeta', 'donation_id'); |
| 66 | |
| 67 | $metaRepository->persist( |
| 68 | $donationID, |
| 69 | [ |
| 70 | '_give_payment_form_id' => $donation['payment_form_id'], |
| 71 | '_give_payment_form_title' => $donation['payment_form_title'], |
| 72 | '_give_payment_donor_id' => $donation['donor_id'], |
| 73 | '_give_payment_total' => $donation['payment_total'], |
| 74 | '_give_payment_currency' => $donation['payment_currency'], |
| 75 | '_give_payment_gateway' => $donation['payment_gateway'], |
| 76 | '_give_payment_mode' => $donation['payment_mode'], |
| 77 | '_give_payment_purchase_key' => $donation['payment_purchase_key'], |
| 78 | '_give_completed_date' => $donation['completed_date'], |
| 79 | '_give_donor_billing_first_name' => $donation['donor_name'], |
| 80 | '_give_donor_billing_last_name' => '', |
| 81 | '_give_payment_donor_email' => $donation['donor_email'], |
| 82 | ] |
| 83 | ); |
| 84 | |
| 85 | // Increase donor donated amount and count |
| 86 | $donor = new Give_Donor($donation['donor_id']); |
| 87 | $donor->increase_value(floatval($donation['payment_total'])); |
| 88 | $donor->increase_purchase_count(); |
| 89 | |
| 90 | do_action('give-test-data-insert-donation', $donationID, $donation, $params); |
| 91 | } |
| 92 | } |
| 93 |