Skip to content

Commit

Permalink
fixes for paypal at nameshake.net
Browse files Browse the repository at this point in the history
  • Loading branch information
james-stevens committed Feb 23, 2023
1 parent 74a19b7 commit b380dce
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 14 deletions.
12 changes: 8 additions & 4 deletions htdocs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1459,9 +1459,11 @@
x += `<tr><td colspan=5 class=promptCell>Amount to Pay :</td><td align=right>${format_amount(total-ctx.user.acct_current_balance)}</td></tr>`;
x += `<tr><td colspan=6><div class="line"/></td></tr>`;

x += "<tr><td colspan=6 align=right>Pay Now: "+make_pay_by_select("order_pay_by","pay_for_order")+"</td></tr>";
if (total > ctx.user.acct_current_balance) {
x += "<tr><td colspan=6 align=right>Pay Now: "+make_pay_by_select("order_pay_by","pay_for_order")+"</td></tr>";
x += "<tr><td colspan=6><div id='payment-whole'>&nbsp;</div></td></tr>";
}

x += "<tr><td colspan=6><div id='payment-whole'>&nbsp;</div></td></tr>";
return x+"</table></td></tr>";
}

Expand All @@ -1475,7 +1477,7 @@
let total = 0;
for(let order of ctx.orders) total += order.price_paid;

payments[pay_by.value].single(`${policy("business_name","Registrar")}: Domain Names`,total);
payments[pay_by.value].single(`${policy("business_name","Registrar")}: Domain Names`,(total-ctx.user.acct_current_balance));
}


Expand Down Expand Up @@ -1551,7 +1553,9 @@
}


function orders_icon()
function orders_icon() { load_domains_list(do_orders_icon); }

function do_orders_icon()
{
callApi("orders/details",(ok,reply) => {
if (!ok) return def_errMsg("Failed to load orders",reply,"errorSpan");
Expand Down
22 changes: 18 additions & 4 deletions htdocs/paypal.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,22 @@ function make_paypal_order(description, amount, custom_id)
{
"description": description,
"custom_id": custom_id,
"amount": { ...this_value, ...{ "breakdown": { "item_total": this_value } } }
"amount": this_value
}
] };

if ((ctx.orders)&&(ctx.orders.length > 0)) {
let this_total = 0;
let item_list = [];
for(let order of ctx.orders) {
let dom = domain_of(order.domain_id);
let yrs = `${order.num_years} yrs`;
if (order.num_years==1) yrs=`${order.num_years} yr`;
this_total += order.price_paid;

let yrs = `${order.num_years} yr`;
if (order.num_years>1) yrs += "s";

item_list.push({
"name": `${order.order_type} ${dom.display_name} for ${yrs}`,
"name": `${order.order_type} '${dom.display_name}' for ${yrs}`,
"quantity": 1,
"unit_amount": {
"currency_code": gbl.config.currency.iso,
Expand All @@ -81,6 +85,16 @@ function make_paypal_order(description, amount, custom_id)
});
}
ret_js.purchase_units[0].items = item_list;
ret_js.purchase_units[0].amount.breakdown = {
"item_total": {
"currency_code": gbl.config.currency.iso,
"value": format_amount(this_total,true)
},
"discount": {
"currency_code": gbl.config.currency.iso,
"value": format_amount(ctx.user.acct_current_balance,true)
}
};
return ret_js;
}

Expand Down
2 changes: 1 addition & 1 deletion python/librar/accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def admin_trans(injs):
if not ok or not user_db or len(user_db) <= 0:
return False, "Invalid user_id given"

amount = amt_from_float(amount)
amount = misc.amt_from_float(amount)
ok, trans_id = apply_transaction(user_id, amount, "Admin: " + injs["description"], as_admin=True)
if not ok:
return False, trans_id
Expand Down
2 changes: 1 addition & 1 deletion python/mailer/run_spooler.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def process_emails_waiting(server=None):
try:
ok, records = spool_email_file(path, server)
except Exception as e:
log(f"ERROR: Failed to email '{path}' - {e}")
log(f"ERROR: Exception running spool_email_file '{path}' - {type(e)}:{str(e)}")
ok = False

if ok:
Expand Down
14 changes: 10 additions & 4 deletions python/payments/plugins/paypal.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ def check_data(self):

self.desc = pay_unit["description"] if "description" in pay_unit else f"PayPal Credit"

return self.token is not None or self.email is not None or self.payer_id is not None
if self.token is None or self.email is None or self.payer_id is None:
return self.err_exit(f"Failed to find all data items ({self.token},{self.email},{self.payer_id})")

return True

def try_match_user(self, prov_ext, token, single_use=False):
where = {
Expand Down Expand Up @@ -189,7 +192,10 @@ def process_webhook(self):

def paypal_process_webhook(sent_data, filename):
hook = PayPalWebHook(sent_data, filename)
return hook.process_webhook()
ok, reply = hook.process_webhook()
if not ok:
log(f"PayPal Processing Error: {reply}")
return ok, reply


pay_handler.add_plugin(THIS_MODULE, {
Expand All @@ -203,8 +209,8 @@ def paypal_process_webhook(sent_data, filename):
def run_debug():
log_init(with_debug=True)
sql.connect("engine")
with open("/opt/github/pyrar/tmp/paypal2.json", "r", encoding="utf-8") as fd:
paypal_process_webhook(json.load(fd),"/opt/github/pyrar/tmp/paypal.json")
with open("/opt/github/pyrar/tmp/paypal3.json", "r", encoding="utf-8") as fd:
print(paypal_process_webhook(json.load(fd),"/opt/github/pyrar/tmp/paypal.json"))


if __name__ == "__main__":
Expand Down
146 changes: 146 additions & 0 deletions tmp/paypal3.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"id": "WH-77R17956UE4571227-3W352776K41340038",
"event_version": "1.0",
"create_time": "2023-02-23T15:22:32.624Z",
"resource_type": "checkout-order",
"resource_version": "2.0",
"event_type": "CHECKOUT.ORDER.APPROVED",
"summary": "An order has been approved by buyer",
"resource": {
"update_time": "2023-02-23T15:22:31Z",
"create_time": "2023-02-23T15:22:05Z",
"purchase_units": [
{
"reference_id": "default",
"amount": {
"currency_code": "USD",
"value": "40.00",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": "40.00"
}
}
},
"payee": {
"email_address": "[email protected]",
"merchant_id": "8ABGNSC8Q2SY2"
},
"description": "NameShake Registrar: Domain Names",
"custom_id": "28D2:ktTrcM3xiIgdx3pgNZxfqeKWIeRQyB",
"items": [
{
"name": "dom/create tam.chug for 1 yr",
"unit_amount": {
"currency_code": "USD",
"value": "40.00"
},
"quantity": "1"
}
],
"shipping": {
"name": {
"full_name": "John Doe"
},
"address": {
"address_line_1": "Whittaker House",
"address_line_2": "2 Whittaker Avenue",
"admin_area_2": "Richmond",
"admin_area_1": "Surrey",
"postal_code": "TW9 1EH",
"country_code": "GB"
}
},
"payments": {
"captures": [
{
"id": "7WL50287YT2658145",
"status": "COMPLETED",
"amount": {
"currency_code": "USD",
"value": "40.00"
},
"final_capture": true,
"seller_protection": {
"status": "ELIGIBLE",
"dispute_categories": [
"ITEM_NOT_RECEIVED",
"UNAUTHORIZED_TRANSACTION"
]
},
"seller_receivable_breakdown": {
"gross_amount": {
"currency_code": "USD",
"value": "40.00"
},
"paypal_fee": {
"currency_code": "USD",
"value": "1.66"
},
"net_amount": {
"currency_code": "USD",
"value": "38.34"
}
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/7WL50287YT2658145",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v2/payments/captures/7WL50287YT2658145/refund",
"rel": "refund",
"method": "POST"
},
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/9B7760128D630694X",
"rel": "up",
"method": "GET"
}
],
"create_time": "2023-02-23T15:22:31Z",
"update_time": "2023-02-23T15:22:31Z"
}
]
}
}
],
"links": [
{
"href": "https://api.sandbox.paypal.com/v2/checkout/orders/9B7760128D630694X",
"rel": "self",
"method": "GET"
}
],
"id": "9B7760128D630694X",
"payment_source": {
"paypal": {}
},
"intent": "CAPTURE",
"payer": {
"name": {
"given_name": "John",
"surname": "Doe"
},
"email_address": "[email protected]",
"payer_id": "SA3XU4N2ALCEA",
"address": {
"country_code": "GB"
}
},
"status": "COMPLETED"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-77R17956UE4571227-3W352776K41340038",
"rel": "self",
"method": "GET"
},
{
"href": "https://api.sandbox.paypal.com/v1/notifications/webhooks-events/WH-77R17956UE4571227-3W352776K41340038/resend",
"rel": "resend",
"method": "POST"
}
]
}

0 comments on commit b380dce

Please sign in to comment.