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