Skip to content

Commit

Permalink
Add selector function for json_decode.
Browse files Browse the repository at this point in the history
  • Loading branch information
srmklive committed Sep 30, 2023
1 parent 27e8f80 commit 6f66e66
Show file tree
Hide file tree
Showing 68 changed files with 237 additions and 214 deletions.
7 changes: 5 additions & 2 deletions src/Services/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
namespace Srmklive\PayPal\Services;

use GuzzleHttp\Utils;
use Srmklive\PayPal\Traits\JsonDecodeSelector;

class Str extends \Illuminate\Support\Str
{
use JsonDecodeSelector;

/**
* Determine if a given value is valid JSON.
*
Expand All @@ -24,8 +27,8 @@ public static function isJson($value): bool
}

try {
Utils::jsonDecode($value, true, 512, 4194304);
} catch (\JsonException $jsonException) {
(new Str)->jsonDecodeFunction()($value, true, 512, 4194304);
} catch (\Exception $jsonException) {
return false;
}

Expand Down
16 changes: 16 additions & 0 deletions src/Traits/JsonDecodeSelector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Srmklive\PayPal\Traits;

trait JsonDecodeSelector
{
/**
* Decide the function to use for decoding json.
*
* @return string
*/
protected function jsonDecodeFunction(): string
{
return class_exists(\GuzzleHttp\Utils::class) ? '\GuzzleHttp\Utils::jsonDecode' : '\GuzzleHttp\Utils::json_decode';
}
}
4 changes: 2 additions & 2 deletions src/Traits/PayPalHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,9 +210,9 @@ private function doPayPalRequest(bool $decode = true)
// Perform PayPal HTTP API request.
$response = $this->makeHttpRequest();

return ($decode === false) ? $response->getContents() : Utils::jsonDecode($response, true);
return ($decode === false) ? $response->getContents() : $this->jsonDecodeFunction()($response, true);
} catch (RuntimeException $t) {
$error = ($decode === false) || (Str::isJson($t->getMessage()) === false) ? $t->getMessage() : Utils::jsonDecode($t->getMessage(), true);
$error = ($decode === false) || (Str::isJson($t->getMessage()) === false) ? $t->getMessage() : $this->jsonDecodeFunction()($t->getMessage(), true);

return ['error' => $error];
}
Expand Down
1 change: 1 addition & 0 deletions src/Traits/PayPalRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

trait PayPalRequest
{
use JsonDecodeSelector;
use PayPalHttpClient;
use PayPalAPI;
use PayPalExperienceContext;
Expand Down
3 changes: 3 additions & 0 deletions tests/MockClientClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@
use GuzzleHttp\Utils;
use Psr\Http\Message\ResponseInterface;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
use Srmklive\PayPal\Traits\JsonDecodeSelector;

trait MockClientClasses
{
use JsonDecodeSelector;

private function mock_http_client($response): HttpClient
{
$mock = new HttpMockHandler([
Expand Down
6 changes: 3 additions & 3 deletions tests/Mocks/Requests/BillingPlans.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait BillingPlans
*/
private function createPlanParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"product_id": "PROD-XXCD1234QWER65782",
"name": "Video Streaming Service Plan",
"description": "Video Streaming Service basic plan",
Expand Down Expand Up @@ -84,7 +84,7 @@ private function createPlanParams(): array
*/
private function updatePlanParams(): array
{
return Utils::jsonDecode('[
return $this->jsonDecodeFunction()('[
{
"op": "replace",
"path": "/payment_preferences/payment_failure_threshold",
Expand All @@ -98,7 +98,7 @@ private function updatePlanParams(): array
*/
private function updatePlanPricingParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"pricing_schemes": [
{
"billing_cycle_sequence": 2,
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/CatalogProducts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait CatalogProducts
*/
private function createProductParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"name": "Video Streaming Service",
"description": "Video streaming service",
"type": "SERVICE",
Expand All @@ -26,7 +26,7 @@ private function createProductParams(): array
*/
private function updateProductParams(): array
{
return Utils::jsonDecode('[
return $this->jsonDecodeFunction()('[
{
"op": "replace",
"path": "/description",
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Requests/Disputes.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Disputes
*/
protected function updateDisputeParams(): array
{
return Utils::jsonDecode('[
return $this->jsonDecodeFunction()('[
{
"op": "add",
"path": "/partner_actions/-",
Expand Down
6 changes: 3 additions & 3 deletions tests/Mocks/Requests/DisputesActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait DisputesActions
*/
protected function acceptDisputeClaimParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"note": "Full refund to the customer.",
"accept_claim_type": "REFUND"
}', true);
Expand All @@ -22,7 +22,7 @@ protected function acceptDisputeClaimParams(): array
*/
protected function acceptDisputeResolutionParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"note": "I am ok with the refund offered."
}', true);
}
Expand All @@ -32,7 +32,7 @@ protected function acceptDisputeResolutionParams(): array
*/
protected function acknowledgeItemReturnedParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"note": "I have received the item back.",
"acknowledgement_type": "ITEM_RECEIVED"
}', true);
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/Identity.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait Identity
{
private function mockCreateMerchantApplicationParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"redirect_uris": [
"https://example.com/callback",
"https://example.com/callback2"
Expand All @@ -29,7 +29,7 @@ private function mockCreateMerchantApplicationParams(): array

private function mockSetAccountPropertiesParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"categories": [
{
"name": "PAYMENT",
Expand Down
16 changes: 8 additions & 8 deletions tests/Mocks/Requests/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Invoices
*/
private function createInvoiceParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"detail": {
"invoice_number": "#123",
"reference": "deal-ref",
Expand Down Expand Up @@ -175,7 +175,7 @@ private function createInvoiceParams(): array
*/
private function updateInvoiceParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"id": "INV2-C82X-JNN9-Y6S5-CNXW",
"status": "DRAFT",
"detail": {
Expand Down Expand Up @@ -375,7 +375,7 @@ private function updateInvoiceParams(): array
*/
private function cancelInvoiceParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"subject": "Invoice Cancelled",
"note": "Cancelling the invoice",
"send_to_invoicer": true,
Expand All @@ -391,7 +391,7 @@ private function cancelInvoiceParams(): array
*/
private function generateQRCodeInvoiceParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"width": 400,
"height": 400
}', true);
Expand All @@ -402,7 +402,7 @@ private function generateQRCodeInvoiceParams(): array
*/
private function registerInvoicePaymentParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"width": 400,
"height": 400
}', true);
Expand All @@ -413,7 +413,7 @@ private function registerInvoicePaymentParams(): array
*/
private function refundInvoicePaymentParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"method": "BANK_TRANSFER",
"refund_date": "2018-05-21",
"amount": {
Expand All @@ -428,7 +428,7 @@ private function refundInvoicePaymentParams(): array
*/
private function sendInvoiceParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"subject": "Payment due for the invoice #ABC-123",
"note": "Please pay before the due date to avoid incurring late payment charges which will be adjusted in the next bill generated.",
"send_to_invoicer": true,
Expand All @@ -444,7 +444,7 @@ private function sendInvoiceParams(): array
*/
private function sendInvoiceReminderParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"subject": "Reminder: Payment due for the invoice #ABC-123",
"note": "Please pay before the due date to avoid incurring late payment charges which will be adjusted in the next bill generated.",
"send_to_invoicer": true,
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Requests/InvoicesSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait InvoicesSearch
*/
private function invoiceSearchParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"total_amount_range": {
"lower_amount": {
"currency_code": "USD",
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/InvoicesTemplates.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait InvoicesTemplates
*/
private function mockCreateInvoiceTemplateParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"default_template": true,
"template_info": {
"configuration": {
Expand Down Expand Up @@ -241,7 +241,7 @@ private function mockCreateInvoiceTemplateParams(): array
*/
private function mockUpdateInvoiceTemplateParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"default_template": true,
"template_info": {
"configuration": {
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/Orders.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Orders
*/
private function createOrderParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"intent": "CAPTURE",
"purchase_units": [
{
Expand All @@ -29,7 +29,7 @@ private function createOrderParams(): array
*/
private function updateOrderParams(): array
{
return Utils::jsonDecode('[
return $this->jsonDecodeFunction()('[
{
"op": "replace",
"path": "/purchase_units/@reference_id==\'PUHF\'/shipping/address",
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Requests/PartnerReferrals.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ trait PartnerReferrals
{
private function mockCreatePartnerReferralParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"individual_owners": [
{
"names": [
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/PaymentAuthorizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait PaymentAuthorizations
*/
private function mockCaptureAuthorizedPaymentParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"amount": {
"value": "10.99",
"currency_code": "USD"
Expand All @@ -27,7 +27,7 @@ private function mockCaptureAuthorizedPaymentParams(): array
*/
private function mockReAuthorizeAuthorizedPaymentParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"amount": {
"value": "10.99",
"currency_code": "USD"
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Requests/PaymentCaptures.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait PaymentCaptures
*/
private function mockRefundCapturedPaymentParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"amount": {
"value": "10.99",
"currency_code": "USD"
Expand Down
6 changes: 3 additions & 3 deletions tests/Mocks/Requests/PaymentExperienceWebProfiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait PaymentExperienceWebProfiles
*/
private function mockCreateWebProfileParams(): array
{
return Utils::jsonDecode('[
return $this->jsonDecodeFunction()('[
{
"id": "XP-GCUV-X35G-HNEY-5MJY",
"name": "exampleProfile",
Expand Down Expand Up @@ -64,7 +64,7 @@ private function mockCreateWebProfileParams(): array
*/
private function partiallyUpdateWebProfileParams(): array
{
return Utils::jsonDecode('[
return $this->jsonDecodeFunction()('[
{
"op": "add",
"path": "/presentation/brand_name",
Expand All @@ -82,7 +82,7 @@ private function partiallyUpdateWebProfileParams(): array
*/
private function updateWebProfileParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"name": "exampleProfile",
"presentation": {
"logo_image": "https://example.com/logo_image/"
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/PaymentMethodsTokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait PaymentMethodsTokens
*/
private function mockCreatePaymentSetupTokensParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"payment_source": {
"card": {
"number": "4111111111111111",
Expand Down Expand Up @@ -41,7 +41,7 @@ private function mockCreatePaymentSetupTokensParams(): array
*/
private function mockCreatePaymentSetupPayPalParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"payment_source": {
"paypal": {
"description": "Description for PayPal to be shown to PayPal payer",
Expand Down
2 changes: 1 addition & 1 deletion tests/Mocks/Requests/Payouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait Payouts
*/
private function mockCreateBatchPayoutParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"sender_batch_header": {
"sender_batch_id": "Payouts_2018_100007",
"email_subject": "You have a payout!",
Expand Down
4 changes: 2 additions & 2 deletions tests/Mocks/Requests/ReferencedPayouts.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trait ReferencedPayouts
*/
private function mockCreateReferencedBatchPayoutParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"referenced_payouts": [
{
"reference_id": "2KP03934U4415543C",
Expand All @@ -30,7 +30,7 @@ private function mockCreateReferencedBatchPayoutParams(): array
*/
private function mockCreateReferencedBatchPayoutItemParams(): array
{
return Utils::jsonDecode('{
return $this->jsonDecodeFunction()('{
"reference_id": "CAPTURETXNID",
"reference_type": "TRANSACTION_ID"
}', true);
Expand Down
Loading

0 comments on commit 6f66e66

Please sign in to comment.