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