| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\DonationForms\DataTransferObjects; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Donors\Models\Donor; |
| 7 |
|
| 8 |
/** |
| 9 |
* This DTO extracts the complexity of supplying an array for use in give_set_purchase_session() |
| 10 |
* |
| 11 |
* @since 3.0.0 |
| 12 |
*/ |
| 13 |
class LegacyPurchaseFormData |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @var Donation |
| 17 |
*/ |
| 18 |
public $donation; |
| 19 |
/** |
| 20 |
* @var Donor |
| 21 |
*/ |
| 22 |
public $donor; |
| 23 |
|
| 24 |
/** |
| 25 |
* @since 3.0.0 |
| 26 |
* |
| 27 |
* @param array{donation: Donation, donor: Donation} $array |
| 28 |
* @return LegacyPurchaseFormData |
| 29 |
*/ |
| 30 |
public static function fromArray(array $array): self |
| 31 |
{ |
| 32 |
$self = new self(); |
| 33 |
|
| 34 |
$self->donation = $array['donation']; |
| 35 |
$self->donor = $array['donor']; |
| 36 |
|
| 37 |
return $self; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Returns shape needed for give_set_purchase_session() |
| 42 |
* |
| 43 |
* @since 3.0.0 |
| 44 |
*/ |
| 45 |
public function toPurchaseData(): array |
| 46 |
{ |
| 47 |
$address = [ |
| 48 |
'line1' => $this->donation->billingAddress->address1, |
| 49 |
'line2' => $this->donation->billingAddress->address2, |
| 50 |
'city' => $this->donation->billingAddress->city, |
| 51 |
'state' => $this->donation->billingAddress->state, |
| 52 |
'country' => $this->donation->billingAddress->country, |
| 53 |
'zip' => $this->donation->billingAddress->zip |
| 54 |
]; |
| 55 |
|
| 56 |
return [ |
| 57 |
'donation_id' => $this->donation->id, |
| 58 |
'price' => $this->donation->amount->formatToDecimal(), |
| 59 |
'purchase_key' => $this->donation->purchaseKey, |
| 60 |
'user_email' => $this->donor->email, |
| 61 |
'date' => $this->donation->createdAt->format('Y-m-d H:i:s'), |
| 62 |
'user_info' => [ |
| 63 |
'id' => $this->donor->id, |
| 64 |
'firstName' => $this->donor->firstName, |
| 65 |
'lastName' => $this->donor->lastName, |
| 66 |
'title' => $this->donor->prefix, |
| 67 |
'email' => $this->donor->email, |
| 68 |
'address' => $address |
| 69 |
], |
| 70 |
'post_data' => [ |
| 71 |
'amount' => $this->donation->amount->formatToDecimal(), |
| 72 |
'first' => $this->donation->firstName, |
| 73 |
'last' => $this->donation->lastName, |
| 74 |
'email' => $this->donation->email, |
| 75 |
'userId' => get_current_user_id(), |
| 76 |
'gateway' => $this->donation->gatewayId, |
| 77 |
], |
| 78 |
'gateway' => $this->donation->gatewayId, |
| 79 |
'card_info' => [ |
| 80 |
'address' => $address |
| 81 |
], |
| 82 |
]; |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|