Skip to content
This repository has been archived by the owner on Apr 8, 2020. It is now read-only.

Commit

Permalink
payment test implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
h4child committed Jun 3, 2018
1 parent e4522e2 commit 22776ab
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 24 deletions.
6 changes: 3 additions & 3 deletions include/framebot/framebot.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

#define API_GETUPDATES "getUpdates\
?offset=%ld\
&limit=%ld\
&limit=%d\
&timeout=%ld\
&allowed_updates=%s"

Expand Down Expand Up @@ -239,7 +239,7 @@
&reply_markup=%s"

#define API_answerShippingQuery "answerShippingQuery\
?shipping_query_id=%ld\
?shipping_query_id=%s\
&ok=%s\
&shipping_options=%s\
&error_message=%s"
Expand Down Expand Up @@ -346,7 +346,7 @@ Error * show_error();

/** Available methods **/
User * get_me(Bot *bot);
Framebot *get_updates (Bot *bot, Framebot *framebot, int64_t offset, int64_t limit,
Framebot *get_updates (Bot *bot, Framebot *framebot, int64_t offset, int32_t limit,
int64_t timeout, char *allowed_updates);

/* sendMessage */
Expand Down
3 changes: 2 additions & 1 deletion include/framebot/payments.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ void option_payment(bool n_name, bool n_phone, bool n_email, bool n_shipping_add
bool s_email_to_provider, bool is_flexible);
Message *send_invoice(Bot *bot, int64_t chat_id, char * title, char *description, char *payload, char *start_parameter, char *prices,
char *provider_data, int64_t reply_to_message_id, char *reply_markup);
bool answerPreCheckoutQuery(Bot *bot, char *pre_checkout_query_id, char *ok, char *error_message);
bool answerShippingQuery(Bot *bot, char *shipping_query_id, bool ok, char *shipping_options, char *error_message);
bool answerPreCheckoutQuery(Bot *bot, char *pre_checkout_query_id, bool ok, char *error_message);


#endif
2 changes: 1 addition & 1 deletion src/framebot.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ User *get_me (Bot *bot) {
* Returns the updates list
* https://core.telegram.org/bots/api#getupdates
*/
Framebot *get_updates (Bot *bot, Framebot *framebot, int64_t offset, int64_t limit,
Framebot *get_updates (Bot *bot, Framebot *framebot, int64_t offset, int32_t limit,
int64_t timeout, char *allowed_updates) {

refjson *s_json = NULL;
Expand Down
9 changes: 8 additions & 1 deletion src/payments.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ static char *currency[SIZECURRENCY] =
"ZAR" /* South African Rand .................. ZAR 12.66 ZAR 126,694.98 */
};


bool set_currency(uint32_t code){
if(code >= SIZECURRENCY)
return false;
Expand Down Expand Up @@ -247,6 +248,9 @@ bool answerShippingQuery(Bot *bot, char *shipping_query_id, bool ok, char *shipp
shipping_query_id, OK(ok), SHIPPING_OPTIONS(shipping_options),
ERROR_MESSAGE(error_message));

if(!s_json)
return -1;

result = json_is_true(s_json->content);

close_json(s_json);
Expand All @@ -259,14 +263,17 @@ bool answerShippingQuery(Bot *bot, char *shipping_query_id, bool ok, char *shipp
* https://core.telegram.org/bots/api#answerprecheckoutquery
*
*/
bool answerPreCheckoutQuery(Bot *bot, char *pre_checkout_query_id, char *ok, char *error_message){
bool answerPreCheckoutQuery(Bot *bot, char *pre_checkout_query_id, bool ok, char *error_message){

bool result;
refjson *s_json;

s_json = generic_method_call(bot->token, API_answerPreCheckoutQuery,
pre_checkout_query_id, OK(ok), ERROR_MESSAGE(error_message));

if(!s_json)
return -1;

result = json_is_true(s_json->content);

close_json(s_json);
Expand Down
106 changes: 88 additions & 18 deletions test/test_payments.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,66 @@ char *provider_token = NULL;
char *prices = "[{\"label\":\"produto 1\", \"amount\":4517}]";
int valid_username = 0;


bool accpte_payment(){

Framebot *framebot = NULL;

char *tax = "{\"id\":\"product_12\", \"title\":\"Product_12\", \"prices\":[{\"label\":\"tax\", \"amount\":515}]}";
char *notax = "";

while(1){
if(framebot == NULL)
framebot = get_updates(_bot, framebot, 0, 0, 0, NULL);
else
framebot = get_updates(_bot, framebot, framebot->update_id + 1, 0, 0, NULL);

while(framebot->up_shipping_query != NULL){
Update *s_q = framebot->up_shipping_query;

if(strcmp(s_q->shipping_query->invoice_payload, "product_12") == 0 ||
strcmp(s_q->shipping_query->invoice_payload, "product_11") == 0){
answerShippingQuery(_bot, s_q->shipping_query->id, 1, "[{\"id\":\"product_12\", \"title\":\"Product_12\", \"prices\":[{\"label\":\"tax\", \"amount\":515}]}]", NULL);
}
else{
answerShippingQuery(_bot, s_q->shipping_query->id, 1, NULL, NULL);
}


Error *error = get_error();
if(error)
printf(RED"false\ncode:%ld | description:%s\n"COLOR_RESET, error->error_code, error->description);

framebot->up_shipping_query = framebot->up_shipping_query->next;
update_free(s_q);

}
while(framebot->up_pre_checkout_query != NULL){
Update *p_c_q = framebot->up_pre_checkout_query;

answerPreCheckoutQuery(_bot, p_c_q->pre_checkout_query->id, 1, NULL);

Error *error = get_error();
if(error)
printf(RED"false\ncode:%ld | description:%s\n"COLOR_RESET, error->error_code, error->description);

framebot->up_pre_checkout_query = framebot->up_pre_checkout_query->next;
update_free(p_c_q);
}
}
}


void test_invoice(){
Message *result = NULL;

printf(WHITE "Send Invoice ... \n");
set_provider_token(provider_token);


printf(WHITE "Send invoice ........." COLOR_RESET);
printf(WHITE "Send invoice .............................." COLOR_RESET);
fflush(stdout);
result = send_invoice(_bot, chat_id, "product 1", "(description)", "(param_payload)", "product_1", prices,
result = send_invoice(_bot, chat_id, "product 1", "(description)", "product_1", "forward_product1", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -35,10 +85,11 @@ void test_invoice(){
}


printf(WHITE "Send invoice(currency) ........." COLOR_RESET);

printf(WHITE "Send invoice(currency) ............." COLOR_RESET);
fflush(stdout);
set_currency(CODE_EUR);
result = send_invoice(_bot, chat_id, "product 2", "(description)", "(param_payload)", "product_2", prices,
result = send_invoice(_bot, chat_id, "product 2", "currency (description)", "product 2", "forward_product2", prices,
NULL, 0, NULL);
set_currency(CODE_USD); /* back DOLLAR */
if(result){
Expand All @@ -52,11 +103,11 @@ void test_invoice(){
}


printf(WHITE "Send invoice(photo) ........." COLOR_RESET);
printf(WHITE "Send invoice(photo) ..............." COLOR_RESET);
fflush(stdout);
/* add_img_invoice(char *photo_url, uint64_t photo_size, uint32_t photo_width, uint32_t photo_heigth) */
add_img_invoice("https://telegram.org/img/t_logo.png", 0, 50, 50);
result = send_invoice(_bot, chat_id, "product 3", "(description)", "(param_payload)", "product_3", prices,
result = send_invoice(_bot, chat_id, "product 3", "photo (description)", "product_3", "forward_product3", prices,
NULL, 0, NULL);
remove_image_invoice(); /* remove invoice image */
if(result){
Expand All @@ -73,7 +124,7 @@ void test_invoice(){
printf(WHITE "Send invoice(option : need_name) ........." COLOR_RESET);
fflush(stdout);
option_payment(1, 0, 0, 0, 0, 0, 0);
result = send_invoice(_bot, chat_id, "product 4", "(description)", "(param_payload)", "product_4", prices,
result = send_invoice(_bot, chat_id, "product 4", "need_name (description)", "product_4", "forward_product4", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -89,7 +140,7 @@ void test_invoice(){
printf(WHITE "Send invoice(option : need_phone_number) ........." COLOR_RESET);
fflush(stdout);
option_payment(0, 1, 0, 0, 0, 0, 0);
result = send_invoice(_bot, chat_id, "product 5", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 5", "need_phone_number (description)", "product_5", "forward_product5", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -105,7 +156,7 @@ void test_invoice(){
printf(WHITE "Send invoice(option : need_email) ........." COLOR_RESET);
fflush(stdout);
option_payment(0, 0, 1, 0, 0, 0, 0);
result = send_invoice(_bot, chat_id, "product 6", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 6", "need_email (description)", "product_6", "forward_product6", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -118,10 +169,10 @@ void test_invoice(){
}


printf(WHITE "Send invoice(option : need_shipping_address) ........." COLOR_RESET);
printf(WHITE "Send invoice(option : need_shipping_address) .." COLOR_RESET);
fflush(stdout);
option_payment(0, 0, 0, 1, 0, 0, 0);
result = send_invoice(_bot, chat_id, "product 7", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 7", "need_shipping_address (description)", "product_7", "forward_product7", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -134,10 +185,10 @@ void test_invoice(){
}


printf(WHITE "Send invoice(option : send_phone_number_to_provider) ........." COLOR_RESET);
printf(WHITE "Send invoice(option : send_phone_number_to_provider) ..." COLOR_RESET);
fflush(stdout);
option_payment(0, 0, 0, 0, 1, 0, 0);
result = send_invoice(_bot, chat_id, "product 8", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 8", "send_phone_number_to_provider (description)", "product_8", "forward_product8", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -150,10 +201,10 @@ void test_invoice(){
}


printf(WHITE "Send invoice(option : send_email_to_provider) ........." COLOR_RESET);
printf(WHITE "Send invoice(option : send_email_to_provider) ...." COLOR_RESET);
fflush(stdout);
option_payment(0, 0, 0, 0, 0, 1, 0);
result = send_invoice(_bot, chat_id, "product 9", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 9", "send_email_to_provider (description)", "product_9", "forward_product9", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -169,7 +220,7 @@ void test_invoice(){
printf(WHITE "Send invoice(option : is_flexible) ........." COLOR_RESET);
fflush(stdout);
option_payment(0, 0, 0, 0, 0, 0, 1);
result = send_invoice(_bot, chat_id, "product 10", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 10", "is_flexible (description)", "product_10", "forward_product10", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -185,7 +236,23 @@ void test_invoice(){
printf(WHITE "Send invoice(option : all) ........." COLOR_RESET);
fflush(stdout);
option_payment(1, 1, 1, 1, 1, 1, 1);
result = send_invoice(_bot, chat_id, "product 11", "(description)", "(param_payload)", "para_start_parameter", prices,
result = send_invoice(_bot, chat_id, "product 11", "all (description)", "product_11", "forward_product11", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
}
else{
Error *error = get_error();
if(error)
printf(RED"false\ncode:%ld | description:%s\n"COLOR_RESET, error->error_code, error->description);
exit(-1);
}


printf(WHITE "Send invoice(option : tax) ........." COLOR_RESET);
fflush(stdout);
option_payment(0, 0, 0, 0, 0, 0, 1);
result = send_invoice(_bot, chat_id, "product 12", "delivery (description)", "product_12", "forward_product12", prices,
NULL, 0, NULL);
if(result){
printf(BLUE "OK\n" COLOR_RESET);
Expand All @@ -198,6 +265,7 @@ void test_invoice(){
}
}


int main(int argc, char *argv[]){
framebot_init();

Expand All @@ -222,11 +290,13 @@ int main(int argc, char *argv[]){
update = get_updates(_bot, update, 0, 0, 0, "message");
message = update->up_message;


while(message){
if(strcmp(update->up_message->message->from->username, argv[3]) == 0){
valid_username = 1;
chat_id = update->up_message->message->from->id;
test_invoice();
accpte_payment();
break;
}

Expand All @@ -236,7 +306,7 @@ int main(int argc, char *argv[]){
}

if(valid_username == 0)
printf("\nUsername %s not found", argv[3]);
printf(RED "\nUsername %s not found" COLOR_RESET, argv[3]);


return 0;
Expand Down

0 comments on commit 22776ab

Please sign in to comment.