package com.diaoyun.zion.chinafrica.controller;

import com.diaoyun.zion.chinafrica.service.PayPalService;
import com.diaoyun.zion.master.base.Result;
import com.egzosn.pay.common.api.PayService;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.http.HttpConfigStorage;
import com.egzosn.pay.paypal.api.PayPalConfigStorage;
import com.egzosn.pay.paypal.api.PayPalPayService;
import com.egzosn.pay.paypal.bean.PayPalTransactionType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Map;
import java.util.UUID;


/**
 * PayPal支付
 */
@RestController
@RequestMapping("/paypal")
public class PayPalController {

    @Autowired
    private PayPalService payPalService;

    @PostMapping ("/pay")
    public Result payForOrder(@RequestParam("orderId") String orderId){

        return payPalService.payForOrder(orderId);
    }

    @PostMapping("/success")
    public Result payForSuccess(HttpServletRequest request,@RequestParam("orderId") String orderId){

        return payPalService.payForSuccess(request,orderId);
    }

    @RequestMapping("/testPay")
    public Result testPay(){
        PayPalConfigStorage storage = new PayPalConfigStorage();
        storage.setClientID("AWbyqn1AKJeVvzNsYms_9fs_LVr5_w5Xyt8nX5q0Wmqoq0NYCZf2wyz9DDeA5ArsWx0LFBwl5Oz6amD2");
        storage.setClientSecret("EFhRjCBzhtI1lf88Xiy3VF07gVpu1vWk0VheSP3n5J0fhQxA6Pu7j89dbyYTLj3FxqiJBOlxQRKG_58e");
        //storage.setTest(true);
        //发起付款后的页面转跳地址
        storage.setReturnUrl("http://127.0.0.1:8083/pay/success");
        //取消按钮转跳地址,这里用异步通知地址的兼容的做法
        storage.setNotifyUrl("http://127.0.0.1:8083/pay/cancel");
        HttpConfigStorage httpConfigStorage = new HttpConfigStorage();

        PayService service = new PayPalPayService(storage);
        BigDecimal price = new BigDecimal("500");
        PayOrder order = new PayOrder("Order_payment:", "Order payment", price, UUID.randomUUID().toString().replace("-", ""), PayPalTransactionType.sale);
        Map orderInfo = service.orderInfo(order);
        service.buildRequest(orderInfo, MethodType.POST);
        return new Result().setMessage("付款成功!");
    }

}
