mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-08-30 05:00:51 +08:00
Add support for refunds in api response structure
This commit is contained in:
parent
65f89fd63e
commit
ef5fc4b3d4
14 changed files with 696 additions and 194 deletions
|
@ -12,11 +12,14 @@ namespace WooCommerce\PayPalCommerce\ApiClient;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingSubscriptions;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingPlans;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerPayableBreakdown;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\BillingCycleFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentPreferencesFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\RefundFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PlanFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ProductFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\RefundPayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\SellerPayableBreakdownFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingOptionFactory;
|
||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
|
@ -293,7 +296,9 @@ return array(
|
|||
'api.factory.refund' => static function ( ContainerInterface $container ): RefundFactory {
|
||||
$amount_factory = $container->get( 'api.factory.amount' );
|
||||
return new RefundFactory(
|
||||
$amount_factory
|
||||
$amount_factory,
|
||||
$container->get( 'api.factory.seller-payable-breakdown' ),
|
||||
$container->get( 'api.factory.refund_payer' )
|
||||
);
|
||||
},
|
||||
'api.factory.purchase-unit' => static function ( ContainerInterface $container ): PurchaseUnitFactory {
|
||||
|
@ -358,6 +363,9 @@ return array(
|
|||
$address_factory = $container->get( 'api.factory.address' );
|
||||
return new PayerFactory( $address_factory );
|
||||
},
|
||||
'api.factory.refund_payer' => static function ( ContainerInterface $container ): RefundPayerFactory {
|
||||
return new RefundPayerFactory();
|
||||
},
|
||||
'api.factory.address' => static function ( ContainerInterface $container ): AddressFactory {
|
||||
return new AddressFactory();
|
||||
},
|
||||
|
@ -403,6 +411,12 @@ return array(
|
|||
$container->get( 'api.factory.platform-fee' )
|
||||
);
|
||||
},
|
||||
'api.factory.seller-payable-breakdown' => static function ( ContainerInterface $container ): SellerPayableBreakdownFactory {
|
||||
return new SellerPayableBreakdownFactory(
|
||||
$container->get( 'api.factory.money' ),
|
||||
$container->get( 'api.factory.platform-fee' )
|
||||
);
|
||||
},
|
||||
'api.factory.fraud-processor-response' => static function ( ContainerInterface $container ): FraudProcessorResponseFactory {
|
||||
return new FraudProcessorResponseFactory();
|
||||
},
|
||||
|
|
|
@ -28,12 +28,12 @@ class Payments {
|
|||
*/
|
||||
private $captures;
|
||||
|
||||
/**
|
||||
* The Captures.
|
||||
*
|
||||
* @var Refund[]
|
||||
*/
|
||||
private $refunds;
|
||||
/**
|
||||
* The Captures.
|
||||
*
|
||||
* @var Refund[]
|
||||
*/
|
||||
private $refunds;
|
||||
|
||||
/**
|
||||
* Payments constructor.
|
||||
|
@ -55,12 +55,12 @@ class Payments {
|
|||
}
|
||||
unset( $captures[ $key ] );
|
||||
}
|
||||
foreach ( $refunds as $key => $refund ) {
|
||||
if ( is_a( $refund, Refund::class ) ) {
|
||||
continue;
|
||||
}
|
||||
unset( $refunds[ $key ] );
|
||||
}
|
||||
foreach ( $refunds as $key => $refund ) {
|
||||
if ( is_a( $refund, Refund::class ) ) {
|
||||
continue;
|
||||
}
|
||||
unset( $refunds[ $key ] );
|
||||
}
|
||||
$this->authorizations = $authorizations;
|
||||
$this->captures = $captures;
|
||||
$this->refunds = $refunds;
|
||||
|
@ -112,12 +112,12 @@ class Payments {
|
|||
return $this->captures;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the Refunds.
|
||||
*
|
||||
* @return Refund[]
|
||||
**/
|
||||
public function refunds(): array {
|
||||
return $this->refunds;
|
||||
}
|
||||
/**
|
||||
* Returns the Refunds.
|
||||
*
|
||||
* @return Refund[]
|
||||
**/
|
||||
public function refunds(): array {
|
||||
return $this->refunds;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -51,6 +51,150 @@ class Refund {
|
|||
*/
|
||||
private $invoice_id;
|
||||
|
||||
/**
|
||||
* The custom id.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $custom_id;
|
||||
|
||||
/**
|
||||
* The acquirer reference number.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $acquirer_reference_number;
|
||||
|
||||
/**
|
||||
* The acquirer reference number.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $note_to_payer;
|
||||
|
||||
/**
|
||||
* The payer of the refund.
|
||||
*
|
||||
* @var ?RefundPayer
|
||||
*/
|
||||
private $payer;
|
||||
|
||||
/**
|
||||
* Refund constructor.
|
||||
*
|
||||
* @param string $id The ID.
|
||||
* @param RefundStatus $status The status.
|
||||
* @param Amount $amount The amount.
|
||||
* @param string $invoice_id The invoice id.
|
||||
* @param string $custom_id The custom id.
|
||||
* @param SellerPayableBreakdown|null $seller_payable_breakdown The detailed breakdown of the refund activity (fees, ...).
|
||||
* @param string $acquirer_reference_number The acquirer reference number.
|
||||
* @param string $note_to_payer The note to payer.
|
||||
* @param RefundPayer|null $payer The payer.
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
RefundStatus $status,
|
||||
Amount $amount,
|
||||
string $invoice_id,
|
||||
string $custom_id,
|
||||
?SellerPayableBreakdown $seller_payable_breakdown,
|
||||
string $acquirer_reference_number,
|
||||
string $note_to_payer,
|
||||
?RefundPayer $payer
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->status = $status;
|
||||
$this->amount = $amount;
|
||||
$this->invoice_id = $invoice_id;
|
||||
$this->custom_id = $custom_id;
|
||||
$this->seller_payable_breakdown = $seller_payable_breakdown;
|
||||
$this->acquirer_reference_number = $acquirer_reference_number;
|
||||
$this->note_to_payer = $note_to_payer;
|
||||
$this->payer = $payer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function id() : string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the status.
|
||||
*
|
||||
* @return RefundStatus
|
||||
*/
|
||||
public function status() : RefundStatus {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the amount.
|
||||
*
|
||||
* @return Amount
|
||||
*/
|
||||
public function amount() : Amount {
|
||||
return $this->amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the invoice id.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function invoice_id() : string {
|
||||
return $this->invoice_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the custom id.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function custom_id() : string {
|
||||
return $this->custom_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the detailed breakdown of the refund activity (fees, ...).
|
||||
*
|
||||
* @return SellerPayableBreakdown|null
|
||||
*/
|
||||
public function seller_payable_breakdown() : ?SellerPayableBreakdown {
|
||||
return $this->seller_payable_breakdown;
|
||||
}
|
||||
|
||||
/**
|
||||
* The acquirer reference number.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function acquirer_reference_number() : string {
|
||||
return $this->acquirer_reference_number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The note to payer.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function note_to_payer() : string {
|
||||
return $this->note_to_payer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the refund payer.
|
||||
*
|
||||
* @return RefundPayer|null
|
||||
*/
|
||||
public function payer() : ?RefundPayer {
|
||||
return $this->payer;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity as array.
|
||||
*
|
||||
|
@ -58,7 +202,24 @@ class Refund {
|
|||
*/
|
||||
public function to_array() : array {
|
||||
$data = array(
|
||||
'id' => $this->id(),
|
||||
'status' => $this->status()->name(),
|
||||
'amount' => $this->amount()->to_array(),
|
||||
'invoice_id' => $this->invoice_id(),
|
||||
'custom_id' => $this->custom_id(),
|
||||
'acquirer_reference_number' => $this->acquirer_reference_number(),
|
||||
'note_to_payer' => (array) $this->note_to_payer(),
|
||||
);
|
||||
$details = $this->status()->details();
|
||||
if ( $details ) {
|
||||
$data['status_details'] = array( 'reason' => $details->reason() );
|
||||
}
|
||||
if ( $this->seller_payable_breakdown ) {
|
||||
$data['seller_payable_breakdown'] = $this->seller_payable_breakdown->to_array();
|
||||
}
|
||||
if ( $this->payer ) {
|
||||
$data['payer'] = $this->payer->to_array();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -110,8 +110,8 @@ class RefundCapture {
|
|||
if ( $this->note_to_payer() ) {
|
||||
$data['note_to_payer'] = $this->note_to_payer();
|
||||
}
|
||||
if ( $this->amount() ) {
|
||||
$data['amount'] = $this->amount()->to_array();
|
||||
if ( $this->amount ) {
|
||||
$data['amount'] = $this->amount->to_array();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
|
79
modules/ppcp-api-client/src/Entity/RefundPayer.php
Normal file
79
modules/ppcp-api-client/src/Entity/RefundPayer.php
Normal file
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
/**
|
||||
* The refund payer object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class RefundPayer
|
||||
* The customer who sends the money.
|
||||
*/
|
||||
class RefundPayer {
|
||||
|
||||
/**
|
||||
* The email address.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $email_address;
|
||||
|
||||
/**
|
||||
* The merchant id.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $merchant_id;
|
||||
|
||||
/**
|
||||
* RefundPayer constructor.
|
||||
*
|
||||
* @param string $email_address The email.
|
||||
* @param string $merchant_id The merchant id.
|
||||
*/
|
||||
public function __construct(
|
||||
string $email_address,
|
||||
string $merchant_id
|
||||
) {
|
||||
|
||||
$this->email_address = $email_address;
|
||||
$this->merchant_id = $merchant_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the email address.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function email_address(): string {
|
||||
return $this->email_address;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the merchant id.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function merchant_id(): string {
|
||||
return $this->merchant_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() {
|
||||
$payer = array(
|
||||
'email_address' => $this->email_address(),
|
||||
);
|
||||
if ( $this->merchant_id ) {
|
||||
$payer['merchant_id'] = $this->merchant_id();
|
||||
}
|
||||
return $payer;
|
||||
}
|
||||
}
|
|
@ -16,10 +16,10 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
|||
*/
|
||||
class RefundStatus {
|
||||
|
||||
const COMPLETED = 'COMPLETED';
|
||||
const CANCELLED = 'CANCELLED';
|
||||
const FAILED = 'FAILED';
|
||||
const PENDING = 'PENDING';
|
||||
const COMPLETED = 'COMPLETED';
|
||||
const CANCELLED = 'CANCELLED';
|
||||
const FAILED = 'FAILED';
|
||||
const PENDING = 'PENDING';
|
||||
|
||||
/**
|
||||
* The status.
|
||||
|
@ -28,13 +28,22 @@ class RefundStatus {
|
|||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* The details.
|
||||
*
|
||||
* @var RefundStatusDetails|null
|
||||
*/
|
||||
private $details;
|
||||
|
||||
/**
|
||||
* RefundStatus constructor.
|
||||
*
|
||||
* @param string $status The status.
|
||||
* @param string $status The status.
|
||||
* @param RefundStatusDetails|null $details The details.
|
||||
*/
|
||||
public function __construct( string $status ) {
|
||||
public function __construct( string $status, ?RefundStatusDetails $details = null ) {
|
||||
$this->status = $status;
|
||||
$this->details = $details;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,4 +65,13 @@ class RefundStatus {
|
|||
public function name(): string {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the details.
|
||||
*
|
||||
* @return RefundStatusDetails|null
|
||||
*/
|
||||
public function details(): ?RefundStatusDetails {
|
||||
return $this->details;
|
||||
}
|
||||
}
|
||||
|
|
71
modules/ppcp-api-client/src/Entity/RefundStatusDetails.php
Normal file
71
modules/ppcp-api-client/src/Entity/RefundStatusDetails.php
Normal file
|
@ -0,0 +1,71 @@
|
|||
<?php
|
||||
/**
|
||||
* The RefundStatusDetails object.
|
||||
*
|
||||
* @see https://developer.paypal.com/docs/api/payments/v2/#definition-refund_status_details
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class RefundStatusDetails
|
||||
*/
|
||||
class RefundStatusDetails {
|
||||
|
||||
const ECHECK = 'ECHECK';
|
||||
|
||||
/**
|
||||
* The reason.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $reason;
|
||||
|
||||
/**
|
||||
* RefundStatusDetails constructor.
|
||||
*
|
||||
* @param string $reason The reason explaining refund status.
|
||||
*/
|
||||
public function __construct( string $reason ) {
|
||||
$this->reason = $reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares the current reason with a given one.
|
||||
*
|
||||
* @param string $reason The reason to compare with.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function is( string $reason ): bool {
|
||||
return $this->reason === $reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the reason explaining refund status.
|
||||
* One of RefundStatusDetails constants.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function reason(): string {
|
||||
return $this->reason;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the human-readable reason text explaining refund status.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function text(): string {
|
||||
switch ( $this->reason ) {
|
||||
case self::ECHECK:
|
||||
return __( 'The payer paid by an eCheck that has not yet cleared.', 'woocommerce-paypal-payments' );
|
||||
default:
|
||||
return $this->reason;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -17,7 +17,7 @@ class SellerPayableBreakdown {
|
|||
/**
|
||||
* The amount for this refunded payment in the currency of the transaction.
|
||||
*
|
||||
* @var Money
|
||||
* @var Money|null
|
||||
*/
|
||||
private $gross_amount;
|
||||
|
||||
|
@ -47,11 +47,15 @@ class SellerPayableBreakdown {
|
|||
private $net_amount;
|
||||
|
||||
/**
|
||||
* The net amount for this refunded payment in the receivable currency.
|
||||
*
|
||||
* @var Money|null
|
||||
*/
|
||||
private $net_amount_in_receivable_currency;
|
||||
|
||||
/**
|
||||
* The total amount for this refund.
|
||||
*
|
||||
* @var Money|null
|
||||
*/
|
||||
private $total_refunded_amount;
|
||||
|
@ -63,140 +67,136 @@ class SellerPayableBreakdown {
|
|||
*/
|
||||
private $platform_fees;
|
||||
|
||||
// /**
|
||||
// * SellerReceivableBreakdown constructor.
|
||||
// *
|
||||
// * @param Money $gross_amount The amount for this captured payment in the currency of the transaction.
|
||||
// * @param Money|null $paypal_fee The applicable fee for this captured payment in the currency of the transaction.
|
||||
// * @param Money|null $paypal_fee_in_receivable_currency The applicable fee for this captured payment in the receivable currency.
|
||||
// * @param Money|null $net_amount The net amount that the payee receives for this captured payment in their PayPal account.
|
||||
// * @param Money|null $receivable_amount The net amount that is credited to the payee's PayPal account.
|
||||
// * @param ExchangeRate|null $exchange_rate The exchange rate that determines the amount that is credited to the payee's PayPal account.
|
||||
// * @param PlatformFee[] $platform_fees An array of platform or partner fees, commissions, or brokerage fees that associated with the captured payment.
|
||||
// */
|
||||
// public function __construct(
|
||||
// Money $gross_amount,
|
||||
// ?Money $paypal_fee,
|
||||
// ?Money $paypal_fee_in_receivable_currency,
|
||||
// ?Money $net_amount,
|
||||
// ?Money $receivable_amount,
|
||||
// ?ExchangeRate $exchange_rate,
|
||||
// array $platform_fees
|
||||
// ) {
|
||||
// $this->gross_amount = $gross_amount;
|
||||
// $this->paypal_fee = $paypal_fee;
|
||||
// $this->paypal_fee_in_receivable_currency = $paypal_fee_in_receivable_currency;
|
||||
// $this->net_amount = $net_amount;
|
||||
// $this->receivable_amount = $receivable_amount;
|
||||
// $this->exchange_rate = $exchange_rate;
|
||||
// $this->platform_fees = $platform_fees;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * The amount for this captured payment in the currency of the transaction.
|
||||
// *
|
||||
// * @return Money
|
||||
// */
|
||||
// public function gross_amount(): ?Money {
|
||||
// return $this->gross_amount;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * The applicable fee for this captured payment in the currency of the transaction.
|
||||
// *
|
||||
// * @return Money|null
|
||||
// */
|
||||
// public function paypal_fee(): ?Money {
|
||||
// return $this->paypal_fee;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * The applicable fee for this captured payment in the receivable currency.
|
||||
// *
|
||||
// * Present only in cases the fee is charged in the receivable currency.
|
||||
// *
|
||||
// * @return Money|null
|
||||
// */
|
||||
// public function paypal_fee_in_receivable_currency(): ?Money {
|
||||
// return $this->paypal_fee_in_receivable_currency;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * The net amount that the payee receives for this captured payment in their PayPal account.
|
||||
// *
|
||||
// * Computed as gross_amount minus the paypal_fee minus the platform_fees.
|
||||
// *
|
||||
// * @return Money|null
|
||||
// */
|
||||
// public function net_amount(): ?Money {
|
||||
// return $this->net_amount;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * The net amount that is credited to the payee's PayPal account.
|
||||
// *
|
||||
// * Present only when the currency of the captured payment is different from the currency
|
||||
// * of the PayPal account where the payee wants to credit the funds. Computed as net_amount times exchange_rate.
|
||||
// *
|
||||
// * @return Money|null
|
||||
// */
|
||||
// public function receivable_amount(): ?Money {
|
||||
// return $this->receivable_amount;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * The exchange rate that determines the amount that is credited to the payee's PayPal account.
|
||||
// *
|
||||
// * Present when the currency of the captured payment is different from the currency of the PayPal account where the payee wants to credit the funds.
|
||||
// *
|
||||
// * @return ExchangeRate|null
|
||||
// */
|
||||
// public function exchange_rate(): ?ExchangeRate {
|
||||
// return $this->exchange_rate;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * An array of platform or partner fees, commissions, or brokerage fees that associated with the captured payment.
|
||||
// *
|
||||
// * @return PlatformFee[]
|
||||
// */
|
||||
// public function platform_fees(): array {
|
||||
// return $this->platform_fees;
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * Returns the object as array.
|
||||
// *
|
||||
// * @return array
|
||||
// */
|
||||
// public function to_array(): array {
|
||||
// $data = array(
|
||||
// 'gross_amount' => $this->gross_amount->to_array(),
|
||||
// );
|
||||
// if ( $this->paypal_fee ) {
|
||||
// $data['paypal_fee'] = $this->paypal_fee->to_array();
|
||||
// }
|
||||
// if ( $this->paypal_fee_in_receivable_currency ) {
|
||||
// $data['paypal_fee_in_receivable_currency'] = $this->paypal_fee_in_receivable_currency->to_array();
|
||||
// }
|
||||
// if ( $this->net_amount ) {
|
||||
// $data['net_amount'] = $this->net_amount->to_array();
|
||||
// }
|
||||
// if ( $this->receivable_amount ) {
|
||||
// $data['receivable_amount'] = $this->receivable_amount->to_array();
|
||||
// }
|
||||
// if ( $this->exchange_rate ) {
|
||||
// $data['exchange_rate'] = $this->exchange_rate->to_array();
|
||||
// }
|
||||
// if ( $this->platform_fees ) {
|
||||
// $data['platform_fees'] = array_map(
|
||||
// function ( PlatformFee $fee ) {
|
||||
// return $fee->to_array();
|
||||
// },
|
||||
// $this->platform_fees
|
||||
// );
|
||||
// }
|
||||
//
|
||||
// return $data;
|
||||
// }
|
||||
/**
|
||||
* SellerPayableBreakdown constructor.
|
||||
*
|
||||
* @param Money|null $gross_amount The amount for this refunded payment in the currency of the transaction.
|
||||
* @param Money|null $paypal_fee The applicable fee for this refunded payment in the currency of the transaction.
|
||||
* @param Money|null $paypal_fee_in_receivable_currency The applicable fee for this refunded payment in the receivable currency.
|
||||
* @param Money|null $net_amount The net amount that the payee receives for this refunded payment in their PayPal account.
|
||||
* @param Money|null $net_amount_in_receivable_currency The net amount for this refunded payment in the receivable currency.
|
||||
* @param Money|null $total_refunded_amount The total amount for this refund.
|
||||
* @param PlatformFee[] $platform_fees An array of platform or partner fees, commissions, or brokerage fees that associated with the captured payment.
|
||||
*/
|
||||
public function __construct(
|
||||
?Money $gross_amount,
|
||||
?Money $paypal_fee,
|
||||
?Money $paypal_fee_in_receivable_currency,
|
||||
?Money $net_amount,
|
||||
?Money $net_amount_in_receivable_currency,
|
||||
?Money $total_refunded_amount,
|
||||
array $platform_fees
|
||||
) {
|
||||
$this->gross_amount = $gross_amount;
|
||||
$this->paypal_fee = $paypal_fee;
|
||||
$this->paypal_fee_in_receivable_currency = $paypal_fee_in_receivable_currency;
|
||||
$this->net_amount = $net_amount;
|
||||
$this->net_amount_in_receivable_currency = $net_amount_in_receivable_currency;
|
||||
$this->total_refunded_amount = $total_refunded_amount;
|
||||
$this->platform_fees = $platform_fees;
|
||||
}
|
||||
|
||||
/**
|
||||
* The amount for this refunded payment in the currency of the transaction.
|
||||
*
|
||||
* @return Money
|
||||
*/
|
||||
public function gross_amount(): ?Money {
|
||||
return $this->gross_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* The applicable fee for this refunded payment in the currency of the transaction.
|
||||
*
|
||||
* @return Money|null
|
||||
*/
|
||||
public function paypal_fee(): ?Money {
|
||||
return $this->paypal_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* The applicable fee for this refunded payment in the receivable currency.
|
||||
*
|
||||
* Present only in cases the fee is charged in the receivable currency.
|
||||
*
|
||||
* @return Money|null
|
||||
*/
|
||||
public function paypal_fee_in_receivable_currency(): ?Money {
|
||||
return $this->paypal_fee_in_receivable_currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* The net amount that the payee receives for this refunded payment in their PayPal account.
|
||||
*
|
||||
* Computed as gross_amount minus the paypal_fee minus the platform_fees.
|
||||
*
|
||||
* @return Money|null
|
||||
*/
|
||||
public function net_amount(): ?Money {
|
||||
return $this->net_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* The net amount for this refunded payment in the receivable currency.
|
||||
*
|
||||
* @return Money|null
|
||||
*/
|
||||
public function net_amount_in_receivable_currency(): ?Money {
|
||||
return $this->net_amount_in_receivable_currency;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total amount for this refund.
|
||||
*
|
||||
* @return Money|null
|
||||
*/
|
||||
public function total_refunded_amount(): ?Money {
|
||||
return $this->total_refunded_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* An array of platform or partner fees, commissions, or brokerage fees that associated with the refunded payment.
|
||||
*
|
||||
* @return PlatformFee[]
|
||||
*/
|
||||
public function platform_fees(): array {
|
||||
return $this->platform_fees;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array(): array {
|
||||
$data = array();
|
||||
if ( $this->gross_amount ) {
|
||||
$data['gross_amount'] = $this->gross_amount->to_array();
|
||||
}
|
||||
if ( $this->paypal_fee ) {
|
||||
$data['paypal_fee'] = $this->paypal_fee->to_array();
|
||||
}
|
||||
if ( $this->paypal_fee_in_receivable_currency ) {
|
||||
$data['paypal_fee_in_receivable_currency'] = $this->paypal_fee_in_receivable_currency->to_array();
|
||||
}
|
||||
if ( $this->net_amount ) {
|
||||
$data['net_amount'] = $this->net_amount->to_array();
|
||||
}
|
||||
if ( $this->net_amount_in_receivable_currency ) {
|
||||
$data['net_amount_in_receivable_currency'] = $this->net_amount_in_receivable_currency->to_array();
|
||||
}
|
||||
if ( $this->total_refunded_amount ) {
|
||||
$data['total_refunded_amount'] = $this->total_refunded_amount->to_array();
|
||||
}
|
||||
if ( $this->platform_fees ) {
|
||||
$data['platform_fees'] = array_map(
|
||||
function ( PlatformFee $fee ) {
|
||||
return $fee->to_array();
|
||||
},
|
||||
$this->platform_fees
|
||||
);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,7 +45,7 @@ class PaymentsFactory {
|
|||
*
|
||||
* @param AuthorizationFactory $authorization_factory The Authorization factory.
|
||||
* @param CaptureFactory $capture_factory The Capture factory.
|
||||
* @param RefundFactory $refund_factory The Refund factory.
|
||||
* @param RefundFactory $refund_factory The Refund factory.
|
||||
*/
|
||||
public function __construct(
|
||||
AuthorizationFactory $authorization_factory,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* The payment refund factory.
|
||||
* The refund factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
@ -10,34 +10,82 @@ declare( strict_types=1 );
|
|||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Refund;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundStatusDetails;
|
||||
|
||||
/**
|
||||
* Class RefundFactory
|
||||
*/
|
||||
class RefundFactory {
|
||||
|
||||
/**
|
||||
* The Amount factory.
|
||||
*
|
||||
* @var AmountFactory
|
||||
*/
|
||||
private $amount_factory;
|
||||
|
||||
/**
|
||||
* The SellerPayableBreakdownFactory factory.
|
||||
*
|
||||
* @var SellerPayableBreakdownFactory
|
||||
*/
|
||||
private $seller_payable_breakdown_factory;
|
||||
|
||||
/**
|
||||
* The RefundPayerFactory factory.
|
||||
*
|
||||
* @var RefundPayerFactory
|
||||
*/
|
||||
private $refund_payer_factory;
|
||||
|
||||
/**
|
||||
* RefundFactory constructor.
|
||||
*
|
||||
* @param AmountFactory $amount_factory The amount factory.
|
||||
* @param AmountFactory $amount_factory The amount factory.
|
||||
* @param SellerPayableBreakdownFactory $seller_payable_breakdown_factory The payable breakdown factory.
|
||||
* @param RefundPayerFactory $refund_payer_factory The payer breakdown factory.
|
||||
*/
|
||||
public function __construct(
|
||||
AmountFactory $amount_factory
|
||||
AmountFactory $amount_factory,
|
||||
SellerPayableBreakdownFactory $seller_payable_breakdown_factory,
|
||||
RefundPayerFactory $refund_payer_factory
|
||||
) {
|
||||
$this->amount_factory = $amount_factory;
|
||||
|
||||
$this->amount_factory = $amount_factory;
|
||||
$this->seller_payable_breakdown_factory = $seller_payable_breakdown_factory;
|
||||
$this->refund_payer_factory = $refund_payer_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the payment refund object based off the PayPal response.
|
||||
* Returns the refund object based off the PayPal response.
|
||||
*
|
||||
* @param \stdClass $data The PayPal response.
|
||||
*
|
||||
* @return Refund
|
||||
*/
|
||||
public function from_paypal_response( \stdClass $data ) : Refund {
|
||||
$reason = $data->status_details->reason ?? null;
|
||||
$seller_payable_breakdown = isset( $data->seller_payable_breakdown ) ?
|
||||
$this->seller_payable_breakdown_factory->from_paypal_response( $data->seller_payable_breakdown )
|
||||
: null;
|
||||
|
||||
$payer = isset( $data->payer ) ?
|
||||
$this->refund_payer_factory->from_paypal_response( $data->payer )
|
||||
: null;
|
||||
|
||||
return new Refund(
|
||||
(string) $data->id,
|
||||
new RefundStatus(
|
||||
(string) $data->status,
|
||||
$reason ? new RefundStatusDetails( $reason ) : null
|
||||
),
|
||||
$this->amount_factory->from_paypal_response( $data->amount ),
|
||||
(string) ( $data->invoice_id ?? '' ),
|
||||
(string) ( $data->custom_id ?? '' ),
|
||||
$seller_payable_breakdown,
|
||||
(string) ( $data->acquirer_reference_number ?? '' ),
|
||||
(string) ( $data->note_to_payer ?? '' ),
|
||||
$payer
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
39
modules/ppcp-api-client/src/Factory/RefundPayerFactory.php
Normal file
39
modules/ppcp-api-client/src/Factory/RefundPayerFactory.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
/**
|
||||
* The RefundPayerFactory factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Address;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerName;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PayerTaxInfo;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Phone;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PhoneWithType;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\RefundPayer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class RefundPayerFactory
|
||||
*/
|
||||
class RefundPayerFactory {
|
||||
|
||||
/**
|
||||
* Returns a Refund Payer object based off a PayPal Response.
|
||||
*
|
||||
* @param \stdClass $data The JSON object.
|
||||
*
|
||||
* @return RefundPayer
|
||||
*/
|
||||
public function from_paypal_response( \stdClass $data ): RefundPayer {
|
||||
return new RefundPayer(
|
||||
isset( $data->email_address ) ? $data->email_address : '',
|
||||
isset( $data->merchant_id ) ? $data->merchant_id : ''
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,81 @@
|
|||
<?php
|
||||
/**
|
||||
* The SellerPayableBreakdownFactory Factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PlatformFee;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerPayableBreakdown;
|
||||
|
||||
/**
|
||||
* Class SellerPayableBreakdownFactory
|
||||
*/
|
||||
class SellerPayableBreakdownFactory {
|
||||
|
||||
/**
|
||||
* The Money factory.
|
||||
*
|
||||
* @var MoneyFactory
|
||||
*/
|
||||
private $money_factory;
|
||||
|
||||
/**
|
||||
* The PlatformFee factory.
|
||||
*
|
||||
* @var PlatformFeeFactory
|
||||
*/
|
||||
private $platform_fee_factory;
|
||||
|
||||
/**
|
||||
* SellerPayableBreakdownFactory constructor.
|
||||
*
|
||||
* @param MoneyFactory $money_factory The Money factory.
|
||||
* @param PlatformFeeFactory $platform_fee_factory The PlatformFee factory.
|
||||
*/
|
||||
public function __construct(
|
||||
MoneyFactory $money_factory,
|
||||
PlatformFeeFactory $platform_fee_factory
|
||||
) {
|
||||
$this->money_factory = $money_factory;
|
||||
$this->platform_fee_factory = $platform_fee_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a SellerPayableBreakdownFactory object based off a PayPal Response.
|
||||
*
|
||||
* @param stdClass $data The JSON object.
|
||||
*
|
||||
* @return SellerPayableBreakdown
|
||||
*/
|
||||
public function from_paypal_response( stdClass $data ): SellerPayableBreakdown {
|
||||
|
||||
$gross_amount = ( isset( $data->gross_amount ) ) ? $this->money_factory->from_paypal_response( $data->gross_amount ) : null;
|
||||
$paypal_fee = ( isset( $data->paypal_fee ) ) ? $this->money_factory->from_paypal_response( $data->paypal_fee ) : null;
|
||||
$paypal_fee_in_receivable_currency = ( isset( $data->paypal_fee_in_receivable_currency ) ) ? $this->money_factory->from_paypal_response( $data->paypal_fee_in_receivable_currency ) : null;
|
||||
$net_amount = ( isset( $data->net_amount ) ) ? $this->money_factory->from_paypal_response( $data->net_amount ) : null;
|
||||
$net_amount_in_receivable_currency = ( isset( $data->net_amount_in_receivable_currency ) ) ? $this->money_factory->from_paypal_response( $data->net_amount_in_receivable_currency ) : null;
|
||||
$total_refunded_amount = ( isset( $data->total_refunded_amount ) ) ? $this->money_factory->from_paypal_response( $data->total_refunded_amount ) : null;
|
||||
$platform_fees = ( isset( $data->platform_fees ) ) ? array_map(
|
||||
function ( stdClass $fee_data ): PlatformFee {
|
||||
return $this->platform_fee_factory->from_paypal_response( $fee_data );
|
||||
},
|
||||
$data->platform_fees
|
||||
) : array();
|
||||
|
||||
return new SellerPayableBreakdown(
|
||||
$gross_amount,
|
||||
$paypal_fee,
|
||||
$paypal_fee_in_receivable_currency,
|
||||
$net_amount,
|
||||
$net_amount_in_receivable_currency,
|
||||
$total_refunded_amount,
|
||||
$platform_fees
|
||||
);
|
||||
}
|
||||
}
|
|
@ -24,13 +24,18 @@ class FeesRenderer {
|
|||
* @return string
|
||||
*/
|
||||
public function render( WC_Order $wc_order ) : string {
|
||||
$breakdown = $wc_order->get_meta( PayPalGateway::FEES_META_KEY );
|
||||
$breakdown = $wc_order->get_meta( PayPalGateway::FEES_META_KEY );
|
||||
$refund_breakdown = $wc_order->get_meta( PayPalGateway::REFUND_FEES_META_KEY ) ?: array();
|
||||
|
||||
if ( ! is_array( $breakdown ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$refund_fee = $refund_breakdown['paypal_fee'] ?? array();
|
||||
$refund_amount = $refund_breakdown['net_amount'] ?? array();
|
||||
$refund_total = ( $refund_fee['value'] ?? 0 ) + ( $refund_amount['value'] ?? 0 );
|
||||
$refund_currency = ( $refund_amount['currency_code'] === $refund_fee['currency_code'] ) ? $refund_amount['currency_code'] : '';
|
||||
|
||||
$html = '';
|
||||
|
||||
$fee = $breakdown['paypal_fee'] ?? null;
|
||||
|
@ -44,14 +49,7 @@ class FeesRenderer {
|
|||
);
|
||||
}
|
||||
|
||||
$refund_total = 0;
|
||||
$refund_currency = null;
|
||||
|
||||
$refund_fee = $refund_breakdown['paypal_fee'] ?? null;
|
||||
if ( is_array( $refund_fee ) ) {
|
||||
$refund_total += $refund_fee['value'];
|
||||
$refund_currency = $refund_fee['currency_code'];
|
||||
|
||||
if ( $refund_fee ) {
|
||||
$html .= $this->render_money_row(
|
||||
__( 'PayPal Refund Fee:', 'woocommerce-paypal-payments' ),
|
||||
__( 'The fee PayPal collects for the refund transactions.', 'woocommerce-paypal-payments' ),
|
||||
|
@ -62,16 +60,7 @@ class FeesRenderer {
|
|||
);
|
||||
}
|
||||
|
||||
$refund_amount = $refund_breakdown['net_amount'] ?? null;
|
||||
if ( is_array( $refund_amount ) ) {
|
||||
$refund_total += $refund_amount['value'];
|
||||
|
||||
if ( null === $refund_currency ) {
|
||||
$refund_currency = $refund_fee['currency_code'];
|
||||
} else if ( $refund_currency !== $refund_fee['currency_code'] ) {
|
||||
$refund_currency = false;
|
||||
}
|
||||
|
||||
if ( $refund_amount ) {
|
||||
$html .= $this->render_money_row(
|
||||
__( 'PayPal Refunded:', 'woocommerce-paypal-payments' ),
|
||||
__( 'The net amount that was refunded.', 'woocommerce-paypal-payments' ),
|
||||
|
|
|
@ -227,10 +227,12 @@ class IncomingWebhookEndpoint {
|
|||
|
||||
foreach ( $this->handlers as $handler ) {
|
||||
if ( $handler->responsible_for_request( $request ) ) {
|
||||
$event_type = ( $handler->event_types() ? current( $handler->event_types() ) : '' ) ?: '';
|
||||
|
||||
$this->logger->debug(
|
||||
sprintf(
|
||||
'Webhook is going to be handled by %s on %s',
|
||||
( $handler->event_types() ) ? current( $handler->event_types() ) : '',
|
||||
$event_type,
|
||||
get_class( $handler )
|
||||
)
|
||||
);
|
||||
|
@ -238,7 +240,7 @@ class IncomingWebhookEndpoint {
|
|||
$this->logger->info(
|
||||
sprintf(
|
||||
'Webhook has been handled by %s on %s',
|
||||
( $handler->event_types() ) ? current( $handler->event_types() ) : '',
|
||||
$event_type,
|
||||
get_class( $handler )
|
||||
)
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue