2013-03-17 73 views
1

我想創建一個訂閱我Paymill。我已經閱讀了他們的例子,但我似乎沒有得到它。Paymill訂閱PHP

我真正想要做的是建立一個訂閱,這裏是我當前的代碼:

$token = $_POST['paymillToken']; 

if ($token) { 
    require "Services/Paymill/Payments.php"; 
    require "Services/Paymill/Transactions.php"; 
    require "Services/Paymill/Subscriptions.php"; 
    require "Services/Paymill/Offers.php"; 


    $params = array(
    'amount'  => '49900', // Cent! 
    'currency' => 'SEK', // ISO 4217 
    'token'  => $token, 
    'description' => 'User ID# ' . $userIdMain . ' Email: ' . $userEmail 
); 

$transactionsObject = new Services_Paymill_Transactions(
    PAYMILL_API_KEY, PAYMILL_API_HOST 
); 
$transaction  = $transactionsObject->create($params); 

echo "<br /><br />";  
print_r($transaction); 

echo $transaction['client']['id']; 
echo $transaction['payment']['id']; 


$params = array(
    'client' => $transaction['client']['id'], 
    'offer' => 'offer_9cdffb501f565bf827a8', 
    'payment' => $transaction['payment']['id'] 
); 
$subscriptionsObject = new Services_Paymill_Subscriptions(PAYMILL_API_KEY, PAYMILL_API_HOST); 
$subscription  = $subscriptionsObject->create($params); 

echo "<br /><br />";  
print_r($subscription); 

} 

的問題是,上述一次創建兩個付款。但是,似乎訂閱對象要求我首先有一個payment_id(請參閱上面的$ transaction ['payment'] ['id'])。

我在這裏做錯了什麼?

回答

3

創建訂閱還會創建一個正確的交易。添加一個支付對象(https://www.paymill.com/en-gb/documentation-3/reference/api-reference/#create-new-credit-card-payment-with而不是創建一個新的交易並將id傳遞給Subscription-> create()。

+0

但我仍然沒有得到的東西很重要 - 我們如何獲得Paymill令牌用於*訂閱*?我的意思是,客戶同意訂閱,並且他/她立即付款(本身不是訂閱費)。客戶希望從那裏每月自動延期。那又怎麼樣?你如何獲得Paymill令牌,1個月後就是這樣?究竟如何?我真的無法理解它。你能幫我做一個工作流程嗎?編輯:順便說一句,我唯一能找到的是使用訂閱中的第一個支付對象。這會工作嗎? – dimitarvp

+0

@dimitko:你在這裏失蹤的是,每個訂閱都有一個優惠。要約就像一個定義數量和間隔的計劃。訂閱只是客戶,報價和所謂的「支付對象」之間的連接。因此,在添加訂閱後,它將根據報價的定義間隔執行。 –

+0

嗯,我可以讀取API,但我仍然很少搞砸。再次強調,如果您不介意提供該功能,則逐點工作流程將非常有用。 =) – dimitarvp

0

好吧,它不是PHP,但我有正確的步驟設置訂閱後有同樣的不確定性。最後它很容易,所以我發佈我的代碼到目前爲止,以防萬一它有幫助。

該應用程序是MVC.net,我使用PayMill PayButton來生成支付令牌。這是註冊過程中模型的一部分。以下代碼調用了PayMill API的.NET包裝器。

private static async Task<Subscription> MakePayMillSubscription(int amount, ApplicationUser user, PaymillContext paymillContext, Payment payment, Client client) 
    { 

     SubscriptionService subscriptionService = paymillContext.SubscriptionService; 
     Subscription subscription = await subscriptionService.CreateAsync(payment, client, null, amount, "EUR", Interval.periodWithChargeDay(1, Interval.TypeUnit.MONTH), null, user.Id, Interval.period(10, Interval.TypeUnit.YEAR)); 
     return subscription; 
    } 

    private static async Task<Client> MakePayMillClient(RegisterViewModel model, ApplicationUser user, PaymillContext paymillContext) 
    { 

     ClientService clientService = paymillContext.ClientService; 
     Client client = await clientService.CreateWithEmailAndDescriptionAsync(
      model.Email, 
      user.Id 
     ); 
     return client; 
    } 

    private static async Task<Payment> MakePayMillPayment(RegisterViewModel model,PaymillContext context, Client client) 
    { 

     Payment payment = await context.PaymentService.CreateWithTokenAndClientAsync(model.paymillToken, client); 

     return payment; 
    } 

然後我此刻這樣的註冊過程中調用這些(我仍然在測試階段)

client = await MakePayMillClient(model, user, paymillContext); 
    payment = await MakePayMillPayment(model, paymillContext,client); 
    subscription = await MakePayMillSubscription((int)(account.TotalAmountDue * 100.0M), user, paymillContext, payment, client);