|
433 | 433 | end |
434 | 434 |
|
435 | 435 | describe '#call_adyen_api' do |
| 436 | + let(:client) { Adyen::Client.new(api_key: 'test_key', env: :test) } |
| 437 | + let(:mock_faraday_connection) { double(Faraday::Connection) } |
| 438 | + |
| 439 | + before do |
| 440 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
| 441 | + allow(mock_faraday_connection).to receive(:adapter) |
| 442 | + end |
| 443 | + |
436 | 444 | it 'successfully makes a POST request and returns AdyenResult' do |
437 | | - client = Adyen::Client.new(api_key: 'test_key', env: :test) |
438 | | - mock_faraday_connection = double(Faraday::Connection) |
439 | 445 | response_body = { pspReference: '123456789', resultCode: 'Authorised' }.to_json |
440 | 446 | mock_response = Faraday::Response.new( |
441 | 447 | status: 200, |
|
446 | 452 | expect(Faraday).to receive(:new) |
447 | 453 | .with('https://checkout-test.adyen.com/v71/payments', anything) |
448 | 454 | .and_return(mock_faraday_connection) |
449 | | - allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
450 | 455 | allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
451 | 456 |
|
452 | 457 | result = client.call_adyen_api('Checkout', 'payments', { amount: { value: 1000 } }, {}, '71') |
|
457 | 462 | end |
458 | 463 |
|
459 | 464 | it 'successfully makes a GET request' do |
460 | | - client = Adyen::Client.new(api_key: 'test_key', env: :test) |
461 | | - mock_faraday_connection = double(Faraday::Connection) |
462 | 465 | response_body = { data: [{ id: '1' }] }.to_json |
463 | 466 | mock_response = Faraday::Response.new(status: 200, body: response_body, response_headers: {}) |
464 | 467 |
|
465 | 468 | expect(Faraday).to receive(:new) |
466 | | - .with('https://management-test.adyen.com/v1/companies', anything) |
| 469 | + .with('https://management-test.adyen.com/v1/merchants/MyMerchantID/paymentsApps', anything) |
467 | 470 | .and_return(mock_faraday_connection) |
468 | 471 | .and_yield(mock_faraday_connection) |
469 | | - allow(mock_faraday_connection).to receive(:adapter) |
470 | | - allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
471 | 472 | allow(mock_faraday_connection).to receive(:get).and_return(mock_response) |
472 | 473 |
|
473 | | - result = client.call_adyen_api('Management', { url: 'companies', method: 'get' }, {}, {}, '1') |
| 474 | + result = client.call_adyen_api( |
| 475 | + 'Management', |
| 476 | + { url: 'merchants/MyMerchantID/paymentsApps', method: 'get' }, |
| 477 | + {}, |
| 478 | + {}, |
| 479 | + '1' |
| 480 | + ) |
474 | 481 |
|
475 | 482 | expect(result).to be_a(Adyen::AdyenResult) |
476 | 483 | expect(result.status).to eq(200) |
477 | 484 | end |
478 | 485 |
|
479 | 486 | it 'sets correct headers including custom headers' do |
480 | | - client = Adyen::Client.new(api_key: 'test_key', env: :test) |
481 | | - mock_faraday_connection = double(Faraday::Connection) |
482 | 487 | mock_response = Faraday::Response.new(status: 200, body: '{}', response_headers: {}) |
483 | | - |
484 | 488 | headers_spy = {} |
| 489 | + |
485 | 490 | expect(Faraday).to receive(:new) |
486 | | - .with('https://checkout-test.adyen.com/v71/payments', anything) |
| 491 | + .with('https://checkout-test.adyen.com/v71/storedPaymentMethods', anything) |
487 | 492 | .and_return(mock_faraday_connection) |
488 | 493 | .and_yield(mock_faraday_connection) |
489 | | - allow(mock_faraday_connection).to receive(:adapter) |
490 | 494 | allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) do |key, value| |
491 | 495 | headers_spy[key] = value |
492 | 496 | end |
493 | 497 | allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
494 | 498 |
|
495 | 499 | custom_headers = { 'Idempotency-Key' => 'test-123' } |
496 | | - client.call_adyen_api('Checkout', 'payments', {}, custom_headers, '71') |
| 500 | + client.call_adyen_api('Checkout', 'storedPaymentMethods', {}, custom_headers, '71') |
497 | 501 |
|
498 | 502 | expect(headers_spy['Content-Type']).to eq('application/json') |
499 | 503 | expect(headers_spy['x-api-key']).to eq('test_key') |
500 | 504 | expect(headers_spy['Idempotency-Key']).to eq('test-123') |
501 | 505 | end |
502 | 506 |
|
503 | 507 | it 'handles connection failures' do |
504 | | - client = Adyen::Client.new(api_key: 'test_key', env: :test) |
505 | | - mock_faraday_connection = double(Faraday::Connection) |
506 | | - |
507 | 508 | expect(Faraday).to receive(:new) |
508 | | - .with('https://checkout-test.adyen.com/v71/payments', anything) |
| 509 | + .with('https://checkout-test.adyen.com/v71/paymentLinks/PL61C53A8B97E6924D', anything) |
509 | 510 | .and_return(mock_faraday_connection) |
510 | 511 | .and_yield(mock_faraday_connection) |
511 | | - allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
512 | | - allow(mock_faraday_connection).to receive(:adapter) |
513 | 512 | allow(mock_faraday_connection).to receive(:post).and_raise(Faraday::ConnectionFailed.new('Connection failed')) |
514 | 513 |
|
515 | 514 | expect { |
516 | | - client.call_adyen_api('Checkout', 'payments', {}, {}, '71') |
| 515 | + client.call_adyen_api('Checkout', 'paymentLinks/PL61C53A8B97E6924D', {}, {}, '71') |
517 | 516 | }.to raise_error(Faraday::ConnectionFailed, /Connection to .* failed/) |
518 | 517 | end |
519 | 518 |
|
520 | 519 | it 'converts request data to JSON' do |
521 | | - client = Adyen::Client.new(api_key: 'test_key', env: :test) |
522 | | - mock_faraday_connection = double(Faraday::Connection) |
523 | 520 | mock_response = Faraday::Response.new(status: 200, body: '{}', response_headers: {}) |
524 | 521 |
|
525 | 522 | expect(Faraday).to receive(:new) |
526 | 523 | .with('https://checkout-test.adyen.com/v71/payments', anything) |
527 | 524 | .and_return(mock_faraday_connection) |
528 | 525 | .and_yield(mock_faraday_connection) |
529 | | - allow(mock_faraday_connection).to receive(:adapter) |
530 | | - allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
531 | 526 |
|
532 | 527 | request_body_sent = nil |
533 | 528 | allow(mock_faraday_connection).to receive(:post) do |&block| |
|
543 | 538 | expect(request_body_sent).to eq(hash_data.to_json) |
544 | 539 | end |
545 | 540 | end |
| 541 | + |
| 542 | + describe '#call_adyen_api_url' do |
| 543 | + let(:client) { Adyen::Client.new(api_key: 'test_key', env: :test) } |
| 544 | + let(:mock_faraday_connection) { double(Faraday::Connection) } |
| 545 | + |
| 546 | + before do |
| 547 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) |
| 548 | + allow(mock_faraday_connection).to receive(:adapter) |
| 549 | + end |
| 550 | + |
| 551 | + it 'successfully makes a POST request with full URL' do |
| 552 | + response_body = { pspReference: '987654321', resultCode: 'Authorised' }.to_json |
| 553 | + mock_response = Faraday::Response.new( |
| 554 | + status: 200, |
| 555 | + body: response_body, |
| 556 | + response_headers: { 'content-type' => 'application/json' } |
| 557 | + ) |
| 558 | + |
| 559 | + expect(Faraday).to receive(:new) |
| 560 | + .with('https://balanceplatform-api-test.adyen.com/capital/v1/grants', anything) |
| 561 | + .and_return(mock_faraday_connection) |
| 562 | + .and_yield(mock_faraday_connection) |
| 563 | + allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
| 564 | + |
| 565 | + result = client.call_adyen_api_url( |
| 566 | + 'https://balanceplatform-api-test.adyen.com/capital/v1/grants', |
| 567 | + 'post', |
| 568 | + { amount: { value: 2000 } }, |
| 569 | + {} |
| 570 | + ) |
| 571 | + |
| 572 | + expect(result).to be_a(Adyen::AdyenResult) |
| 573 | + expect(result.status).to eq(200) |
| 574 | + expect(result.response["pspReference"]).to eq('987654321') |
| 575 | + end |
| 576 | + |
| 577 | + it 'successfully makes a GET request' do |
| 578 | + response_body = { data: [{ id: 'comp-1' }] }.to_json |
| 579 | + mock_response = Faraday::Response.new(status: 200, body: response_body, response_headers: {}) |
| 580 | + |
| 581 | + expect(Faraday).to receive(:new) |
| 582 | + .with('https://management-test.adyen.com/v3/merchants/MyMerchantID/stores', anything) |
| 583 | + .and_return(mock_faraday_connection) |
| 584 | + .and_yield(mock_faraday_connection) |
| 585 | + allow(mock_faraday_connection).to receive(:get).and_return(mock_response) |
| 586 | + |
| 587 | + result = client.call_adyen_api_url( |
| 588 | + 'https://management-test.adyen.com/v3/merchants/MyMerchantID/stores', |
| 589 | + 'get', |
| 590 | + {}, |
| 591 | + {} |
| 592 | + ) |
| 593 | + |
| 594 | + expect(result).to be_a(Adyen::AdyenResult) |
| 595 | + expect(result.status).to eq(200) |
| 596 | + end |
| 597 | + |
| 598 | + it 'sets correct headers' do |
| 599 | + mock_response = Faraday::Response.new(status: 200, body: '{}', response_headers: {}) |
| 600 | + headers_spy = {} |
| 601 | + |
| 602 | + expect(Faraday).to receive(:new) |
| 603 | + .with('https://custom.adyen.com/api/endpoint', anything) |
| 604 | + .and_return(mock_faraday_connection) |
| 605 | + .and_yield(mock_faraday_connection) |
| 606 | + allow(mock_faraday_connection).to receive_message_chain(:headers, :[]=) do |key, value| |
| 607 | + headers_spy[key] = value |
| 608 | + end |
| 609 | + allow(mock_faraday_connection).to receive(:post).and_return(mock_response) |
| 610 | + |
| 611 | + custom_headers = { 'X-Custom-Header' => 'custom-value' } |
| 612 | + client.call_adyen_api_url('https://custom.adyen.com/api/endpoint', 'post', {}, custom_headers) |
| 613 | + |
| 614 | + expect(headers_spy['Content-Type']).to eq('application/json') |
| 615 | + expect(headers_spy['x-api-key']).to eq('test_key') |
| 616 | + expect(headers_spy['X-Custom-Header']).to eq('custom-value') |
| 617 | + end |
| 618 | + |
| 619 | + it 'handles connection failures' do |
| 620 | + expect(Faraday).to receive(:new) |
| 621 | + .with('https://management-test.adyen.com/v3/stores', anything) |
| 622 | + .and_return(mock_faraday_connection) |
| 623 | + .and_yield(mock_faraday_connection) |
| 624 | + allow(mock_faraday_connection).to receive(:post).and_raise(Faraday::ConnectionFailed.new('Connection failed')) |
| 625 | + |
| 626 | + expect { |
| 627 | + client.call_adyen_api_url('https://management-test.adyen.com/v3/stores', 'post', {}, {}) |
| 628 | + }.to raise_error(Faraday::ConnectionFailed, /Connection to .* failed/) |
| 629 | + end |
| 630 | + |
| 631 | + it 'supports different HTTP methods' do |
| 632 | + mock_response = Faraday::Response.new(status: 200, body: '{"status":"updated"}', response_headers: {}) |
| 633 | + |
| 634 | + expect(Faraday).to receive(:new) |
| 635 | + .with('https://checkout-test.adyen.com/v71/paymentLinks/PL61C53A8B97E6915A', anything) |
| 636 | + .and_return(mock_faraday_connection) |
| 637 | + .and_yield(mock_faraday_connection) |
| 638 | + allow(mock_faraday_connection).to receive(:patch).and_return(mock_response) |
| 639 | + |
| 640 | + result = client.call_adyen_api_url( |
| 641 | + 'https://checkout-test.adyen.com/v71/paymentLinks/PL61C53A8B97E6915A', |
| 642 | + 'patch', |
| 643 | + { status: 'active' }, |
| 644 | + {} |
| 645 | + ) |
| 646 | + |
| 647 | + expect(result).to be_a(Adyen::AdyenResult) |
| 648 | + expect(result.status).to eq(200) |
| 649 | + end |
| 650 | + |
| 651 | + it 'raises for invalid HTTP method' do |
| 652 | + expect { |
| 653 | + client.call_adyen_api_url('https://management-test.adyen.com/v1/something', 'invalid', {}, {}) |
| 654 | + }.to raise_error(ArgumentError, "Invalid HTTP method: invalid") |
| 655 | + end |
| 656 | + |
| 657 | + it 'validates authentication is provided' do |
| 658 | + client_without_auth = Adyen::Client.new(env: :test) |
| 659 | + |
| 660 | + expect { |
| 661 | + client_without_auth.call_adyen_api_url( |
| 662 | + 'https://checkout-test.adyen.com/v71/paymentMethods/balance', |
| 663 | + 'post', |
| 664 | + {}, |
| 665 | + {} |
| 666 | + ) |
| 667 | + }.to raise_error(Adyen::AuthenticationError, /No authentication found/) |
| 668 | + end |
| 669 | + end |
546 | 670 | end |
0 commit comments