Skip to content

Commit

Permalink
Merge pull request #40 from pronamic/37-no-customer-and-payment-lines
Browse files Browse the repository at this point in the history
37 no customer and payment lines
  • Loading branch information
remcotolsma committed Jun 14, 2023
2 parents 40f8385 + 133a5ad commit 05be2c3
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 82 deletions.
95 changes: 16 additions & 79 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -391,10 +391,14 @@ public function process_payment( $order_id ) {
// Add subscription and period.
$payment->add_subscription( $subscription );

$period = $subscription->next_period();
$start_date = $subscription->get_start_date();

if ( null !== $period ) {
$payment->add_period( $period );
if ( null !== $start_date ) {
$period = $subscription->get_period_for_date( $start_date );

if ( null !== $period ) {
$payment->add_period( $period );
}
}

$payment->set_meta( 'mollie_sequence_type', 'first' );
Expand Down Expand Up @@ -516,23 +520,14 @@ private function new_pronamic_payment_from_wc_order( WC_Order $order ) {

$description = strtr( $this->payment_description, $replacements );

// Contact.
$contact_name = new ContactName();
$contact_name->set_first_name( WooCommerce::get_billing_first_name( $order ) );
$contact_name->set_last_name( WooCommerce::get_billing_last_name( $order ) );
// Order helper.
$order_helper = new OrderHelper( $order );

$customer = new Customer();
$customer->set_name( $contact_name );
$customer->set_email( WooCommerce::get_billing_email( $order ) );
$customer->set_phone( WooCommerce::get_billing_phone( $order ) );
$customer->set_user_id( $order->get_user_id() );
// Contact name.
$contact_name = $order_helper->get_contact_name();

// Company name.
$company_name = WooCommerce::get_billing_company( $order );

if ( ! empty( $company_name ) ) {
$customer->set_company_name( $company_name );
}
// Customer.
$customer = $order_helper->get_customer();

// Customer gender.
$gender = null;
Expand Down Expand Up @@ -707,59 +702,8 @@ private function new_pronamic_payment_from_wc_order( WC_Order $order ) {
)
);

/*
* Payment lines and order items.
*
* WooCommerce has multiple order item types:
* `line_item`, `fee`, `shipping`, `tax`, `coupon`
* @link https://github.com/woocommerce/woocommerce/search?q=%22extends+WC_Order_Item%22
*
* For now we handle only the `line_item`, `fee` and `shipping` items,
* we consciously don't handle the `tax` and `coupon` items.
*
* **Order item `coupon`**
* Coupon items are also applied to the `line_item` item and line total.
* @link https://basecamp.com/1810084/projects/10966871/todos/372490988
*
* **Order item `tax`**
* Tax items are also applied to the `line_item` item and line total.
*/
$items = $order->get_items( [ 'line_item', 'fee', 'shipping' ] );

$tax_percentages = [ 0 ];

$payment->lines = new PaymentLines();

foreach ( $items as $item_id => $item ) {
$line = $payment->lines->new_line();

$type = OrderItemType::transform( $item );

// Quantity.
$quantity = wc_stock_amount( $item['qty'] );

if ( PaymentLineType::SHIPPING === $type ) {
$quantity = 1;
}

// Tax.
$tax_rate_id = WooCommerce::get_order_item_tax_rate_id( $item );

$percent = is_null( $tax_rate_id ) ? null : \WC_Tax::get_rate_percent_value( $tax_rate_id );

// Set line properties.
$line->set_id( (string) $item_id );
$line->set_sku( WooCommerce::get_order_item_sku( $item ) );
$line->set_type( (string) $type );
$line->set_name( $item['name'] );
$line->set_quantity( $quantity );
$line->set_unit_price( new TaxedMoney( $order->get_item_total( $item, true ), WooCommerce::get_currency(), $order->get_item_tax( $item ), $percent ) );
$line->set_total_amount( new TaxedMoney( $order->get_line_total( $item, true ), WooCommerce::get_currency(), $order->get_line_tax( $item ), $percent ) );
$line->set_product_url( WooCommerce::get_order_item_url( $item ) );
$line->set_image_url( WooCommerce::get_order_item_image( $item ) );
$line->set_product_category( WooCommerce::get_order_item_category( $item ) );
$line->set_meta( 'woocommerce_order_item_id', $item_id );
}
// Payment lines and order items.
$payment->lines = $order_helper->get_lines();

return $payment;
}
Expand Down Expand Up @@ -812,14 +756,7 @@ private function connect_subscription_payment_renewal( $payment, $order ) {
$pronamic_subscription = $subscription_helper->get_pronamic_subscription();

if ( null !== $pronamic_subscription ) {
// Add subscription and period.
$payment->add_subscription( $pronamic_subscription );

$period = $pronamic_subscription->next_period();

if ( null !== $period ) {
$payment->add_period( $period );
}
}
}
}
Expand Down Expand Up @@ -978,7 +915,7 @@ public function process_refund( $order_id, $amount = null, $reason = '' ) {

if ( null !== $payment_line ) {
$line->meta = $payment_line->meta;

$line->set_payment_line( $payment_line );
}
}
Expand Down
141 changes: 141 additions & 0 deletions src/OrderHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php
/**
* Order helper
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2023 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Pay\Extensions\WooCommerce
*/

namespace Pronamic\WordPress\Pay\Extensions\WooCommerce;

use Pronamic\WordPress\Money\TaxedMoney;
use Pronamic\WordPress\Pay\Customer;
use Pronamic\WordPress\Pay\ContactName;
use Pronamic\WordPress\Pay\Payments\PaymentLines;
use Pronamic\WordPress\Pay\Payments\PaymentLineType;
use WC_Order;
use WC_Tax;

class OrderHelper {
/**
* WooCommerce order.
*
* @var WC_Order
*/
private $woocommerce_order;

/**
* Construct order helper.
*
* @param WC_Order $woocommerce_order WooCommerce order.
*/
public function __construct( WC_Order $woocommerce_order ) {
$this->woocommerce_order = $woocommerce_order;
}

/**
* Get contact name.
*
* @return ContactName
*/
public function get_contact_name() {
$order = $this->woocommerce_order;

$contact_name = new ContactName();
$contact_name->set_first_name( WooCommerce::get_billing_first_name( $order ) );
$contact_name->set_last_name( WooCommerce::get_billing_last_name( $order ) );

return $contact_name;
}

/**
* Get customer.
*
* @return Customer
*/
public function get_customer() {
$order = $this->woocommerce_order;

$customer = new Customer();
$customer->set_name( $this->get_contact_name() );
$customer->set_email( WooCommerce::get_billing_email( $order ) );
$customer->set_phone( WooCommerce::get_billing_phone( $order ) );
$customer->set_user_id( $order->get_user_id() );

// Company name.
$company_name = WooCommerce::get_billing_company( $order );

if ( ! empty( $company_name ) ) {
$customer->set_company_name( $company_name );
}

return $customer;
}

/**
* Get lines.
*
* @return PaymentLines|null
*/
public function get_lines() {
/*
* Payment lines and order items.
*
* WooCommerce has multiple order item types:
* `line_item`, `fee`, `shipping`, `tax`, `coupon`
* @link https://github.com/woocommerce/woocommerce/search?q=%22extends+WC_Order_Item%22
*
* For now we handle only the `line_item`, `fee` and `shipping` items,
* we consciously don't handle the `tax` and `coupon` items.
*
* **Order item `coupon`**
* Coupon items are also applied to the `line_item` item and line total.
* @link https://basecamp.com/1810084/projects/10966871/todos/372490988
*
* **Order item `tax`**
* Tax items are also applied to the `line_item` item and line total.
*/
$order = $this->woocommerce_order;

$items = $order->get_items( [ 'line_item', 'fee', 'shipping' ] );

$tax_percentages = [ 0 ];

$lines = new PaymentLines();

foreach ( $items as $item_id => $item ) {
$line = $lines->new_line();

$type = OrderItemType::transform( $item );

// Quantity.
$quantity = \wc_stock_amount( $item['qty'] );

if ( PaymentLineType::SHIPPING === $type ) {
$quantity = 1;
}

// Tax.
$tax_rate_id = WooCommerce::get_order_item_tax_rate_id( $item );

$percent = is_null( $tax_rate_id ) ? null : WC_Tax::get_rate_percent_value( $tax_rate_id );

// Set line properties.
$line->set_id( (string) $item_id );
$line->set_sku( WooCommerce::get_order_item_sku( $item ) );
$line->set_type( (string) $type );
$line->set_name( $item['name'] );
$line->set_quantity( $quantity );
$line->set_unit_price( new TaxedMoney( $order->get_item_total( $item, true ), WooCommerce::get_currency(), $order->get_item_tax( $item ), $percent ) );
$line->set_total_amount( new TaxedMoney( $order->get_line_total( $item, true ), WooCommerce::get_currency(), $order->get_line_tax( $item ), $percent ) );
$line->set_product_url( WooCommerce::get_order_item_url( $item ) );
$line->set_image_url( WooCommerce::get_order_item_image( $item ) );
$line->set_product_category( WooCommerce::get_order_item_category( $item ) );
$line->set_meta( 'woocommerce_order_item_id', $item_id );
}

return $lines;
}
}
15 changes: 12 additions & 3 deletions src/SubscriptionUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function update_pronamic_subscription() {
$pronamic_subscription->status = WooCommerceSubscriptionStatus::from_subscription( $woocommerce_subscription )->to_pronamic_status();

// Date.
$start_date = new \DateTimeImmutable( $woocommerce_subscription->get_date( 'date_created', 'gmt' ), new \DateTimeZone( 'GMT' ) );
$start_date = new \DateTimeImmutable( $woocommerce_subscription->get_date( 'start', 'gmt' ), new \DateTimeZone( 'GMT' ) );

$pronamic_subscription->date = $start_date;

Expand Down Expand Up @@ -93,6 +93,12 @@ public function update_pronamic_subscription() {
)
);

// Order helper.
$order_helper = new OrderHelper( $woocommerce_subscription );

// Customer.
$pronamic_subscription->set_customer( $order_helper->get_customer() );

// Phases.
$pronamic_subscription->set_phases( [] );

Expand Down Expand Up @@ -171,13 +177,16 @@ public function update_pronamic_subscription() {

$regular_phase->set_end_date( empty( $end_date ) ? null : new \DateTimeImmutable( $end_date ) );

// Add phase.
$pronamic_subscription->add_phase( $regular_phase );

// Next payment date.
$next_date = $woocommerce_subscription->get_date( 'next_payment' );

$pronamic_subscription->set_next_payment_date( empty( $next_date ) ? null : new \DateTimeImmutable( $next_date ) );

// Add phase.
$pronamic_subscription->add_phase( $regular_phase );
// Lines
$pronamic_subscription->lines = $order_helper->get_lines();
}

/**
Expand Down

0 comments on commit 05be2c3

Please sign in to comment.