| Conditions | 5 |
| Paths | 16 |
| Total Lines | 97 |
| Code Lines | 64 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 64 | function stripe_session_ajax($successPage, $cancelPage) |
||
| 65 | { |
||
| 66 | $amount = donate_amount(); |
||
| 67 | $giftaid = giftaid_decision(); |
||
| 68 | $howoften = $_POST["how-often"]; |
||
| 69 | $utm_source = $_POST["utm_source"]; |
||
| 70 | $utm_content = $_POST["utm_content"]; |
||
| 71 | $utm_medium = $_POST["utm_medium"]; |
||
| 72 | $utm_campaign = $_POST["utm_campaign"]; |
||
| 73 | $full_name = $_POST["full_name"]; |
||
| 74 | $contact_permission = $_POST["contact_permission"] ?? "No"; |
||
| 75 | |||
| 76 | # backward compatibility |
||
| 77 | if ($howoften == "recurring") { |
||
| 78 | $howoften = "monthly"; |
||
| 79 | } |
||
| 80 | $validPeriods = ["monthly" => "month", "annually" => "year"]; |
||
| 81 | $period = $validPeriods[$howoften] ?? "once"; |
||
| 82 | |||
| 83 | $metadata = [ |
||
| 84 | "gift-aid" => $giftaid, |
||
| 85 | "gift-aid-name" => $full_name, |
||
| 86 | "utm_source" => $utm_source, |
||
| 87 | "utm_content" => $utm_content, |
||
| 88 | "utm_medium" => $utm_medium, |
||
| 89 | "utm_campaign" => $utm_campaign, |
||
| 90 | "contact_permission" => $contact_permission, |
||
| 91 | ]; |
||
| 92 | // set billing addres var to required if giftaid is true |
||
| 93 | if ($giftaid == "Yes") { |
||
| 94 | $collectBilling = "required"; |
||
| 95 | $name = "Donation to mySociety (with gift aid)"; |
||
| 96 | } else { |
||
| 97 | $collectBilling = "auto"; |
||
| 98 | $name = "Donation to mySociety"; |
||
| 99 | } |
||
| 100 | $data = [ |
||
| 101 | "payment_method_types" => ["card", "bacs_debit", "paypal"], |
||
| 102 | "success_url" => $successPage, |
||
| 103 | "cancel_url" => $cancelPage, |
||
| 104 | "billing_address_collection" => $collectBilling, |
||
| 105 | ]; |
||
| 106 | |||
| 107 | $mysocDesc = " |
||
| 108 | mySociety is a charity committed to making a fairer society |
||
| 109 | by providing digital services, research and data, openly and |
||
| 110 | at no cost. We use technology to help people understand and |
||
| 111 | take part in the decisions that affect their lives and communities. |
||
| 112 | We run services such as TheyWorkForYou, WhatDoTheyKnow and FixMyStreet. |
||
| 113 | "; |
||
| 114 | |||
| 115 | if ($period == "once") { |
||
| 116 | // one off payments |
||
| 117 | $data += [ |
||
| 118 | "mode" => "payment", |
||
| 119 | "payment_intent_data" => [ |
||
| 120 | "metadata" => $metadata, |
||
| 121 | ], |
||
| 122 | "submit_type" => "donate", |
||
| 123 | "line_items" => [ |
||
| 124 | [ |
||
| 125 | "amount" => $amount * 100, |
||
| 126 | "currency" => "gbp", |
||
| 127 | "name" => $name, |
||
| 128 | "description" => $mysocDesc, |
||
| 129 | "quantity" => 1, |
||
| 130 | ], |
||
| 131 | ], |
||
| 132 | ]; |
||
| 133 | } else { |
||
| 134 | // recurring subscription payments |
||
| 135 | $data += [ |
||
| 136 | "mode" => "subscription", |
||
| 137 | "subscription_data" => [ |
||
| 138 | "metadata" => $metadata, |
||
| 139 | ], |
||
| 140 | "line_items" => [ |
||
| 141 | [ |
||
| 142 | "price_data" => [ |
||
| 143 | "unit_amount" => $amount * 100, |
||
| 144 | "currency" => "gbp", |
||
| 145 | "product_data" => [ |
||
| 146 | "name" => $name, |
||
| 147 | "description" => $mysocDesc, |
||
| 148 | ], |
||
| 149 | "recurring" => ["interval" => $period], |
||
| 150 | ], |
||
| 151 | "quantity" => 1, |
||
| 152 | ], |
||
| 153 | ], |
||
| 154 | ]; |
||
| 155 | } |
||
| 156 | $result = stripe_post("/checkout/sessions", $data); |
||
| 157 | if (is_string($result)) { |
||
| 158 | return ["error" => $result]; |
||
| 159 | } else { |
||
| 160 | return $result; |
||
| 161 | } |
||
| 224 | } |