mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Fix ExchangeRate parsing
This commit is contained in:
parent
c8a55581a0
commit
1ae473f297
1 changed files with 11 additions and 11 deletions
|
@ -22,20 +22,20 @@ class ExchangeRateFactory {
|
|||
*
|
||||
* @param stdClass $data The JSON object.
|
||||
*
|
||||
* @return ExchangeRate
|
||||
* @return ExchangeRate|null
|
||||
* @throws RuntimeException When JSON object is malformed.
|
||||
*/
|
||||
public function from_paypal_response( stdClass $data ): ExchangeRate {
|
||||
if ( ! isset( $data->source_currency ) ) {
|
||||
throw new RuntimeException( 'Exchange rate source currency not found' );
|
||||
}
|
||||
if ( ! isset( $data->target_currency ) ) {
|
||||
throw new RuntimeException( 'Exchange rate target currency not found' );
|
||||
}
|
||||
if ( ! isset( $data->value ) ) {
|
||||
throw new RuntimeException( 'Exchange rate value not found' );
|
||||
public function from_paypal_response( stdClass $data ): ?ExchangeRate {
|
||||
// Looks like all fields in this object are optional, according to the docs,
|
||||
// and sometimes we get an empty object.
|
||||
$source_currency = $data->source_currency ?? '';
|
||||
$target_currency = $data->target_currency ?? '';
|
||||
$value = $data->value ?? '';
|
||||
if ( ! $source_currency && ! $target_currency && ! $value ) {
|
||||
// Do not return empty object.
|
||||
return null;
|
||||
}
|
||||
|
||||
return new ExchangeRate( $data->source_currency, $data->target_currency, $data->value );
|
||||
return new ExchangeRate( $source_currency, $target_currency, $value );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue