{"action":"created","comment":{"url":"https://api.github.com/repos/komoju/spree_komoju/pulls/comments/90385889","pull_request_review_id":10904205,"id":90385889,"diff_hunk":"@@ -7,18 +7,44 @@ def callback\n \n       case params[:type]\n       when \"payment.captured\"\n-        order_number = extract_payment_number(params[:data][:external_order_num])\n-        payment = Spree::Payment.find_by_number!(order_number)\n-        payment.complete! unless payment.completed?\n+        payment_captured!\n+      when \"payment.refunded\"\n+        payment_refunded!\n       end\n \n       head 200\n     end\n \n     private\n \n-    def extract_payment_number(external_order_num)\n-      external_order_num.split('-').try(:last)\n+    def order_number\n+      params[:data][:external_order_num].split(\"-\").try(:first)\n+    end\n+\n+    def payment_number\n+      params[:data][:external_order_num].split(\"-\").try(:last)\n+    end\n+\n+    def order\n+      @order ||= Spree::Order.find_by_number!(order_number)\n+    end\n+\n+    def payment\n+      @payment ||= order.payments.find_by_number!(payment_number)\n+    end\n+\n+    def payment_captured!\n+      payment.complete! unless payment.completed?\n+    end\n+\n+    def payment_refunded!\n+      if payment.completed?","path":"app/controllers/spree/komoju_controller.rb","position":null,"original_position":40,"commit_id":"161d11c3ec4405e585dbed9c06cc078ab9e99f61","original_commit_id":"f9260f6b782f6d5eda4dfda7228c390207aa3d35","user":{"login":"project-eutopia","id":5336619,"avatar_url":"https://avatars.githubusercontent.com/u/5336619?v=3","gravatar_id":"","url":"https://api.github.com/users/project-eutopia","html_url":"https://github.com/project-eutopia","followers_url":"https://api.github.com/users/project-eutopia/followers","following_url":"https://api.github.com/users/project-eutopia/following{/other_user}","gists_url":"https://api.github.com/users/project-eutopia/gists{/gist_id}","starred_url":"https://api.github.com/users/project-eutopia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-eutopia/subscriptions","organizations_url":"https://api.github.com/users/project-eutopia/orgs","repos_url":"https://api.github.com/users/project-eutopia/repos","events_url":"https://api.github.com/users/project-eutopia/events{/privacy}","received_events_url":"https://api.github.com/users/project-eutopia/received_events","type":"User","site_admin":false},"body":"@rramsden That is a good point.  It wouldn't make an infinite loop as it will still return 200, but the payment stays `complete` even when refunded.\r\n\r\nInstead, I improved this logic to check the that refund amount is less than the maximum possible refund value (`credit_allowed`), so that successive calls should not double up refunds.\r\n\r\nDo you think I should raise an error if the amount is too large or the currencies disagree?  The `captured` webhook does not raise an error if it is already `complete?`.","created_at":"2016-12-01T05:34:10Z","updated_at":"2016-12-01T05:34:10Z","html_url":"https://github.com/komoju/spree_komoju/pull/57#discussion_r90385889","pull_request_url":"https://api.github.com/repos/komoju/spree_komoju/pulls/57","_links":{"self":{"href":"https://api.github.com/repos/komoju/spree_komoju/pulls/comments/90385889"},"html":{"href":"https://github.com/komoju/spree_komoju/pull/57#discussion_r90385889"},"pull_request":{"href":"https://api.github.com/repos/komoju/spree_komoju/pulls/57"}}},"pull_request":{"url":"https://api.github.com/repos/komoju/spree_komoju/pulls/57","id":94775953,"html_url":"https://github.com/komoju/spree_komoju/pull/57","diff_url":"https://github.com/komoju/spree_komoju/pull/57.diff","patch_url":"https://github.com/komoju/spree_komoju/pull/57.patch","issue_url":"https://api.github.com/repos/komoju/spree_komoju/issues/57","number":57,"state":"open","locked":false,"title":"Handle refunded callback","user":{"login":"project-eutopia","id":5336619,"avatar_url":"https://avatars.githubusercontent.com/u/5336619?v=3","gravatar_id":"","url":"https://api.github.com/users/project-eutopia","html_url":"https://github.com/project-eutopia","followers_url":"https://api.github.com/users/project-eutopia/followers","following_url":"https://api.github.com/users/project-eutopia/following{/other_user}","gists_url":"https://api.github.com/users/project-eutopia/gists{/gist_id}","starred_url":"https://api.github.com/users/project-eutopia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-eutopia/subscriptions","organizations_url":"https://api.github.com/users/project-eutopia/orgs","repos_url":"https://api.github.com/users/project-eutopia/repos","events_url":"https://api.github.com/users/project-eutopia/events{/privacy}","received_events_url":"https://api.github.com/users/project-eutopia/received_events","type":"User","site_admin":false},"body":"Delivers: #54 \r\n\r\nThe `Spree::KomojuController` handles \"payment.refunded\" callbacks by creating a `Spree::Refund` on the associated payment.","created_at":"2016-11-22T09:27:34Z","updated_at":"2016-12-01T05:34:10Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ad59f1403c3d03d93b33fdc9734776def2394908","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/komoju/spree_komoju/pulls/57/commits","review_comments_url":"https://api.github.com/repos/komoju/spree_komoju/pulls/57/comments","review_comment_url":"https://api.github.com/repos/komoju/spree_komoju/pulls/comments{/number}","comments_url":"https://api.github.com/repos/komoju/spree_komoju/issues/57/comments","statuses_url":"https://api.github.com/repos/komoju/spree_komoju/statuses/161d11c3ec4405e585dbed9c06cc078ab9e99f61","head":{"label":"project-eutopia:handle_refunded_callback","ref":"handle_refunded_callback","sha":"161d11c3ec4405e585dbed9c06cc078ab9e99f61","user":{"login":"project-eutopia","id":5336619,"avatar_url":"https://avatars.githubusercontent.com/u/5336619?v=3","gravatar_id":"","url":"https://api.github.com/users/project-eutopia","html_url":"https://github.com/project-eutopia","followers_url":"https://api.github.com/users/project-eutopia/followers","following_url":"https://api.github.com/users/project-eutopia/following{/other_user}","gists_url":"https://api.github.com/users/project-eutopia/gists{/gist_id}","starred_url":"https://api.github.com/users/project-eutopia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-eutopia/subscriptions","organizations_url":"https://api.github.com/users/project-eutopia/orgs","repos_url":"https://api.github.com/users/project-eutopia/repos","events_url":"https://api.github.com/users/project-eutopia/events{/privacy}","received_events_url":"https://api.github.com/users/project-eutopia/received_events","type":"User","site_admin":false},"repo":{"id":73992801,"name":"spree_komoju","full_name":"project-eutopia/spree_komoju","owner":{"login":"project-eutopia","id":5336619,"avatar_url":"https://avatars.githubusercontent.com/u/5336619?v=3","gravatar_id":"","url":"https://api.github.com/users/project-eutopia","html_url":"https://github.com/project-eutopia","followers_url":"https://api.github.com/users/project-eutopia/followers","following_url":"https://api.github.com/users/project-eutopia/following{/other_user}","gists_url":"https://api.github.com/users/project-eutopia/gists{/gist_id}","starred_url":"https://api.github.com/users/project-eutopia/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/project-eutopia/subscriptions","organizations_url":"https://api.github.com/users/project-eutopia/orgs","repos_url":"https://api.github.com/users/project-eutopia/repos","events_url":"https://api.github.com/users/project-eutopia/events{/privacy}","received_events_url":"https://api.github.com/users/project-eutopia/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/project-eutopia/spree_komoju","description":"Spree extension for using the Komoju Payments API.","fork":true,"url":"https://api.github.com/repos/project-eutopia/spree_komoju","forks_url":"https://api.github.com/repos/project-eutopia/spree_komoju/forks","keys_url":"https://api.github.com/repos/project-eutopia/spree_komoju/keys{/key_id}","collaborators_url":"https://api.github.com/repos/project-eutopia/spree_komoju/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/project-eutopia/spree_komoju/teams","hooks_url":"https://api.github.com/repos/project-eutopia/spree_komoju/hooks","issue_events_url":"https://api.github.com/repos/project-eutopia/spree_komoju/issues/events{/number}","events_url":"https://api.github.com/repos/project-eutopia/spree_komoju/events","assignees_url":"https://api.github.com/repos/project-eutopia/spree_komoju/assignees{/user}","branches_url":"https://api.github.com/repos/project-eutopia/spree_komoju/branches{/branch}","tags_url":"https://api.github.com/repos/project-eutopia/spree_komoju/tags","blobs_url":"https://api.github.com/repos/project-eutopia/spree_komoju/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/project-eutopia/spree_komoju/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/project-eutopia/spree_komoju/git/refs{/sha}","trees_url":"https://api.github.com/repos/project-eutopia/spree_komoju/git/trees{/sha}","statuses_url":"https://api.github.com/repos/project-eutopia/spree_komoju/statuses/{sha}","languages_url":"https://api.github.com/repos/project-eutopia/spree_komoju/languages","stargazers_url":"https://api.github.com/repos/project-eutopia/spree_komoju/stargazers","contributors_url":"https://api.github.com/repos/project-eutopia/spree_komoju/contributors","subscribers_url":"https://api.github.com/repos/project-eutopia/spree_komoju/subscribers","subscription_url":"https://api.github.com/repos/project-eutopia/spree_komoju/subscription","commits_url":"https://api.github.com/repos/project-eutopia/spree_komoju/commits{/sha}","git_commits_url":"https://api.github.com/repos/project-eutopia/spree_komoju/git/commits{/sha}","comments_url":"https://api.github.com/repos/project-eutopia/spree_komoju/comments{/number}","issue_comment_url":"https://api.github.com/repos/project-eutopia/spree_komoju/issues/comments{/number}","contents_url":"https://api.github.com/repos/project-eutopia/spree_komoju/contents/{+path}","compare_url":"https://api.github.com/repos/project-eutopia/spree_komoju/compare/{base}...{head}","merges_url":"https://api.github.com/repos/project-eutopia/spree_komoju/merges","archive_url":"https://api.github.com/repos/project-eutopia/spree_komoju/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/project-eutopia/spree_komoju/downloads","issues_url":"https://api.github.com/repos/project-eutopia/spree_komoju/issues{/number}","pulls_url":"https://api.github.com/repos/project-eutopia/spree_komoju/pulls{/number}","milestones_url":"https://api.github.com/repos/project-eutopia/spree_komoju/milestones{/number}","notifications_url":"https://api.github.com/repos/project-eutopia/spree_komoju/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/project-eutopia/spree_komoju/labels{/name}","releases_url":"https://api.github.com/repos/project-eutopia/spree_komoju/releases{/id}","deployments_url":"https://api.github.com/repos/project-eutopia/spree_komoju/deployments","created_at":"2016-11-17T05:16:54Z","updated_at":"2016-11-17T05:16:55Z","pushed_at":"2016-12-01T05:31:30Z","git_url":"git://github.com/project-eutopia/spree_komoju.git","ssh_url":"git@github.com:project-eutopia/spree_komoju.git","clone_url":"https://github.com/project-eutopia/spree_komoju.git","svn_url":"https://github.com/project-eutopia/spree_komoju","homepage":"https://komoju.com/","size":173,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"komoju:master","ref":"master","sha":"1f820e074dba4fb0deeda54439f7d078f588452d","user":{"login":"komoju","id":8425317,"avatar_url":"https://avatars.githubusercontent.com/u/8425317?v=3","gravatar_id":"","url":"https://api.github.com/users/komoju","html_url":"https://github.com/komoju","followers_url":"https://api.github.com/users/komoju/followers","following_url":"https://api.github.com/users/komoju/following{/other_user}","gists_url":"https://api.github.com/users/komoju/gists{/gist_id}","starred_url":"https://api.github.com/users/komoju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/komoju/subscriptions","organizations_url":"https://api.github.com/users/komoju/orgs","repos_url":"https://api.github.com/users/komoju/repos","events_url":"https://api.github.com/users/komoju/events{/privacy}","received_events_url":"https://api.github.com/users/komoju/received_events","type":"Organization","site_admin":false},"repo":{"id":33156767,"name":"spree_komoju","full_name":"komoju/spree_komoju","owner":{"login":"komoju","id":8425317,"avatar_url":"https://avatars.githubusercontent.com/u/8425317?v=3","gravatar_id":"","url":"https://api.github.com/users/komoju","html_url":"https://github.com/komoju","followers_url":"https://api.github.com/users/komoju/followers","following_url":"https://api.github.com/users/komoju/following{/other_user}","gists_url":"https://api.github.com/users/komoju/gists{/gist_id}","starred_url":"https://api.github.com/users/komoju/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/komoju/subscriptions","organizations_url":"https://api.github.com/users/komoju/orgs","repos_url":"https://api.github.com/users/komoju/repos","events_url":"https://api.github.com/users/komoju/events{/privacy}","received_events_url":"https://api.github.com/users/komoju/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/komoju/spree_komoju","description":"Spree extension for using the Komoju Payments API.","fork":false,"url":"https://api.github.com/repos/komoju/spree_komoju","forks_url":"https://api.github.com/repos/komoju/spree_komoju/forks","keys_url":"https://api.github.com/repos/komoju/spree_komoju/keys{/key_id}","collaborators_url":"https://api.github.com/repos/komoju/spree_komoju/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/komoju/spree_komoju/teams","hooks_url":"https://api.github.com/repos/komoju/spree_komoju/hooks","issue_events_url":"https://api.github.com/repos/komoju/spree_komoju/issues/events{/number}","events_url":"https://api.github.com/repos/komoju/spree_komoju/events","assignees_url":"https://api.github.com/repos/komoju/spree_komoju/assignees{/user}","branches_url":"https://api.github.com/repos/komoju/spree_komoju/branches{/branch}","tags_url":"https://api.github.com/repos/komoju/spree_komoju/tags","blobs_url":"https://api.github.com/repos/komoju/spree_komoju/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/komoju/spree_komoju/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/komoju/spree_komoju/git/refs{/sha}","trees_url":"https://api.github.com/repos/komoju/spree_komoju/git/trees{/sha}","statuses_url":"https://api.github.com/repos/komoju/spree_komoju/statuses/{sha}","languages_url":"https://api.github.com/repos/komoju/spree_komoju/languages","stargazers_url":"https://api.github.com/repos/komoju/spree_komoju/stargazers","contributors_url":"https://api.github.com/repos/komoju/spree_komoju/contributors","subscribers_url":"https://api.github.com/repos/komoju/spree_komoju/subscribers","subscription_url":"https://api.github.com/repos/komoju/spree_komoju/subscription","commits_url":"https://api.github.com/repos/komoju/spree_komoju/commits{/sha}","git_commits_url":"https://api.github.com/repos/komoju/spree_komoju/git/commits{/sha}","comments_url":"https://api.github.com/repos/komoju/spree_komoju/comments{/number}","issue_comment_url":"https://api.github.com/repos/komoju/spree_komoju/issues/comments{/number}","contents_url":"https://api.github.com/repos/komoju/spree_komoju/contents/{+path}","compare_url":"https://api.github.com/repos/komoju/spree_komoju/compare/{base}...{head}","merges_url":"https://api.github.com/repos/komoju/spree_komoju/merges","archive_url":"https://api.github.com/repos/komoju/spree_komoju/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/komoju/spree_komoju/downloads","issues_url":"https://api.github.com/repos/komoju/spree_komoju/issues{/number}","pulls_url":"https://api.github.com/repos/komoju/spree_komoju/pulls{/number}","milestones_url":"https://api.github.com/repos/komoju/spree_komoju/milestones{/number}","notifications_url":"https://api.github.com/repos/komoju/spree_komoju/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/komoju/spree_komoju/labels{/name}","releases_url":"https://api.github.com/repos/komoju/spree_komoju/releases{/id}","deployments_url":"https://api.github.com/repos/komoju/spree_komoju/deployments","created_at":"2015-03-31T00:59:23Z","updated_at":"2016-10-27T09:22:56Z","pushed_at":"2016-12-01T05:31:31Z","git_url":"git://github.com/komoju/spree_komoju.git","ssh_url":"git@github.com:komoju/spree_komoju.git","clone_url":"https://github.com/komoju/spree_komoju.git","svn_url":"https://github.com/komoju/spree_komoju","homepage":"https://komoju.com/","size":199,"stargazers_count":3,"watchers_count":3,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":7,"mirror_url":null,"open_issues_count":9,"forks":7,"open_issues":9,"watchers":3,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/komoju/spree_komoju/pulls/57"},"html":{"href":"https://github.com/komoju/spree_komoju/pull/57"},"issue":{"href":"https://api.github.com/repos/komoju/spree_komoju/issues/57"},"comments":{"href":"https://api.github.com/repos/komoju/spree_komoju/issues/57/comments"},"review_comments":{"href":"https://api.github.com/repos/komoju/spree_komoju/pulls/57/comments"},"review_comment":{"href":"https://api.github.com/repos/komoju/spree_komoju/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/komoju/spree_komoju/pulls/57/commits"},"statuses":{"href":"https://api.github.com/repos/komoju/spree_komoju/statuses/161d11c3ec4405e585dbed9c06cc078ab9e99f61"}}}}
{"actor":{"display_login":"project-eutopia"}}