-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add webhooks verify to extension paypal sdk
Every call to the paypal webhooks endpoint must be validated to proceed
- Loading branch information
Showing
4 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
lib/solidus_paypal_marketplace/paypal_partner_sdk/webhook_verify.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
frozen_string_literal: true | ||
|
||
module SolidusPaypalMarketplace | ||
module PaypalPartnerSdk | ||
class WebhookVerify | ||
PATH = '/v1/notifications/verify-webhook-signature' | ||
VERB = 'POST' | ||
HEADERS = { "Content-Type": "application/json" }.freeze | ||
|
||
attr_accessor :path, :verb, :headers, :body | ||
|
||
def initialize(headers:, params:) | ||
@path = PATH.dup | ||
@verb = VERB.dup | ||
@headers = { "Content-Type" => "application/json" } | ||
@body = { | ||
"auth_algo" => headers["PAYPAL-AUTH-ALGO"], | ||
"cert_url" => headers["PAYPAL-CERT-URL"], | ||
"transmission_id" => headers["PAYPAL-TRANSMISSION-ID"], | ||
"transmission_sig" => headers["PAYPAL-TRANSMISSION-SIG"], | ||
"transmission_time" => headers["PAYPAL-TRANSMISSION-TIME"], | ||
"webhook_id" => SolidusPaypalMarketplace.config.paypal_webhook_id, | ||
"webhook_event" => params | ||
} | ||
end | ||
end | ||
end | ||
end |
63 changes: 63 additions & 0 deletions
63
spec/lib/solidus_paypal_marketplace/paypal_partner_sdk/webhook_verify_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'spec_helper' | ||
|
||
RSpec.describe SolidusPaypalMarketplace::PaypalPartnerSdk::WebhookVerify do | ||
subject(:verify) { described_class.new(headers: headers, params: params) } | ||
|
||
let(:headers) { | ||
{ | ||
"PAYPAL-AUTH-ALGO" => "string", | ||
"PAYPAL-CERT-URL" => "string", | ||
"PAYPAL-TRANSMISSION-ID" => "string", | ||
"PAYPAL-TRANSMISSION-SIG" => "string", | ||
"PAYPAL-TRANSMISSION-TIME" => "string" | ||
} | ||
} | ||
let(:params) { | ||
{ something: "string" } | ||
} | ||
|
||
before do | ||
SolidusPaypalMarketplace.config.paypal_webhook_id = "string" | ||
end | ||
|
||
it { is_expected.to respond_to(:path) } | ||
it { is_expected.to respond_to(:verb) } | ||
it { is_expected.to respond_to(:headers) } | ||
it { is_expected.to respond_to(:body) } | ||
|
||
describe "#path" do | ||
it do | ||
expect(verify.path).to eq '/v1/notifications/verify-webhook-signature' | ||
end | ||
end | ||
|
||
describe "#verb" do | ||
it do | ||
expect(verify.verb).to eq 'POST' | ||
end | ||
end | ||
|
||
describe "#headers" do | ||
it do | ||
expect(verify.headers['Content-Type']).to eq 'application/json' | ||
end | ||
end | ||
|
||
describe "#body" do | ||
[ | ||
"auth_algo", | ||
"cert_url", | ||
"transmission_id", | ||
"transmission_sig", | ||
"transmission_time", | ||
"webhook_id", | ||
"webhook_event" | ||
].each do |key| | ||
it "includes #{key}" do | ||
expect(verify.body[key]).to be_present | ||
end | ||
end | ||
end | ||
end |